部署为WAR时,与param的Springboot内容协商无法解决

时间:2015-04-29 19:22:52

标签: rest spring-mvc spring-boot content-negotiation

我正在尝试在json和xml之间的@RestController上格式化我的响应内容类型。它似乎适用于Accept标头或路径扩展(.json.xml)的情况。我的应用程序打包为WAR并部署到tomcat实例。但是我目前面临着两个问题。

1)即使我在ContentNegotiationConfigurer上将我的默认内容类型设置为JSON,curl -X GET -H "Cache-Control: no-cache" http://localhost:8080/v1/api/version之类的请求也会解析为XML

2)当我在请求中使用param时,内容会错误地解析。在访问curl -X GET -H "Cache-Control: no-cache" http://localhost:8080/v1/api/version?format=json时,我得到了XML。

SpringBoot应用程序

@EnableAutoConfiguration
@ComponentScan
@Configuration
@EnableWebMvc
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder     builder) {
        builder.sources(Application.class);
        return super.configure(builder);
    }
}

WebMvcConfigurerAdapter

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {    

    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;    

    private CustomObjectMapper objectMapper;    

    @PostConstruct
    public void init() {
        List<HttpMessageConverter<?>> messageConverters = requestMappingHandlerAdapter.getMessageConverters();
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter m = (MappingJackson2HttpMessageConverter) messageConverter;
                m.setObjectMapper(objectMapper);
            }
        }
    }    

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
                .defaultContentType(MediaType.APPLICATION_JSON)
                .favorParameter(true)
                .useJaf(false)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .mediaType("json", MediaType.APPLICATION_JSON);
        super.configureContentNegotiation(configurer);
    }    


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(mappingJackson2HttpMessageConverter);    

        converters.add(createXmlHttpMessageConverter());
        super.configureMessageConverters(converters);
    }    

    private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
        MarshallingHttpMessageConverter xmlConverter =
                new MarshallingHttpMessageConverter();    

        XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
        xmlConverter.setMarshaller(xstreamMarshaller);
        xmlConverter.setUnmarshaller(xstreamMarshaller);
        return xmlConverter;
    }    

    @Autowired
    public void setRequestMappingHandlerAdapter(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
        this.requestMappingHandlerAdapter  = requestMappingHandlerAdapter;
    }    

    @Autowired
    public void setObjectMapper(CustomObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }
}

休息控制器

@RestController
@RequestMapping("/v1/api")
public class RestVersionController {    

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/version", method = RequestMethod.GET)
    public
    @ResponseBody
    ApiVersion getVersion() {
        return new ApiVersion(1);
    }    

    @JsonIgnoreProperties(ignoreUnknown = true)
    @XmlRootElement
    public static class ApiVersion {
        int version;
        String currentVersion = "Current version is ";    

        public ApiVersion() {
        }    

        public ApiVersion(int version) {
            this.version = version;
            this.currentVersion = this.currentVersion + version;
        }    

        public ApiVersion(int version, String currentVersion) {
            this.version = version;
            this.currentVersion = currentVersion;
        }    

        public int getVersion() {
            return version;
        }    

        public void setVersion(int version) {
            this.version = version;
        }    

        public String getCurrentVersion() {
            return currentVersion;
        }    

        public void setCurrentVersion(String currentVersion) {
            this.currentVersion = currentVersion;
        }
    }
}

代码@ Github sample

感谢任何帮助,谢谢!

0 个答案:

没有答案