【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化

接着来说这个JSON循环引用的问题:

关于JSON格式的转化,其实关键就是这几个依赖:

     <!-- json -->

         <!-- 1号 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<!-- 2号 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency> <!-- 3号 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency> <!-- 4号 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<!-- 5号 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- 5号json-lib还需要以下依赖包 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

如果要解决查询出来实体 将实体转换为JSON数据的问题,Product产品类:Disease疾病类(1:n)这两个实体类正确写法如下:

Product.java

 package com.agen.entity;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient; import org.hibernate.annotations.GenericGenerator; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.annotations.Expose; /**
* Product entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "product", catalog = "biologyinfo") public class Product implements java.io.Serializable { private static final long serialVersionUID = 1L;
private String productId;
private String productName;
private String productPath;
private Integer productOrder;
private String productCre;
private Set<Disease> diseases = new HashSet<Disease>(0); // Constructors /** default constructor */
public Product() {
} /** full constructor */
public Product(String productName, String productPath,
Integer productOrder, String productCre, Set<Disease> diseases) {
this.productName = productName;
this.productPath = productPath;
this.productOrder = productOrder;
this.productCre = productCre;
this.diseases = diseases;
} // Property accessors
@GenericGenerator(name = "generator", strategy = "uuid.hex")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "productId", unique = true, nullable = false, length = 36)
public String getProductId() {
return this.productId;
} public void setProductId(String productId) {
this.productId = productId;
} @Column(name = "productName", length = 30)
public String getProductName() {
return this.productName;
} public void setProductName(String productName) {
this.productName = productName;
} @Column(name = "productPath", length = 200)
public String getProductPath() {
return this.productPath;
} public void setProductPath(String productPath) {
this.productPath = productPath;
} @Column(name = "productOrder")
public Integer getProductOrder() {
return this.productOrder;
} public void setProductOrder(Integer productOrder) {
this.productOrder = productOrder;
} @Column(name = "productCre", length = 500)
public String getProductCre() {
return this.productCre;
} public void setProductCre(String productCre) {
this.productCre = productCre;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "product")
public Set<Disease> getDiseases() {
return this.diseases;
} public void setDiseases(Set<Disease> diseases) {
this.diseases = diseases;
} }

Disease.java

 package com.agen.entity;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.annotations.Expose; /**
* Disease entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "disease", catalog = "biologyinfo")
@JsonIgnoreProperties(value = {"product"})
public class Disease implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 1L;
private String diseaseId;
private transient Product product;
private String diseaseName;
private String diseasePath;
private Integer diseaseOrder;
private String diseaseCre;
private Set<Filelist> filelists = new HashSet<Filelist>(0);
private Set<Gene> genes = new HashSet<Gene>(0); // Constructors /** default constructor */
public Disease() {
} /** minimal constructor */
public Disease(Product product) {
this.product = product;
} /** full constructor */
public Disease(Product product, String diseaseName, String diseasePath,
Integer diseaseOrder, String diseaseCre, Set<Filelist> filelists,
Set<Gene> genes) {
this.product = product;
this.diseaseName = diseaseName;
this.diseasePath = diseasePath;
this.diseaseOrder = diseaseOrder;
this.diseaseCre = diseaseCre;
this.filelists = filelists;
this.genes = genes;
} // Property accessors
@GenericGenerator(name = "generator", strategy = "uuid.hex")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "diseaseId", unique = true, nullable = false, length = 36)
public String getDiseaseId() {
return this.diseaseId;
} public void setDiseaseId(String diseaseId) {
this.diseaseId = diseaseId;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "productId", nullable = false)
public Product getProduct() {
return this.product;
} public void setProduct(Product product) {
this.product = product;
} @Column(name = "diseaseName", length = 30)
public String getDiseaseName() {
return this.diseaseName;
} public void setDiseaseName(String diseaseName) {
this.diseaseName = diseaseName;
} @Column(name = "diseasePath", length = 200)
public String getDiseasePath() {
return this.diseasePath;
} public void setDiseasePath(String diseasePath) {
this.diseasePath = diseasePath;
} @Column(name = "diseaseOrder")
public Integer getDiseaseOrder() {
return this.diseaseOrder;
} public void setDiseaseOrder(Integer diseaseOrder) {
this.diseaseOrder = diseaseOrder;
} @Column(name = "diseaseCre", length = 500)
public String getDiseaseCre() {
return this.diseaseCre;
} public void setDiseaseCre(String diseaseCre) {
this.diseaseCre = diseaseCre;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "disease")
public Set<Filelist> getFilelists() {
return this.filelists;
} public void setFilelists(Set<Filelist> filelists) {
this.filelists = filelists;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "disease")
public Set<Gene> getGenes() {
return this.genes;
} public void setGenes(Set<Gene> genes) {
this.genes = genes;
} }

第一种方法:

如上面的Disease.java实体类的类上添加了@JsonIgnoreProperties(value = {"product"})

这个表明 ,现在是在Disease对象的product字段上进行循环引用的隔断。

那么查询的时候,查询Product实体,其中的的disease这个字段是有值的;但是查询Disease实体,其中的product字段是没有值的,因为这个注解在Disease.java类上进行的注解。

同理,如果将这个注解加在哪个实体上,指定了哪个字段就是在哪个字段上阻断。

那么此时的Controller中,我要查询Product对象,并且要将查询出来的对象转化为JSON格式的数据:

 @RequestMapping("/disease")
public String checkdisease(String productId, ModelMap model)
throws IOException {
Product product = productService.get(productId);
//将对象转化为JSON字符串
//方法1 com.fasterxml.jackson.databind.ObjectMapper 使用3号架包
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(product);
model.addAttribute("product", json); //将JSON字符串 转化为对象[只是在此将方法列出,并无实际意义]
//com.fasterxml.jackson.databind.ObjectMapper 使用3号架包
Product product2 = mapper.readValue(json, Product.class); return "/geneinfo/disease/disease";
}

第二种方法:

如上面的Disease.java实体类上添加了Java的关键字   transient

使用transient关键字的作用在于:变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问

这个也是阻断循环引用的一种方法。

同样的,查询Product实体,其中的的disease这个字段是有值的;但是查询Disease实体,其中的product字段是没有值的,因为这个关键字在Disease.java类的product字段上加的。

同理,将这个关键字加在那个字段上 就是在哪个字段上阻断。

那么 此时的Controller中,我们要查询Product对象,并且要将查询出来的对象转化为JSON格式的数据:

 @RequestMapping("/disease")
public String checkdisease(String productId, ModelMap model)
throws IOException {
Product product = productService.get(productId);
//将对象转化为JSON字符串
//方法1 com.google.gson.Gson 使用4号架包
String json1 = new Gson().toJson(product);
model.addAttribute("product", json1);
return "/geneinfo/disease/disease";
}

声明:

上面的两种方法中 ,分别采用哪个架包中的哪个类,标识的很明白了。

但是由于Spring框架默认的是采用com.fasterxml.jackson.annotation进行JSON的转化,所以我们第一种方法中的@JsonIgnoreProperties(value = {"product"})注解必须要添加在实体上。

第二种中的transient关键字是和采用com.google.gson.Gson 谷歌提供的这个架包中的方法配套使用,如果不使用第二种方法进行对象转化JSON格式的数据,可以不用在字段上添加关键字。

声明2:

//net.sf.json.JSONObject
JSONObject.fromObject(product2).toString();

这种将Object转化为JSON数据的方法比较常见,但是它这个架包识别不到@JsonIgnoreProperties这个注解,因为他们不是同一个架包,不是同一个规范,所以在转化JSON的时候,会发生循环引用的问题。

因此这里不采用这种方式转化!!!

一个学习关键字transient的好文章 给大家推荐:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html

当然使用了这两种的方法之后,就不用采用@JsonIgnore了。

上一篇:MySQL官方.NET Core驱动已出,支持EF Core


下一篇:问问题_为什么关闭浏览器后Session会失效