FastJsonHttpMessageConverter 返回json字串有轉義符
阿新 • • 發佈:2019-01-11
@Configuration public class WebConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry interceptorRegistry) { interceptorRegistry.addInterceptor(new SignInterceptor()).addPathPatterns("/v1/**"); } public HttpMessageConverter<String> stringConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8")); return converter; } public HttpMessageConverter fastConverter() { //1、定義一個convert轉換訊息的物件 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、新增fastjson的配置資訊 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonConfig.setCharset(Charset.forName("UTF-8")); //2-1 處理中文亂碼問題 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); //3、在convert中新增配置資訊 fastConverter.setFastJsonConfig(fastJsonConfig); return fastConverter; } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.clear(); converters.add(stringConverter()); converters.add(fastConverter()); } }
問題原因是FastJsonHttpMessageConverter 前面必須有一個StringHttpMessageConverter,converters裡可以檢視具體的順序