Springboot使用FastJson后,接口返回中文乱码的问题解决。
来源:互联网 发布:cctv2郑州淘宝诈骗 编辑:程序博客网 时间:2024/06/05 23:03
哎,天下文章一大抄,到处都是一模一样的教你怎么替换掉jackson成fastjson的,可后续中文乱码网上居然没一篇文章。翻了一会源码还是写个文章共享下吧。免得后来人又浪费时间折腾。
在springboot中,就一步就把替换和乱码问题解决了,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public
class
WebMvcConfig
extends
WebMvcConfigurerAdapter {
/**
* 利用fastjson替换掉jackson,且解决中文乱码问题
* @param converters
*/
@Override
public
void
configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastConverter =
new
FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig =
new
FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//处理中文乱码问题
List<MediaType> fastMediaTypes =
new
ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
搞定了。还是推荐用configureMessageConverters这种配置的模式去加载,而不是网上那种直接覆写HttpMessageConverter加@Bean注解的方式更好吧。
本文地址:http://www.cnblogs.com/xql4j/p/6729524.html,请别乱复制了。
阅读全文
0 1
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- Springboot使用FastJson后,接口返回中文乱码的问题解决(两种解决方式)
- 解决Springboot使用FastJson 返回中文乱码的问题
- Springboot使用FastJson中文乱码解决方法。
- SpringBoot+FastJson 中文乱码问题
- ajax返回中文时乱码的问题解决
- SpringMVC的@ResponseBody返回中文乱码问题解决
- 前台ajax传值到controller后,返回值中文乱码问题解决
- ajax返回中文乱码问题解决
- SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题
- ajax返回乱码后的中文乱码使用eval函数解决方案
- SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 安装完wampserver后使用mysql命令行中文乱码问题解决
- paython接口返回中文乱码
- springboot 使用自己的fastJson框架
- httpclient返回值中文乱码问题解决方法
- springMVC3返回JSON中文乱码问题解决
- Android自定义view 实现半圆环
- Linux Deepin
- Kotlin简单使用
- 第3节---爬取知乎‘美女’
- JS初级基础
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- MyBatis动态Sql
- oracle function函数
- web前端到底怎么学?干货资料!
- Electron 打包时下载 xxx-electron-v1.6.8-<arch>-x64.zip 文件出错
- awk使用
- 三分天下如何选择vue,angular,react
- 设计模式练习--工厂模式
- POJ