@JsonInclude(Include.NON_NULL)
项目框架:spring+springMVC+mybatis
@JsonInclude(Include.NON_NULL)的作用:jackson 实体转json 为NULL的字段不参加序列化(即不显示)
实体类:
package com.baidu.entity; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import org.springframework.data.annotation.Transient; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 8121761080892505330L; private String username; /*@Transient @JsonIgnore*/ @JsonInclude(JsonInclude.Include.NON_NULL) private String password; //transient public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
后台返回结果:
页面返回结果:
从图片上可以看出后台返回的实体在转化为json时,字段值为null的字段不显示。