1

我已经配置了一个应用程序,它根据 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

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

.../state

.../state.xml

上面的不行。。

这是控制器映射

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

2 回答 2

2

这是通过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;
        }
    }

}
于 2013-04-17T14:00:10.063 回答
0

尝试以下

从类路径中删除杰克逊

<entry key="json" value="application/json" />mediaTypes中删除条目ContentNegotiatingViewResolver

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />defaultViews中删除 bean ContentNegotiatingViewResolver

于 2013-04-17T13:15:03.010 回答