項目使用自定義 FastJsonHttpMessageConverter 進行API數據響應JSON轉換器
在原來springboot1.X 版本中是可以生效,配置如下:
/** * 替換使用 FastJson 解析返回結果 */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { /** * 1.先定義一個convert轉換消息的對象 * 2.添加fastjson的配置信息,比如:是否要格式化返回的json數據 * 3.在convert中添加配置信息 * 4.將convert添加到converters當中 */ //1.先定義一個convert轉換消息的對象 FastJsonJsonpHttpMessageConverter fastConverter = new FastJsonJsonpHttpMessageConverter(); //2.添加fastjson的配置信息,比如:是否要格式化返回的json數據 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //處理中文亂碼問題(不然出現中文亂碼) List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); //3.在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //4.將convert添加到converters當中 converters.add(fastConverter); }
升級為springboot2后,響應 FastJsonHttpMessageConverter 未起作用,按對springmvc的了解 ,如果自定義不生效的話,應該是使用默認消息轉換器。為驗證想法,在配置中添加如下代碼打印出消息轉換器列表:
@Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter<?> messageConverter : converters) { System.out.println(messageConverter); //2 } }
輸出如下信息:
org.springframework.http.converter.ByteArrayHttpMessageConverter@433e3357 org.springframework.http.converter.StringHttpMessageConverter@4a1066a2 org.springframework.http.converter.StringHttpMessageConverter@37531849 org.springframework.http.converter.ResourceHttpMessageConverter@1cc4de0 org.springframework.http.converter.ResourceRegionHttpMessageConverter@54629d11 org.springframework.http.converter.xml.SourceHttpMessageConverter@66f4a841 org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@49ca7586 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@61a93e7c org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2a0a9212 com.dreamer.core.converter.FastJsonJsonpHttpMessageConverter@6a400857
可以看出自定義的消息轉換器【FastJsonJsonpHttpMessageConverter】在列表最后。
根據消息轉換器的應用規則,會順序選擇符合要求的消費轉換器,MappingJackson2HttpMessageConverter 在 FastJsonJsonpHttpMessageConverter 前面,這樣就會使用 MappingJackson2HttpMessageConverter 進行消費轉換,為了確認想法正確,我在 MappingJackson2HttpMessageConverter -> writeInternal 方法進行了debug,運行后,確實與想法一致。
找到原因,那就只要把 自定義的消息轉換器【FastJsonJsonpHttpMessageConverter】添加到 MappingJackson2HttpMessageConverter 前面就可以,而對於自定義的消息轉換器配置還有另一種方式,如下
@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { /** * 1.先定義一個convert轉換消息的對象 * 2.添加fastjson的配置信息,比如:是否要格式化返回的json數據 * 3.在convert中添加配置信息 * 4.將convert添加到converters當中 */ //1.先定義一個convert轉換消息的對象 FastJsonJsonpHttpMessageConverter fastConverter = new FastJsonJsonpHttpMessageConverter(); //2.添加fastjson的配置信息,比如:是否要格式化返回的json數據 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //處理中文亂碼問題(不然出現中文亂碼) List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); //3.在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastConverter); }
配置后,重啟項目,看到項目中消息轉換器列表打印如下內容:
com.dreamer.core.converter.FastJsonJsonpHttpMessageConverter@75652058
org.springframework.http.converter.ByteArrayHttpMessageConverter@30fa29ef
org.springframework.http.converter.StringHttpMessageConverter@7749f8a4
org.springframework.http.converter.ResourceHttpMessageConverter@5351785e
org.springframework.http.converter.ResourceRegionHttpMessageConverter@f6af58c
org.springframework.http.converter.xml.SourceHttpMessageConverter@6ebe10df
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@560a4974
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@e415863
這里就可以看出 自定義的消息轉換器【FastJsonJsonpHttpMessageConverter】在 MappingJackson2HttpMessageConverter 前面 ,同時運行API,debug 是運行 FastJsonJsonpHttpMessageConverter -> writeInternal 方法 。