开发环境:Struts2+Hibernate4+Spring4+MySQL5+Tomcat7+JDK1.8+Maven
在实现关系映射的时候,我采用的是注解形式实现的,但是,我想在写json数据的时候忽略某些映射出来的数据,完整的代码如下:
package com.value.yun.modules.entity; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonManagedReference; import com.value.yun.common.base.BaseDataEntity; import org.hibernate.annotations.*; import org.hibernate.annotations.Cache; import javax.persistence.*; import javax.persistence.Entity; import javax.persistence.Table; import java.util.Date; import java.util.List; /** * Created by 丽 on 2015/4/14. */ @Entity @Table(name = "user") @DynamicInsert @DynamicUpdate @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @JsonIgnoreProperties(value ={"role","menuList"}) public class User extends BaseDataEntity{ /** * 密码 */ private String password; /** * 工号、学号 */ private String number; /** * 真实姓名 */ private String realName; /** * 手机 */ private String mobile; /** * 邮箱 */ private String email; /** * qq */ private String qq; /** * 登陆的ip */ private String loginIp; /** * 登录时间 */ private Date loginTime; /** * 登陆名 */ private String loginName; /** * 角色、类型 */ @JsonManagedReference private Role role; @JsonManagedReference private List
在上面的代码中,对于json需要忽略的属性,必须要在@JsonIgnoreProperties中指定,否则不会有有效果;
关于另外两个注解@JsonManagedReference、@JsonBackReference,具体作用如下:
(see Jira entry JACKSON-235 for more details)
Before Jackson 1.5, back references such as "parent" links from child objects, previous sibling for doubly-linked lists, and general bi-directional references for ORM-managed beans (iBatis, Hibernate) would cause serialization to failure since they are cyclic dependencies.
In Jackson 1.6 there are 2 new annotations that allow specifying relationship between 2 properties that form such bi-directional dependency so that Jackson can properly omit cycle during serialization, and re-construct during serialization.
New annotations
Annotations are:
-
@JsonManagedReference is the "forward" part of reference: one that gets serialized normally, and handling of which triggers back-linkage for the other reference
-
Annotated property can be a bean, array, Collection (List, Set) or Map type, and it must be a bean property (handled by a property of type serialized using BeanSerializer
-
-
@JsonBackReference is the "back" part of reference: it will be omitted from serialization, and re-constructed during deserialization of forward reference.
- Annotated property must be of bean type
These annotations can be used for:
* Methods (getters and setters) * Fields
but not for types (classes) or constructor arguments.
Annotations take optional "name" parameter; if omitted default name is used. Name is needed to allow multiple reference pairs for one bean type.
上面的这段话说明,@JsonManagedReference、@JsonBackReference这两个注解是JDK1.6开始才有的,而且是针对于父子关系映射的,当然非父子关系,即普通属性也行的,但是,有这两个注解的属性,必须要在@JsonIgnoreProperties中指定才会有效。
@JsonManagedReference只适合bean 类型的属性;
@JsonManagedReference的适用范围较广:bean, array, Collection (List, Set) or Map 类型,但是,集合中的实体对象必须是实现了序列化接口的。
@JsonManagedReference、@JsonBackReference这两个注解可以用在getters和setters方法上,也可以用在某个属性上,但是不能用在某个类上或者某个构造方法上。