SpringBoot REST 項目, 默認返回類型爲 JSON. 可是爲了兼容老項目調用默認返回XML, 因此須要設置默認類型爲XML.html
默認設置爲XML之後, 客戶端依然能夠設置 http header, 的 Accept
爲 application/json
, SpringBoot 將會正常返回 JSON 數據.git
設置方法以下:github
/** * 將默認 content-type 設置爲 XML <br/> * * 參考: <br/> * * http://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/content-negotiation-default-media-type/ * <br/> * * https://stackoverflow.com/questions/33009918/spring-boot-controller-content-negotiation */ @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.defaultContentType(MediaType.TEXT_XML); } // 下面解決因在SpringBoot項目中使用 @EnableWebMvc 註解, 而沒法使用 swagger-ui 的問題 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** * Setup Swagger UI <br/> * refer: https://github.com/springfox/springfox/issues/1427 */ registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/images"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
SpringBoot 要支持 XML 序列化須要引入 Jackson 的 XML 支持, 參考: http://www.javashuo.com/article/p-bohltosn-ht.htmlweb
同時使用swagger注意, 在 SpringBoot 項目中, 若是使用
@EnableWebMvc
註解, 會影響 SpringBoot 的一些加載, 而致使 swagger ui 沒法使用. 因此上面添加了相應的 ResourceHandler 去解決這個問題.spring