springboot 中,使用fastjson替换jackson ,接口返回出现乱码,解决方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Bean public HttpMessageConverters fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes);
HttpMessageConverter<?> converter = fastConverter ;
return new HttpMessageConverters(converter) ; }
|
如果这样不能解决问题,再试下如下方法:
在 application.properties 文件中新增 :
1 2 3 4
| spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
|
我的是这样解决的。