笔者的JSON如下:

{
"code": 10001,
"message": "成功",
"nextUrl": null,
"data": {
"updateTime": "2020-02-23 13:43:18",
"result": [
{
"confirm": 24,
"suspect": 0,
"dead": 0,
"heal": 17,
"weight": 18.8,
"mapId": null,
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 19,
"suspect": 0,
"dead": 0,
"heal": 14,
"weight": 15.1,
"mapId": "c320500_2",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 10,
"suspect": 0,
"dead": 0,
"heal": 7,
"weight": 7.8,
"mapId": "c320500_4",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 8,
"suspect": 0,
"dead": 0,
"heal": 6,
"weight": 6.4,
"mapId": "c320500_7",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 6,
"suspect": 0,
"dead": 0,
"heal": 3,
"weight": 4.2,
"mapId": "c320500_8",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 6,
"suspect": 0,
"dead": 0,
"heal": 2,
"weight": 3.8,
"mapId": null,
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 5,
"suspect": 0,
"dead": 0,
"heal": 4,
"weight": 4.1,
"mapId": "c320500_5",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 4,
"suspect": 0,
"dead": 0,
"heal": 3,
"weight": 3.2,
"mapId": "c320500_0",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 3,
"suspect": 0,
"dead": 0,
"heal": 1,
"weight": 1.9,
"mapId": "c320500_3",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 2,
"suspect": 0,
"dead": 0,
"heal": 0,
"weight": 1,
"mapId": "c320500_6",
"updateTime": "2020-02-25 18:17:14"
},
{
"confirm": 0,
"suspect": 0,
"dead": 0,
"heal": 2,
"weight": 0.8,
"mapId": null,
"updateTime": "2020-02-26 11:56:04"
}
]
}
}

里面有mapId这个字段,多处都为null,在大json考虑网络传输速度的情况下,这些null字段是多余的。

springmvc/springBoot中json框架默认使用Jackson,我们就可以通过Jackson相关注解来过滤字段为null的字段。

只要加

@JsonInclude(JsonInclude.Include.NON_EMPTY)

对应java类Snapshot.java

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.base.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType; import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient; @Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "snapshot")
public class SnapShot { @JsonIgnore
@Id
@GeneratedValue(generator = "JDBC")
private Integer id; @JsonIgnore
private Integer distId; private Integer confirm; private Integer suspect; private Integer dead; @JsonIgnore
private String deadRate; private Integer heal;
@ColumnType(column = "`weight`",jdbcType = JdbcType.FLOAT)
private Float weight;
@JsonIgnore
private Float riskLevel = 0.0f; @JsonIgnore
private String healRate; @JsonIgnore
@Transient
//@ColumnType(column = "`level`",jdbcType = JdbcType.VARCHAR)
private String level; @JsonInclude(JsonInclude.Include.NON_EMPTY)
private String mapId; private String updateTime; @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SnapShot snapShot = (SnapShot) o;
return Objects.equal(distId, snapShot.distId) &&
Objects.equal(confirm, snapShot.confirm) &&
Objects.equal(suspect, snapShot.suspect) &&
("area".equals(level)?
Objects.equal(suspect, snapShot.suspect):
true) &&
Objects.equal(dead, snapShot.dead) &&
Objects.equal(heal, snapShot.heal);
} @Override
public int hashCode() {
return 0;
}
}

设置后效果如下:

转载自:https://www.cnblogs.com/yangy608/p/3936848.html

SrpingMVC/SpringBoot中restful接口序列化json的时候使用Jackson将空字段,空字符串不传递给前端的更多相关文章

  1. 【快学springboot】2.Restful简介,SpringBoot构建Restful接口

    Restful简介 Restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现 ...

  2. [2019/05/17]解决springboot测试List接口时JSON传参异常

    报错信息,大致如下 c.c.c.c.a.BaseControllerExceptionHandler : 运行时异常: java.lang.IllegalStateException: No prim ...

  3. 基于springboot的restful接口的单元测试示例

    一.知识点 代码中对应的知识点 1.jsonPath github网址 1)操作符见文档 2)方法见文档 3)例子见文档 2.MockMvc(org.springframework.test.web. ...

  4. [二十七]SpringBoot 之 Restful接口的跨域请求

    什么是跨域 简单的说即为浏览器限制访问A站点下的js代码对B站点下的url进行ajax请求.比如说,前端域名是www.abc.com,那么在当前环境中运行的js代码,出于安全考虑,访问www.xyz. ...

  5. SpringBoot中配置不序列化返回值为null的属性

    package com.weiresearch.properties; import com.fasterxml.jackson.annotation.JsonInclude;import com.f ...

  6. Springboot中WebMvcConfigurer接口详解

    Springboot 使用越来越多,企业的基本框架,到Springcloud分布式,可以说无论面试还是平常技术学习,一说到spring几乎就就代替了Java,可以说spring,springboot的 ...

  7. 聊一聊 Spring Boot 中 RESTful 接口设计规范

    在设计接口时,有很多因素要考虑,如接口的业务定位,接口的安全性,接口的可扩展性.接口的稳定性.接口的跨域性.接口的协议规则.接口的路径规则.接口单一原则.接口过滤和接口组合等诸多因素,本篇文章将简要分 ...

  8. 无规矩不成方圆,聊一聊 Spring Boot 中 RESTful 接口设计规范

    在设计接口时,有很多因素要考虑,如接口的业务定位,接口的安全性,接口的可扩展性.接口的稳定性.接口的跨域性.接口的协议规则.接口的路径规则.接口单一原则.接口过滤和接口组合等诸多因素,本篇文章将简要分 ...

  9. [JAVA]SpringBoot中让接口支持跨域

    官方原文:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework ===抽空翻译 最简单办法:在方法上增加注解: @Cro ...

随机推荐

  1. selenium 启动、窗口、获取标题

    1. from selenium import webdriver #启动chrom浏览器,没写executable_path,这是因为配置环境时,已经将chromdriver放到python安装文件 ...

  2. CCF 201703-4 地铁修建(最小生成树)

    题意:A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁.地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之 ...

  3. Java之反射 — 用法及原理

    Java之反射 - 用法及原理 定义 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象 ...

  4. 027、MySQL字符串替换函数,文本替换函数,字符串填充函数

    #文本填充 ,'); #ABC12121212121212121 #文本替换 SELECT REPLACE('田攀520','攀','ABC'); #田ABC520 不忘初心,如果您认为这篇文章有价值 ...

  5. 002.Delphi插件之QPlugins,菜单插件

    运行之后的效果如下, 图一 图二 主界面代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages, System.SysUt ...

  6. leetcode1 twoSum

    """ Given an array of integers, return indices of the two numbers such that they add ...

  7. Java线程池 ThreadPoolExecutor类

    什么是线程池? java线程池是将大量的线程集中管理的类, 包括对线程的创建, 资源的管理, 线程生命周期的管理. 当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程, 从而减少系 ...

  8. Win10下数据增强及标注工具安装

    Win10下数据增强及标注工具安装 一.   数据增强利器—Augmentor 1.安装 只需在控制台输入:pip install Augmentor 2.简介 Augmentor是用于图像增强的软件 ...

  9. 开源免费的安卓投屏工具-Scrcpy

    最近需要使用安卓投屏在桌面上操作,一开始使用Vysor,免费版画质无法直视,发现一个开源的工具,Scrcpy,貌似效果不错,但没有GUI,命令行安装,整起(Mac) 1.安装 homebrew: 通过 ...

  10. 在excel表格里,为所有数字添上绿色小三角

    在excel表格里,为所有数字添上绿色小三角的方法有4种: 1. 为一个单元格添加:直接在单元格里添加一个英文的逗号 2. 为一列数据添加:选中要添加绿色小三角的列,选择 数据-->分列--&g ...