SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题
2017-11-18 16:39
736 查看
在入口类里面进行配置,入口类继承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
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
相关文章推荐
- 解决Springboot使用FastJson 返回中文乱码的问题
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- 分享知识-快乐自己:spring_Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson
- SpringBoot+FastJson 中文乱码问题
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- (二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
- 解决SpringBoot框架下利用jdbcTemplate插入mysql数据库中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- SpringMVC与fastjson整合并同时解决中文乱码问题
- Springboot使用FastJson后,接口返回中文乱码的问题解决(两种解决方式)
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- 解决Spring 3.0 json返回中文乱码问题
- SpringMVC与fastjson整合并同时解决中文乱码问题
- spring boot 使用fastjson 处理json数据中文乱码 默认使用 ISO-8859-1编码格式
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot使用FastJson后,接口返回中文乱码的问题解决。