笔者的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. hadoop 第一个 mapreduce 程序(对MapReduce的几种固定代码的理解)

    1.2MapReduce 和 HDFS 是如何工作的 MapReduce 其实是两部分,先是 Map 过程,然后是 Reduce 过程.从词频计算来说,假设某个文件块里的一行文字是”Thisis a ...

  2. ubuntu安装discourse论坛----结合在apache服务上建立虚拟主机

    指导操作:https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md 一.先安装 Docker / Git: wg ...

  3. VMware Tools 组件、配置选项和安全要求

  4. spring-boot-autoconfigure-xx.jar核心注解

  5. 基于线程池、消息队列和epoll模型实现并发服务器架构

    引言 并发是什么?企业在进行产品开发过程中为什么需要考虑这个问题?想象一下天猫的双11和京东的618活动,一秒的点击量就有几十万甚至上百万,这么多请求一下子涌入到服务器,服务器需要对这么多的请求逐个进 ...

  6. 043、Java中逻辑运算之实现位与操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. 031.SAP上查看所有的用户账号,查询SAP用户账号的后台数据库表

    01. 输入事务代码SU11, 然后输入SAP用户账号数据表USER_ADDR 02. 点击实用程序,再点击内容 03.点击查询 04. 将查看到的结果通过Excel表格导出 不忘初心,如果您认为这篇 ...

  8. centos7下安装maven

    步骤1:在home目录下解压apache-maven-3.5.0-bin.tar.gz安装包 [root@model ~]# -bin.tar.gz 步骤2:创建/maven目录并将解压后的文件夹移至 ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:Map接口使用的注意事项

    import java.util.HashMap ; import java.util.Map ; import java.util.Set ; import java.util.Iterator ; ...

  10. java中关键字super

    super关键字的作用 java中的super关键字是一个引用变量,用于引用父类对象.关键字“super”以继承的概念出现在类中. 主要用于以下情况:1.调用父类的方法   2.调用父类的变量  3. ...