笔者的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. SciPy 插值

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  2. 802.11X用户身份验证

    静态WEP企图同时解决802.11无线网络安全的两个问题.它即打算提供身份验证以限定拥有特定密钥方能进行网络访问,也想要提供机密性以在数据经过无线链路时予以加密.然而,它在这两方面的表现都不是特别好. ...

  3. HTML学习第六天

    HTML学习第六天 一.全局属性 contentEditable属性,控制标签元素的可修改性,默认与“”(空字符串)都代表真,即可编辑 <!DOCTYPE html> <html l ...

  4. CodeForces - 876D Sorting the Coins

    题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...

  5. chart 目录结构【转】

    chart 是 Helm 的应用打包格式.chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service.Deployment.PersistentV ...

  6. 第2节 网站点击流项目(下):3、流量统计分析,分组求topN

    四. 模块开发----统计分析 select * from ods_weblog_detail limit 2;+--------------------------+---------------- ...

  7. vue - 子组件向父组件 传递方法和参数

    1,子组件 TodoItem.vue  : <template>   <div class="todo-item" :class="{'is-compl ...

  8. VTK基于MFC单文档的开发

    目录 项目的搭建 相关头文件的引用 添加成员变量vtkRenderer, vtkMFCWindow CXxxView()中实例化变量vtkRenderer CXxxView::OnInitialUpd ...

  9. 1、MYSQL 数据库的安装与配置

    安装 1.打开官网https://www.mysql.com,选择社区版本   2.如图点击下在安装(本人在下载过程中亲身感觉下载时间非常漫长,需要等待,不知道为啥会有限速,可以参考网上教程用迅雷进行 ...

  10. mysql日常小总结(其实就今天)

    联表查询: SELECT t1.user_Name  FROM t_user AS t1   ,  t_comment AS t2 WHERE t2.user_id=t1.id 结果如图: 加上GRO ...