SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题
在入口类里面进行配置,入口类继承WebMvcConfigurerAdapter并重写configureMessageConverters方法,在方法里继承并修改fastjson的编码格式
package com.ljun;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.需要先定義一個convert轉換信息對象
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
//2.添加fastJson配置信息,比如是否要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
//3.在convert中添加配置信息
fastJsonConverter.setFastJsonConfig(fastJsonConfig);
//4.将converter添加到converters中
converters.add(fastJsonConverter);
}
/**
* 使用@Bean注入fastJsonMessageConverter
* @return
*/
// @Bean
// public HttpMessageConverter fastJsonMessageConverters() {
// //1.需要先定義一個convert轉換信息對象
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// //2.添加fastJson配置信息,比如是否要格式化返回的json数据
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// //3.在convert中添加配置信息
// fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// //4.将converter添加到converters中
// HttpMessageConverter httpMessageConverter = fastJsonHttpMessageConverter;
// return httpMessageConverter;
// }
public static void main( String[] args ){
SpringApplication.run(App.class, args);
}
}
原地址https://www.cnblogs.com/xql4j/p/6729524.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-01-21 2016年Esri技术公开课全年资料分享
2017-01-21 二次封装arcgis的timeslider
2017-01-21 二次封装dojo slider
2016-01-21 使用JFreeChart实现基于Web的柱状图
2016-01-21 How to display values with in piechart using Jfreechart(values in sector )
2016-01-21 JFreeChart框架中生成饼状图上怎样显示数据 [问题点数:40分,结帖人GreenLawn]