处理springboot使用fastJson浏览器调用接口正常返回数据却中文乱码的问题
这属于fastJson的一个bug只需要像下面这样操作就可以了
@Bean//使用Bean入fast JsonHt tpllessageConvert
public HttpMessageConverter fastJsonHttpMessageConverters() {
//需婴定义一个Convert转换
FastJsonHttpMessageConverter fastConverter =new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
// 处理中文乱码问题
List fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
SerializeConfig.globalInstance.put(Long.class, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(SerializeConfig.globalInstance);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter converter = fastConverter;
return converter;
}
@Override
public void configureMessageConverters(List<httpmessageconverter<?>> converters){
converters.add(fastJsonHttpMessageConverters());
}
另外的一种方式是在配置文件中加入
server:
servlet:
encoding:
force-response: true
标签:fastJson,springboot,fastConverter,乱码,new,fastJsonConfig
From: https://www.cnblogs.com/luo9tian/p/18391053