Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
配置Spring MVC REST以仅返回xml_Spring_Spring Mvc - Fatal编程技术网

配置Spring MVC REST以仅返回xml

配置Spring MVC REST以仅返回xml,spring,spring-mvc,Spring,Spring Mvc,我已经配置了一个应用程序,它根据url末尾附加的内容返回json和xml视图 根据: 我想重新配置它,但只允许返回xml 不知道该怎么做,我所做的一切仍然返回json,而结尾没有附加任何内容 @Bean(name = "viewResolver") public ContentNegotiatingViewResolver viewResolver() { ContentNegotiatingViewResolver contentNegotiat

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

根据:

我想重新配置它,但只允许返回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;
        }
我想要的是以下两个URL返回xml

.../state

.../state.xml
上述方法行不通

这是控制器映射

@RequestMapping(value= "/{id}/{ref}/state", method = RequestMethod.GET)
public state getState(...) {...}
试试下面的方法

从类路径中删除jackson

ContentNegotiatingViewResolver
mediaTypes
中删除条目


ContentNegotiatingViewResolver
defaultview
中删除bean
,下面是一个通过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"/>

#{T(org.springframework.http.MediaType).TEXT\u XML\u VALUE}
这是一个配置版本

@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;
        }
    }

}
@配置
@EnableWebMvc
@组件扫描(“de.incompleteco.spring.web”)
公共类SimpleConfiguration扩展了WebMVCConfigureAdapter{
私人XStreamMarshaller marshaller;
@凌驾

public void configureMessageConverters(列表我已经完成了,删除了json..但是我不能在没有附加任何内容时将其默认为xml…谢谢..将其添加到控制器中如何产生={“应用程序/xml”}是-这是可以做到的-更好的方法是
products=MediaType。应用程序\u XML\u VALUE
-保存所有拼写错误
@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;
        }
    }

}