【问题标题】:Configuring Spring MVC REST to return only xml将 Spring MVC REST 配置为仅返回 xml
【发布时间】:2013-04-17 13:10:32
【问题描述】:

我已经配置了一个应用程序,它根据 url 末尾附加的内容返回 json 和 xml 视图。

根据:

http://pfelitti87.blogspot.co.uk/2012/07/rest-services-with-spring-3-xml-json.html

我想重新配置但只允许返回xml。

不知道怎么做,我所做的一切仍然返回 json,当没有附加到末尾时

   @Bean(name = "viewResolver")
        public ContentNegotiatingViewResolver viewResolver() {
            ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
            contentNegotiatingViewResolver.setOrder(1);
            contentNegotiatingViewResolver.setFavorPathExtension(true);
            contentNegotiatingViewResolver.setFavorParameter(true);
            contentNegotiatingViewResolver.setIgnoreAcceptHeader(false);
            Map<String, String> mediaTypes = new HashMap<String, String>();
            mediaTypes.put("xml", "application/xml");
            contentNegotiatingViewResolver.setMediaTypes(mediaTypes);
            List<View> defaultViews = new ArrayList<View>();
            defaultViews.add(xmlView());
            contentNegotiatingViewResolver.setDefaultViews(defaultViews);
            return contentNegotiatingViewResolver;
        }

所以输出是这样的:

.../state.json    error no jsp page json

.../state.xml     works fine

.../state         error no jsp page

我想要的是以下两个 url 来返回 xml

.../state

.../state.xml

以上都行不通..

这是控制器映射

@RequestMapping(value= "/{id}/{ref}/state", method = RequestMethod.GET)
public state getState(...) {...}

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    这是一个通过xml配置的解决方案; - 控制器

    @Controller 
    @RequestMapping("/simpleEntity")
    public class SimpleRestController {
    
        @RequestMapping(method=RequestMethod.GET)
        public @ResponseBody SimpleEntity get() {
            return new SimpleEntity();
        }   
    

    }

    xml

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="xstream"/>
                <property name="unmarshaller" ref="xstream"/>
                <property name="supportedMediaTypes">
                    <list>
                        <value>#{T(org.springframework.http.MediaType).TEXT_XML_VALUE}</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <context:component-scan base-package="de.incompleteco.spring.web"/>
    
    <bean id="xstream" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
    

    这是一个配置版本

    @Configuration
    @EnableWebMvc
    @ComponentScan("de.incompleteco.spring.web")
    public class SimpleConfiguration extends WebMvcConfigurerAdapter {
    
        private XStreamMarshaller marshaller;
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(getHttpMessageConverter());
            super.configureMessageConverters(converters);
    
        }
    
        public HttpMessageConverter<Object> getHttpMessageConverter() {
            MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
            converter.setMarshaller(marshaller());
            converter.setUnmarshaller(marshaller());
            return converter;
        }
    
        @Bean
        public AbstractMarshaller marshaller() {
            if (marshaller == null) {
                return new XStreamMarshaller();
            } else {
                return marshaller;
            }
        }
    
    }
    

    【讨论】:

    • 谢谢你。如何将它添加到控制器中产生={“application/xml”}
    • 是的 - 可以做到 - 一个“更好”的方法是 produces=MediaType.APPLICATION_XML_VALUE - 保存任何拼写错误
    【解决方案2】:

    试试下面的

    从类路径中移除jackson

    ContentNegotiatingViewResolvermediaTypes 中删除条目&lt;entry key="json" value="application/json" /&gt;

    defaultViewsContentNegotiatingViewResolver 中删除bean &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt;

    【讨论】:

    • 我已经这样做了,删除了 json.. 但是当没有附加任何内容时,我不能将其默认为 xml...
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2022-01-23
    • 1970-01-01
    • 2012-08-02
    • 2012-03-23
    • 1970-01-01
    相关资源
    最近更新 更多