javascript
SpringBoot项目 整合 JacksonXml
一、需求場景:
有個需求場景:同一個請求路徑,入參類型分別有“JSON格式”和“XML格式”的入參,需要根據入參類型的不同,處理不同的業務場景,然后返回對應格式的返回。值。比如:
? JSON格式入參,返回JSON格式的返回值。
? XML格式入參,返回XML格式的返回值。
印象中是可以區分的,然后網上搜索了資料完善了解決方案,好記性不如爛筆頭,現在梳理一下如下:
二、解決方案
第一種方案:SpringMvc 自定義 XML的 HttpMessageConverter 轉換器。
自定義轉換器的方式本身沒有問題,自己寫一個 繼承 ObjectMapper 類的xmlMapper就能實現。一開始我是用的這個,后來發現這樣寫很多余,因為 ObjectMapper 我是引用了"com.fasterxml.jackson.dataformat.xml.XmlMapper" ,實際上完全可以不用自己寫,原因是 :如果引入了"com.fasterxml.jackson.dataformat.xml.XmlMapper"依賴,SpringMVC會自動加載XML轉換器,優先級是XML優先。更優的辦法詳見第二種方式。
import java.util.List;import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import com.fasterxml.jackson.dataformat.xml.XmlMapper;@Configuration public class WebConfiguration implements WebMvcConfigurer {/*** 新增自定義轉換器* @param converters {@link List<HttpMessageConverter<?>>}*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2XmlHttpMessageConverter xmlConverter = new MappingJackson2XmlHttpMessageConverter();XmlMapper xmlMapper = new XmlMapper();xmlConverter.setObjectMapper(xmlMapper);//新增 xml轉換器,設置順序為0converters.add(0,xmlConverter);} }第二種方案:直接引入XML 轉換器的依賴
SpringBoot項目,可以在 Pom.xml 中直接引入**" jackson-dataformat-xml"** 依賴。
<!-- Springboot默認使用json格式解析參數,如果需要支持xml入參,則需要引入 'jackson-dataformat-xml'依賴 --> <dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><!-- 注意版本應該與 Springboot版本匹配。看SpringBoot版本發布日期去Maven找對應的版本號 --><version>${jackson-dataformat-xml.version}</version> </dependency>定義同一個名為"/test"的請求,通過指定“consumes” 和“produces”屬性區分JSON格式和XML格式的控制,注意方法名要不一樣。
/*** 示例:* 接收JSON格式的入參,并返回JSON格式的返回值* consumes:指入參的 MediaType類型,這里設置為:接收UTF-8的JSON格式,* produces:指返回值的MediaType類型,這里設置為:返回UTF-8的JSON格式。*/@PostMapping(value = "/test",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)public JSONObject Json(@RequestBody JSONObject requestJsonObject) {//業務代碼,略}/*** 示例:* 接收XML格式的入參,并返回XML格式的返回值* consumes:指入參的 MediaType類型,這里設置為:接收XML格式,* produces:指返回值的MediaType類型,這里設置為:返回XML格式。*/@PostMapping(value = "/test",consumes = MediaType.APPLICATION_XML_VALUE,produces = MediaType.APPLICATION_XML_VALUE)public JSONObject Xml(@RequestBody JSONObject requestJsonObject) {//業務代碼,略}三、源碼分析:SpringMVC ,默認的設置的 MediaType的順序
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {private static final boolean romePresent =ClassUtils.isPresent("com.rometools.rome.feed.WireFeed",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jaxb2Present =ClassUtils.isPresent("javax.xml.bind.Binder",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jackson2Present =ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",WebMvcConfigurationSupport.class.getClassLoader()) &&ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jackson2XmlPresent =ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jackson2SmilePresent =ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jackson2CborPresent =ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean gsonPresent =ClassUtils.isPresent("com.google.gson.Gson",WebMvcConfigurationSupport.class.getClassLoader());private static final boolean jsonbPresent =ClassUtils.isPresent("javax.json.bind.Jsonb",WebMvcConfigurationSupport.class.getClassLoader());protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316messageConverters.add(new ByteArrayHttpMessageConverter());messageConverters.add(stringHttpMessageConverter);messageConverters.add(new ResourceHttpMessageConverter());messageConverters.add(new ResourceRegionHttpMessageConverter());messageConverters.add(new SourceHttpMessageConverter<>());messageConverters.add(new AllEncompassingFormHttpMessageConverter());if (romePresent) {messageConverters.add(new AtomFeedHttpMessageConverter());messageConverters.add(new RssChannelHttpMessageConverter());}if (jackson2XmlPresent) {Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();if (this.applicationContext != null) {builder.applicationContext(this.applicationContext);}// 新增 XML 轉換器messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));}else if (jaxb2Present) {messageConverters.add(new Jaxb2RootElementHttpMessageConverter());}if (jackson2Present) {Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();if (this.applicationContext != null) {builder.applicationContext(this.applicationContext);}// 新增 JSON 轉換器messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));}else if (gsonPresent) {// 新增 Gson 轉換器messageConverters.add(new GsonHttpMessageConverter());}else if (jsonbPresent) {// 新增 Jsonb 轉換器messageConverters.add(new JsonbHttpMessageConverter());}if (jackson2SmilePresent) {Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();if (this.applicationContext != null) {builder.applicationContext(this.applicationContext);}//新增 Smile 轉換器messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));}if (jackson2CborPresent) {Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();if (this.applicationContext != null) {builder.applicationContext(this.applicationContext);}//新增 Cbor 轉換器messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));}} }從源碼看,導入了對應的依賴,比如 “jackson-dataformat-xml” ,依賴包會有 "com.fasterxml.jackson.dataformat.xml.XmlMapper"類被SpringMVC找到,
然后就會自動加到 messageConverters 轉換器中,順序就是代碼add的順序, XML > Json > Gson > Jsonb > Smile > Cbor
參考網站
總結
以上是生活随笔為你收集整理的SpringBoot项目 整合 JacksonXml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gulp压缩css文件夹,使用 gulp
- 下一篇: expect java_expect命令