大家好,又见面了,我是你们的朋友全栈君。
@jsonignore的作用
作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
如下:
package com.hzboy.orm; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonManagedReference; /** * 系统用户实体类 * @author tinner * */ @Entity(name = "com.hzboy.orm.User") @Table(name = "sys_user") @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler"}) public class User extends BaseEntity { /** * */ private static final long serialVersionUID = -1343842065506227012L; public User() { } public User(Integer id) { this.id = id; } // 登录名 private String loginName; // 密码123456-b594510740d2ac4261c1b2fe87850d08 private String pswd; // 姓名 private String nickName; // 性别 private Short sex; // 年龄段 private String ageStage; // 是否系统管理员 private boolean systemAdmin; //联系电话 private String tel; //Email private String email; //是否工程师 private Boolean isEnginner; //是否前端客户 是则显示前台html,但同时保留后台相关模块 private Boolean isFrontUser; //相关部门 private Department department; //相关部门前台描述 private String departmentName; private Integer departmentId; //用户编号 private String userCode; //附加用于导入 private String idCode; //附加的部门信息 用于计算 private String departmentCode; @Column(name = "login_name") public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @Column(name = "pswd") public String getPswd() { return pswd; } public void setPswd(String pswd) { this.pswd = pswd; } @Column(name = "nick_name") public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } @Column(name = "sex") public Short getSex() { return sex; } public void setSex(Short sex) { this.sex = sex; } @Column(name = "age_stage") public String getAgeStage() { return ageStage; } public void setAgeStage(String ageStage) { this.ageStage = ageStage; } @Column(name = "system_admin") public boolean isSystemAdmin() { return systemAdmin; } public void setSystemAdmin(boolean systemAdmin) { this.systemAdmin = systemAdmin; } private List<Role> roles; @ManyToMany(fetch = FetchType.LAZY, cascade = (CascadeType.REMOVE)) @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") }) public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } private Integer[] roleIds; @Transient public Integer[] getRoleIds() { return roleIds; } public void setRoleIds(Integer[] roleIds) { this.roleIds = roleIds; } @Column(name="email") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name="is_enginner") public Boolean getIsEnginner() { return isEnginner; } public void setIsEnginner(Boolean isEnginner) { this.isEnginner = isEnginner; } @Column(name="is_front_user") public Boolean getIsFrontUser() { return isFrontUser; } public void setIsFrontUser(Boolean isFrontUser) { this.isFrontUser = isFrontUser; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @JsonIgnore @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) @JoinColumn(name="department_id") public Department getDepartment() { return department; } @JsonManagedReference public void setDepartment(Department department) { this.department = department; } @Transient public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Transient public Integer getDepartmentId() { return departmentId; } public void setDepartmentId(Integer departmentId) { this.departmentId = departmentId; } @Column(name="user_code") public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } @Column(name="id_from_import") public String getIdCode() { return idCode; } public void setIdCode(String idCode) { this.idCode = idCode; } public String getDepartmentCode() { return departmentCode; } public void setDepartmentCode(String departmentCode) { this.departmentCode = departmentCode; } }
上个实体User中的 department属性就在getDepartment属性上加了 @JsonIgnore,这个时候从后台推数据到前台的时候,就会把department这个引用属性给忽略掉。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128091.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...