6

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

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

2)当我在请求中使用参数时,内容解析不正确。在访问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 示例

任何帮助表示赞赏,谢谢!

4

0 回答 0