前提

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

1.引入fastjson依赖库:

  maven:

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.46</version>
  6. </dependency>
  7. </dependencies>

  gradle:

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

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

2.在启动类中配置

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

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

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableScheduling
  4. public class MemberApplication extends WebMvcConfigurerAdapter {
  5. /**
  6. * 配置FastJson为方式一
  7. * @return*/
  8. @Override
  9. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  10. super.configureMessageConverters(converters);
  11. /*
  12. * 1、需要先定义一个convert转换消息的对象 2、添加fastJson的配置信息,比如:是否要格式化返回json数据 3、在convert中添加配置信息
  13. * 4、将convert添加到converters当中
  14. *
  15. */
  16. // 1、需要先定义一个·convert转换消息的对象;
  17. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  18. // 2、添加fastjson的配置信息,比如 是否要格式化返回json数据
  19. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  20. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  21. // 3、在convert中添加配置信息.
  22. fastConverter.setFastJsonConfig(fastJsonConfig);
  23. // 4、将convert添加到converters当中.
  24. converters.add(fastConverter);
  25. }
  26.  
  27. public static void main(String[] args) {
  28. SpringApplication.run(MemberApplication.class, args);
  29. }
  30.  
  31. }

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

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

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

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

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableScheduling
  4. public class MemberApplication {
  5. /**
  6. * 配置FastJson方式二
  7. * @return HttpMessageConverters
  8. */
  9. @Bean
  10. public HttpMessageConverters fastJsonHttpMessageConverters() {
  11. // 1.定义一个converters转换消息的对象
  12. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  13. // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
  14. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  15. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  16. // 3.在converter中添加配置信息
  17. fastConverter.setFastJsonConfig(fastJsonConfig);
  18. // 4.将converter赋值给HttpMessageConverter
  19. HttpMessageConverter<?> converter = fastConverter;
  20. // 5.返回HttpMessageConverters对象
  21. return new HttpMessageConverters(converter);
  22. }
  23.  
  24. public static void main(String[] args) {
  25. SpringApplication.run(MemberApplication.class, args);
  26. }
  27.  
  28. }

  在pojo类中:

  1. private int id;
  2. private String name;
  3.  
  4. //com.alibaba.fastjson.annotation.JSONField
  5. @JSONField(format="yyyy-MM-dd HH:mm")
  6. private Date createTime;//创建时间.
  7.  
  8. /*
  9. * serialize:是否需要序列化属性.
  10. */
  11. @JSONField(serialize=false)
  12. 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. Spring Boot 默认、自定义数据源 、配置多个数据源 jdbcTemplate操作DB

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

  8. Spring Boot 2.X(十七):应用监控之 Spring Boot Admin 使用及配置

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

  9. SpringBoot学习笔记(2) Spring Boot的一些配置

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

随机推荐

  1. UOJ #7 【NOI2014】 购票

    题目链接:购票 这道题我调了好久啊……主要还是因为这种用\(CDQ\)分治来搞斜率优化的题已经很久没写过了……上一次要追溯到去年暑假去了…… 看下面这些东西之前你需要先自己推出斜率优化的式子…… 这道 ...

  2. 破解CobaltStrike3.12(转)

      0x00  概述 CobaltStrike是一款内网渗透的商业远控软件,支持自定义脚本扩展,功能非常强大.前段时间Github上有好心人放出了CobaltStrike3.12的试用版,接着Lz1y ...

  3. Map / HashMap 获取Key值的方法

    方法1:keySet()HashMap hashmp = ne HashMap();hashmp.put("aa", "111");Set set = hash ...

  4. ImageView的android:scaleType各属性含义(zz)

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size.ImageView.ScaleType / android:scaleType值的意义区别 ...

  5. dingo 内部调用获取异常信息

    $exception->getResponse(); $params = [ 'company_id' => $this->request->input('company_id ...

  6. NATS_01:NATS基础介绍

    1.介绍 NATS(Message bus): 从CloudFoundry的总架构图看,位于各模块中心位置的是一个叫nats的组件.NATS是由CloudFoundry的架构师Derek开发的一个开源 ...

  7. 关于dubbo的架构

    dubbo是国内一个十分受欢迎的分布式rpc框架. 这篇博客是从dubbo官网出发,来说明下dubbo的技术架构.首先我们看下官网的架构图. 节点角色说明: Provider: 暴露服务的服务提供方. ...

  8. 科学计算三维可视化---Mayavi入门(Mayavi介绍和安装)

    Mayavi介绍 是基于VTK开发的可视化软件(更加高效),Mayavi完全由python编写,方便使用,而且可以使用python编写扩展,嵌入到用户程序中 安装要求 VTK >pip3 ins ...

  9. poj 2125 Destroying The Graph (最小点权覆盖)

    Destroying The Graph http://poj.org/problem?id=2125 Time Limit: 2000MS   Memory Limit: 65536K       ...

  10. Linux系统自动备份的Shell

    公司现在需要对现有的服务器进行定期备份,并将备份文件放置到正在使用的NAS中去: 为了备份的效率,还需要对备份的文件进行筛选,排除一些后缀名的文件: 实现方法如下: 1. 编写备份的shell文件 在 ...