笔者的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
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。