SpringBoot 添加fastjson
Posted 思微随想
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 添加fastjson相关的知识,希望对你有一定的参考价值。
1.先在项目中添加fastjson依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency>
2.1第一种方法,让入口类实现WebMvcConfigure接口,并重写configureMessageConverters方法:
public class App implements WebMvcConfigurer{ ...... }
重写方法:
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //第一种配置fastJson方法 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastJsonHttpMessageConverter); }
2.2第二种方法,直接在入口类加入@Bean注解并加入方法:
@Bean public HttpMessageConverters fastJsonHttpMessage() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); }
以上是关于SpringBoot 添加fastjson的主要内容,如果未能解决你的问题,请参考以下文章