笔者的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. 常用keycode列表

    KEYCODE列表 电话键   KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOME 按键Home 3 KEYCODE_MENU 菜单键 82 K ...

  2. Win7 + Ubuntu 14.04 +tomcat + mysql 搭建测试环境手册

    一.Win7下做安装ubuntu 下载ununtu14.04和EasyBCD软件 Win7下要腾出一个盘来安装ubuntu.我是格式化掉F盘,然后回到桌面,右键点击计算机-->管理-->存 ...

  3. linux下解决git clone太慢

    此教程同样也适用与vscode下载太慢的问题 git和vscode会自动使用http_proxy,https_proxy环境变量的代理,所以我们只需要设置这个环境变量即可 前提 需要一个可用的代理,这 ...

  4. python+ selenium + webdriver的环境准备

    web自动化安装 1.安装最新的selenium pip install -U selenium 2.安装chrom浏览器和chromdriver的下载 http://chromedriver.sto ...

  5. L2-002. 链表去重(模拟)

    题意: 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须被保存在另外一个链 ...

  6. Vulkan SDK Demo 之一 熟悉

    DiligentEngine的API是D3d11和D3D12风格的,vulkan也被封装成了这种风格的API. 在了解Diligent Engine是如何对vulkan进行封装之前,我准备先学习下Vu ...

  7. 人脸识别 API Key和Secret Key作用

    App key简称API接口验证序号,是用于验证API接入合法性的.接入哪个网站的API接口,就需要这个网站允许才能够接入,如果简单比喻的话:可以理解成是登陆网站的用户名 App Secret简称AP ...

  8. 不可不知的spark shuffle

    shuffle概览 一个spark的RDD有一组固定的分区组成,每个分区有一系列的记录组成.对于由窄依赖变换(例如map和filter)返回的RDD,会延续父RDD的分区信息,以pipeline的形式 ...

  9. G. Repeat it

    G. Repeat it time limit per test 2.0 s memory limit per test 64 MB input standard input output stand ...

  10. HDU - 6205 card card card (尺取法)

    题意:有n堆牌,ai表示每堆牌的牌数,bi表示每堆牌的penaltyvalue,操作开始前,可以重复进行将第一堆牌挪到最后一堆这一操作.然后,对于挪完后的牌,从第一堆开始,依次取.对于每一堆牌,首先将 ...