@JsonInclude(JsonInclude.Include.NON_NULL)作用
作者:互联网
其作用是jackson 实体转json 为NULL的字段不参加序列化(即不显示)
如果在某字段前添加了此注解,如果此字段在输出的时候为null时,系统将不显示此字段。
一、没加此注解时,执行一个简单的查询操作
1. User.java类内容如下:
package com.example.simple.model; /* * 用户表 * */ import java.io.Serializable; import java.sql.Blob; import java.util.Date; import java.util.List; public class SysUser implements Serializable { private static final long serialVersionUID = 6320941908222932112L ; /* * 用户ID * */ private Long id; /* * 用户 * */ private String userName; /* * 密码 * */ private String userPassword; /* * 邮箱 * */ private String userEmail; /* * 简介 * */ private String userInfo; /* * 头像 * */ private byte[] headImg; /* * 创建时间 * */ private Date createTime; //一个用户拥有一个角色 为实现自动映射一对一关系 因此需要加上SysRole字段 private SysRole role; private List<SysRole> roleList; public List<SysRole> getRoleList() { return roleList; } public void setRoleList(List<SysRole> roleList) { this.roleList = roleList; } public SysRole getRole() { return role; } public void setRole(SysRole role) { this.role = role; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
测试代码如下:
@Test public void testSelectById(){ //获取会话 SqlSession sqlSession = getSqlSession(); try{ //获取UserMapper接口 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); SysUser user = userMapper.selectById(1020L); Assert.assertNotNull(user); Assert.assertEquals("test-selective",user.getUserName()); }finally { sqlSession.close(); } }
查询结果如下:
DEBUG [main] - Cache Hit Ratio [com.example.simple.mapper.RoleMapper]: 0.0
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 1020(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 1020, test-selective, 123456, null, <<BLOB>>, <<BLOB>>, 2020-12-09 21:04:13
DEBUG [main] - <== Total: 1
二、如果加上此注解
1. User.java类内容如下:
package com.example.simple.model; /* * 用户表 * */ import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; import java.sql.Blob; import java.util.Date; import java.util.List; public class SysUser implements Serializable { private static final long serialVersionUID = 6320941908222932112L ; /* * 用户ID * */ private Long id; /* * 用户 * */ private String userName; /* * 密码 * */ private String userPassword; /* * 邮箱 * */ @JsonInclude(JsonInclude.Include.NON_NULL) private String userEmail; /* * 简介 * */ private String userInfo; /* * 头像 * */ private byte[] headImg; /* * 创建时间 * */ private Date createTime; //一个用户拥有一个角色 为实现自动映射一对一关系 因此需要加上SysRole字段 private SysRole role; private List<SysRole> roleList; public List<SysRole> getRoleList() { return roleList; } public void setRoleList(List<SysRole> roleList) { this.roleList = roleList; } public SysRole getRole() { return role; } public void setRole(SysRole role) { this.role = role; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
测试代码一样,控制台执行结果也和上面一样,只是在页面上显示时email为空的值不在页面上显示,这个部分暂时还没实现,后面实现了再更新。
标签:NON,return,String,void,private,public,id,Include,JsonInclude 来源: https://www.cnblogs.com/yeyuting/p/14245275.html