spring boot2 配置 FastJsonHttpMessageConverter 不起作用
專案使用自定義 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 } }
輸出如下資訊:
o[email protected]433e3357
[email protected]1066a2
[email protected]531849
[email protected]1cc4de0
org.sp[email protected]54629d11
or[email protected]66f4a841
org.springframework[email protected]49ca7586
org.springfr[email protected]61a93e7c
org.springfr [email protected]2a0a9212
[email protected]00857
可以看出自定義的訊息轉換器【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);
}
配置後,重啟專案,看到專案中訊息轉換器列表列印如下內容:
[email protected]52058
o[email protected]30fa29ef
[email protected]49f8a4
[email protected]5351785e
org.sp[email protected]f6af58c
or[email protected]6ebe10df
org.springframework[email protected]560a4974
org.springfr[email protected]e415863
這裡就可以看出 自定義的訊息轉換器【FastJsonJsonpHttpMessageConverter】在 MappingJackson2HttpMessageConverter 前面 ,同時執行API,debug 是執行 FastJsonJsonpHttpMessageConverter -> writeInternal 方法 。
Good Bye