FastJson简介:

  fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!在这之前jaskJson是命名为快速操作json的工具,而当阿里巴巴的fastJson诞生后jaskjson就消声匿迹了,不过目前很多项目还在使用。
 

本文目标:

  将fastJson加入到SpringBoot项目内,配置json返回视图使用fastJson解析。
 

一、项目搭建

  项目搭建目录及数据库

二、添加依赖(FastJson依赖)

  spring-boot-stater-tomcat依赖的scope属性一定要注释掉,我们才能在IntelliJ IDEA工具使用SpringBootApplication的形式运行项目!

  依赖地址:mvnrepository.com/artifact/com.alibaba/fastjson/1.2.31

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.dyh</groupId>
  12. <artifactId>lesson_three</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <packaging>war</packaging>
  15. <name>lesson_three</name>
  16. <description>Demo project for Spring Boot</description>
  17.  
  18. <properties>
  19. <java.version>1.8</java.version>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-data-jpa</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31.  
  32. <dependency>
  33. <groupId>mysql</groupId>
  34. <artifactId>mysql-connector-java</artifactId>
  35. <scope>runtime</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-tomcat</artifactId>
  40. <!--<scope>provided</scope>-->
  41. </dependency>
  42.  
  43. <dependency>
  44. <!--fastJson-->
  45. <groupId>com.alibaba</groupId>
  46. <artifactId>fastjson</artifactId>
  47. <version>1.2.51</version>
  48. </dependency>
  49.  
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-test</artifactId>
  53. <scope>test</scope>
  54. </dependency>
  55. </dependencies>
  56.  
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65.  
  66. </project>

三、配置文件(该项目用Spring Data JPA架构)

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
  4. driverClassName: com.mysql.cj.jdbc.Driver
  5. username: root
  6. password: root
  7.  
  8. jpa:
  9. database: MySQL
  10. show-sql: true
  11. hibernate:
  12. # naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
  13. # ddl-auto: create

四、创建一个FastJsonConfiguration配置信息类。

  添加@Configuration注解让SpringBoot自动加载类内的配置,有一点要注意我们继承了WebMvcConfigurerAdapter这个类,这个类是SpringBoot内部提供专门处理用户自行添加的配置,里面不仅仅包含了修改视图的过滤还有其他很多的方法,包括我们后面章节要讲到的拦截器,过滤器,Cors配置等。

  1. @Configuration
  2. public class FastJsonConfiguration extends WebMvcConfigurerAdapter
  3. {
  4. /**
  5. * 修改自定义消息转换器
  6. * @param converters 消息转换器列表
  7. */
  8. @Override
  9. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  10. //调用父类的配置
  11. super.configureMessageConverters(converters);
  12. //创建fastJson消息转换器
  13. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  14. //创建配置类
  15. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  16. //修改配置返回内容的过滤
  17. fastJsonConfig.setSerializerFeatures(
  18. SerializerFeature.DisableCircularReferenceDetect,
  19. SerializerFeature.WriteMapNullValue,
  20. SerializerFeature.WriteNullStringAsEmpty
  21. );
  22. fastConverter.setFastJsonConfig(fastJsonConfig);
  23. //将fastjson添加到视图消息转换器列表内
  24. converters.add(fastConverter);
  25. }
  26. }

FastJson SerializerFeatures

WriteNullListAsEmpty  :List字段如果为null,输出为[],而非null

WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非null
WriteMapNullValue:是否输出值为null的字段,默认为false。

五、测试,并比较用了FastJson与没有用之间的比较

使用前:

 

使用后:

name的值从NULL变成了"",那么证明我们的fastJson消息的转换配置完美生效了

springboot(五)使用FastJson返回Json视图的更多相关文章

  1. SpringBoot 03_利用FastJson返回Json数据

    自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...

  2. 小记SpringMVC与SpringBoot 的controller的返回json数据的不同

    近期由于项目的改动变更,在使用springmvc和springboot测试的时候发现一个有趣的现象 1.springmvc的controller使用@ResponseBody返回的仅仅是json格式的 ...

  3. fastjson 返回json字符串,JSON.parse 报错

    这是由于转义字符引起的如 : \ , fastjson 处理后是双反斜杠:\\ ,而 JSON.parse 解析时需要4个反斜杠 ,即 js解析json 反斜杠时,需要 4个 解成 1 个 解决方法: ...

  4. SpringBoot项目中处理返回json的null值

    在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...

  5. 如何搭建一个WEB服务器项目(五)—— Controller返回JSON字符串

    从服务器获取所需数据(JSON格式) 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验 ...

  6. SpringBoot 02_返回json数据

    在SpringBoot 01_HelloWorld的基础上来返回json的数据,现在前后端分离的情况下多数都是通过Json来进行交互,下面就来利用SpringBoot返回Json格式的数据. 1:新建 ...

  7. nutz的json视图

    2.3. json视图 返回json视图有两种方法: @Ok("json")  与@Ok(“raw:json”) 2.3.1. @Ok("json") (1) ...

  8. 2.SpringBoot之返回json数据

    一.创建一个springBoot个项目 操作详情参考:1.SpringBoo之Helloword 快速搭建一个web项目 二.编写实体类 /** * Created by CR7 on 2017-8- ...

  9. springboot使用fastJson作为json解析框架

    springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...

随机推荐

  1. Python3-datetime模块-日期与时间

    官方文档 http://python.usyiyi.cn/translate/python_352/library/datetime.html 代码示例 from datetime import da ...

  2. 洛谷 CF1012C Hills (动态规划)

    题目大意:有n个山丘 , 可以在山丘上建房子 , 建房子的要求是 : 该山丘的左右山丘严格的矮于该山丘 (如果有的话),你有一架挖掘机,每单位时间可以给一个山丘挖一个单位的高度,问你想要建造 1,2, ...

  3. egret.sys.TextNode

    class Test extends egret.Shape{ protected textNode:egret.sys.TextNode; constructor(){ this.width = t ...

  4. 《算法笔记》6.6小节 问题 A: 任务调度

    这道题我一开始看到的时候,想到的是拓补排序,可是这么菜又这么懒的我怎么可能用呢,既然出现在优先队列里面,那么久一定和他有关了 可是并没有使用优先队列 思路: 对于这道题,我们肯定是对他们定义优先级,然 ...

  5. SQL语法LPAD和RPAD

    一.[LPAD左侧补齐] LPAD(str,len,padstr) LPAD(str,len,padstr) 返回字符串 str, 其左边由字符串padstr 填补到len 字符长度.假如str 的长 ...

  6. Oracle 11gR2 待定的统计信息(Pending Statistic)

    Oracle 11gR2 待定的统计信息(Pending Statistic) 官档最权威: 发布优化器统计信息的用户界面 管理已发布和待处理的统计信息 实验先拖着.

  7. P1004 方格取数——奇怪的dp

    P1004 方格取数 题目描述 设有 \(N\times N\) 的方格图 \((N\leq 20)\),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 \(0\) .如下图所示(见样例) ...

  8. Mysql中Union和OR性能对比

    博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中Union和OR性能对比 在leetcode上看到一篇文章,整理一下 参考:https://leet ...

  9. git clone 别人的项目的步骤

    1.)从github上克隆出来 git clone +项目地址 2)切换到你需要的分支 git checkout +分支名称 3)下载到你的电脑上之后,在项目根目录中 npm install 安装所有 ...

  10. BUUCTF-Misc-No.1

    # BUUCTF-Misc # 签到 flag{buu_ctf} 金三胖 说实话直接看出来flag{he11ohongke} 二维码 直接binwalk扫一下,-e分离就出来一个带锁的zip爆破一下就 ...