十七、springboot配置FastJson为Spring Boot默认JSON解析框架

时间:2021-07-24 13:31:02

前提

  springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用

1.引入fastjson依赖库:

  maven:

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
</dependencies>

  gradle:

compile("com.alibaba:fastjson:${fastJsonVersion}")
ext {
fastJsonVersion = '1.2.46'
}

  注: 这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+

2.在启动类中配置

2.1配置方式一(通过继承的方式)

  1、启动类继承WebMvcConfigurerAdapter
  2、重写configureMessageConverters方法

@SpringBootApplication
@EnableDiscoveryClient
@EnableScheduling
public class MemberApplication extends WebMvcConfigurerAdapter {
/**
* 配置FastJson为方式一
* @return*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/*
* 1、需要先定义一个convert转换消息的对象 2、添加fastJson的配置信息,比如:是否要格式化返回json数据 3、在convert中添加配置信息
* 4、将convert添加到converters当中
*
*/
// 1、需要先定义一个·convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastjson的配置信息,比如 是否要格式化返回json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4、将convert添加到converters当中.
converters.add(fastConverter);
} public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
} }

  注:开发中为了统一管理配置,可以放入配置类中,启动类只做启动的功能

@Configuration
public class HttpConverterConfig { @Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
}

2.2配置方式二(通过@Bean注入的方式

  在App.java启动类中,注入Bean : HttpMessageConverters

@SpringBootApplication
@EnableDiscoveryClient
@EnableScheduling
public class MemberApplication {
/**
* 配置FastJson方式二
* @return HttpMessageConverters
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
} public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
} }

  在pojo类中:

    private int id;
private String name; //com.alibaba.fastjson.annotation.JSONField
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;//创建时间. /*
* serialize:是否需要序列化属性.
*/
@JSONField(serialize=false)
private String remarks;//备注信息.

那么这时候在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的话,那么恭喜你配置成功了,其中JSONField的包路径是:com.alibaba.fastjson.annotation.JSONField。

参考:

  http://www.cnblogs.com/xujie09/p/8461483.html

  https://blog.csdn.net/xuqingge/article/details/53561529

十七、springboot配置FastJson为Spring Boot默认JSON解析框架的更多相关文章

  1. 将SpringBoot默认Json解析框架jackson替换成fastjson

    步骤一:引入依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...

  2. Spring Boot默认的JSON解析框架设置

    方案一:启动类继承WebMvcConfigurerAdapter,覆盖方法configureMessageConverters ... @SpringBootApplication public cl ...

  3. Spring Boot默认日志logback配置解析

    前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  4. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  5. 一行配置搞定 Spring Boot项目的 log4j2 核弹漏洞!

    相信昨天,很多小伙伴都因为Log4j2的史诗级漏洞忙翻了吧? 看到群里还有小伙伴说公司里还特别建了800+人的群在处理... 好在很快就有了缓解措施和解决方案.同时,log4j2官方也是速度影响发布了 ...

  6. Spring boot 启动过程解析 logback

    使用 Spring Boot 默认的日志框架 Logback. 所有这些 POM 依赖的好处在于为开发 Spring 应用提供了一个良好的基础.Spring Boot 所选择的第三方库是经过考虑的,是 ...

  7. 20&period; Spring Boot 默认、自定义数据源 、配置多个数据源 jdbcTemplate操作DB

    Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari,这里主要研究下hikari的默认配置 0.  创建Spring Boot项目,选中 ...

  8. Spring Boot 2&period;X&lpar;十七&rpar;:应用监控之 Spring Boot Admin 使用及配置

    Admin 简介 Spring Boot Admin 是 Spring Boot 应用程序运行状态监控和管理的后台界面.最新UI使用vue.js重写里. Spring Boot Admin 为已注册的 ...

  9. SpringBoot学习笔记&lpar;2&rpar; Spring Boot的一些配置

    外部配置 Spring Boot允许使用properties文件.yaml文件或者命令行参数作为外部配置 使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Enviro ...

随机推荐

  1. 相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了!

    相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了! 先说图片X×dpi=点数dotX是图片实际尺寸,简单点,我们只算图片的高吧,比如说拍了张图片14 ...

  2. Commons-Collections 集合工具类的使用

    package com.bjsxt.others.commons; import java.util.ArrayList; import java.util.List; import org.apac ...

  3. &lbrack;DNX&rsqb;解决dnu restore时找不到Newtonsoft&period;Json的问题

    在Mac上用最新版的dnx 1.0.0-beta5-11855进行dnu restore,出现下面的错误: System.IO.FileNotFoundException: Could not loa ...

  4. hdu 4622 Reincarnation trie树&plus;树状数组&sol;dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  5. Windows、Ubuntu双系统重装windows系统后grub引导的修复及默认启动项的修改

    今天帮童鞋重装系统,他的电脑是Windows.Ubuntu双系统,需要重装的系统是windows,据说是因为很多游戏都只支持64位,要给换成64位的 = =...      于是我就帮他装了个wind ...

  6. Oracle通过主键id删除记录很慢

    问题描述: Oracle通过主键id删除2000条记录很慢,需要花费十二分钟. 解决过程: 1.首先查看SQL的执行计划,执行计划正常,cost只有4,用到了主键索引. 2.查看等待事件, selec ...

  7. Hibernate 查询:HQL查询&lpar;Hibernate Query Languge&rpar;

    HQL是一种面向对象的查询语言,其中没有表和字段的概念,只有类,对象和属性的概念. 使用HQL查询所有学生: public static void main(String[] args) { Sess ...

  8. easyui判断下拉列表

    {field:'state',title:'状态',width:100, formatter : function(value, row, index){ if (value == 0) { retu ...

  9. Jquery如何获取iframe里面body的html呢?

    如果是自己网页的话,可以这样,$("iframe").contents().find("body").html();意思是,获取iframe里面页面body的内 ...

  10. Python &plus; Selenium &plus; AutoIt 模拟键盘实现另存为、上传、下载操作详解

    前言 在web页面中,可以使用selenium的定位方式来识别元素,从而来实现页面中的自动化,但对于页面中弹出的文件选择框,selenium就实现不了了,所以就需引用AutoIt工具来实现. Auto ...