首先引入fastjson的maven依赖:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>

  然后添加一个配置类:

package com.boot.demo;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import javafx.scene.chart.Chart;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; @Configuration
public class FastJsonConfiguration implements WebMvcConfigurer { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { /**
* 先移除MappingJackson2HttpMessageConverter转换器,再加入fastjson转换器
*/
Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
while(iterator.hasNext()){
HttpMessageConverter<?> converter = iterator.next();
if(converter instanceof MappingJackson2HttpMessageConverter){
iterator.remove();
}
}
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue
,SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
fastJsonHttpMessageConverter.setFastJsonConfig(config); //解决返回的json数据中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); //将转换器添加到转换列表
converters.add(fastJsonHttpMessageConverter); } }

spring boot 2.0添加对fastjson的支持的更多相关文章

  1. spring boot 2.0(二)动态banner的支持

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  2. Spring Boot 2.0 升级指南

    Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...

  3. springboot2.0(一):【重磅】Spring Boot 2.0权威发布

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  4. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  5. Spring Boot 2.0 教程 | AOP 切面统一打印请求日志

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ...

  6. 【重磅】Spring Boot 2.0权威发布

    新版本特性 新版本值得关注的亮点有哪些: 基于 Java 8,支持 Java 9 也就是说Spring Boot2.0的最低版本要求为JDK8,据了解国内大部分的互联网公司系统都还跑在JDK1.6/7 ...

  7. (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布

    http://www.ityouknow.com/springboot/2018/03/01/spring-boot-2.0.html 就在今天Spring Boot2.0.0.RELEASE正式发布 ...

  8. Spring Boot 2(一):Spring Boot 2.0新特性

    Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...

  9. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

随机推荐

  1. 利用Python读取文件名并生成txt文件——以图片文件为例

    效果如下: 代码: import os class ReadImageName(): def __init__(self): self.path = '.' def readname(self): f ...

  2. Linux下lvm在线扩容步骤

    转 :https://jingyan.baidu.com/article/25648fc18f22b29191fd0011.html 图片看不清可以看原文 LVM是逻辑盘卷管理(Logical Vol ...

  3. JAVA方法传递参数:传值?传引用?

    先来看下面这三段代码: //Example1: public class Example1 { static void check(int a) { a++; } public static void ...

  4. Atitit. Derby的使用总结attilax

    Atitit. Derby的使用总结attilax 1. Derby数据库的存储结构1 2. Derby gui工具1 3. 代码读写derby2 4. 与sqlite db4o的对比4 5. 参考5 ...

  5. mock带参数的构造函数

    @RunWith(PowerMockRunner.class)@PrepareForTest(Helper.class)//1.添加要初始化的类,就是构造函数所在的类public class Help ...

  6. Android 四大组件学习之Service六

    上几节.我们学习怎样用StartServer启动一个服务,用bindServer去绑定一个服务.以及服务的生命周期,以及什么是IntentService. 也许有读者会发现,我们BindServer中 ...

  7. 配置Redmine的邮件通知功能

    依据<Windows下安装Redmine 2.5.2不全然指南 >一文,我们搭建主要的 Redmine 平台.如今是时候做进一步的配置了. 作为一个项目管理平台,必须能够通知项目成员有关项 ...

  8. mysql存储引擎的种类与差别(innodb与myisam)

    查找数据库的存数引擎: show engines show variables like '%storage_engine%' 更改数据库的引擎更改配置文件/etc/my.cnf 改动default- ...

  9. Crontab使用方式

    Liunx系统的定时任务需要Crontab来完成 一.添加 添加定时脚本 crontab -e 或者直接编辑/etc/crontab文件进行任务添加 vim /etc/crontab 二.格式 三.举 ...

  10. ORA-06519: 检测到活动的自治事务处理,已经回退

    写了一个函数,由于在定义时加入了 create or replace function F_计算结果(In_参数 varchar2) return number is --使用自治事务PRAGMA A ...