【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化
接着来说这个JSON循环引用的问题:
关于JSON格式的转化,其实关键就是这几个依赖:
1 <!-- json --> 2 3 <!-- 1号 --> 4 <dependency> 5 <groupId>com.fasterxml.jackson.core</groupId> 6 <artifactId>jackson-core</artifactId> 7 <version>2.8.1</version> 8 </dependency> 9 <!-- 2号 --> 10 <dependency> 11 <groupId>com.fasterxml.jackson.core</groupId> 12 <artifactId>jackson-annotations</artifactId> 13 <version>2.8.1</version> 14 </dependency> 15 16 <!-- 3号 --> 17 <dependency> 18 <groupId>com.fasterxml.jackson.core</groupId> 19 <artifactId>jackson-databind</artifactId> 20 <version>2.8.1</version> 21 <exclusions> 22 <exclusion> 23 <artifactId>jackson-core</artifactId> 24 <groupId>com.fasterxml.jackson.core</groupId> 25 </exclusion> 26 <exclusion> 27 <artifactId>jackson-annotations</artifactId> 28 <groupId>com.fasterxml.jackson.core</groupId> 29 </exclusion> 30 </exclusions> 31 </dependency> 32 33 <!-- 4号 --> 34 <dependency> 35 <groupId>com.google.code.gson</groupId> 36 <artifactId>gson</artifactId> 37 <version>2.7</version> 38 </dependency> 39 <!-- 5号 --> 40 <dependency> 41 <groupId>net.sf.json-lib</groupId> 42 <artifactId>json-lib</artifactId> 43 <version>2.4</version> 44 <classifier>jdk15</classifier> 45 </dependency> 46 <!-- 5号json-lib还需要以下依赖包 --> 47 <dependency> 48 <groupId>commons-lang</groupId> 49 <artifactId>commons-lang</artifactId> 50 <version>2.5</version> 51 </dependency> 52 <dependency> 53 <groupId>commons-beanutils</groupId> 54 <artifactId>commons-beanutils</artifactId> 55 <version>1.9.2</version> 56 </dependency> 57 <dependency> 58 <groupId>commons-collections</groupId> 59 <artifactId>commons-collections</artifactId> 60 <version>3.2.1</version> 61 </dependency> 62 <dependency> 63 <groupId>commons-logging</groupId> 64 <artifactId>commons-logging</artifactId> 65 <version>1.2</version> 66 </dependency>
如果要解决查询出来实体 将实体转换为JSON数据的问题,Product产品类:Disease疾病类(1:n)这两个实体类正确写法如下:
Product.java
1 package com.agen.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.FetchType; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.Id; 12 import javax.persistence.OneToMany; 13 import javax.persistence.Table; 14 import javax.persistence.Transient; 15 16 import org.hibernate.annotations.GenericGenerator; 17 18 import com.fasterxml.jackson.annotation.JsonIgnore; 19 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 20 import com.google.gson.annotations.Expose; 21 22 /** 23 * Product entity. @author MyEclipse Persistence Tools 24 */ 25 @Entity 26 @Table(name = "product", catalog = "biologyinfo") 27 28 public class Product implements java.io.Serializable { 29 30 private static final long serialVersionUID = 1L; 31 private String productId; 32 private String productName; 33 private String productPath; 34 private Integer productOrder; 35 private String productCre; 36 private Set<Disease> diseases = new HashSet<Disease>(0); 37 38 // Constructors 39 40 /** default constructor */ 41 public Product() { 42 } 43 44 /** full constructor */ 45 public Product(String productName, String productPath, 46 Integer productOrder, String productCre, Set<Disease> diseases) { 47 this.productName = productName; 48 this.productPath = productPath; 49 this.productOrder = productOrder; 50 this.productCre = productCre; 51 this.diseases = diseases; 52 } 53 54 // Property accessors 55 @GenericGenerator(name = "generator", strategy = "uuid.hex") 56 @Id 57 @GeneratedValue(generator = "generator") 58 @Column(name = "productId", unique = true, nullable = false, length = 36) 59 public String getProductId() { 60 return this.productId; 61 } 62 63 public void setProductId(String productId) { 64 this.productId = productId; 65 } 66 67 @Column(name = "productName", length = 30) 68 public String getProductName() { 69 return this.productName; 70 } 71 72 public void setProductName(String productName) { 73 this.productName = productName; 74 } 75 76 @Column(name = "productPath", length = 200) 77 public String getProductPath() { 78 return this.productPath; 79 } 80 81 public void setProductPath(String productPath) { 82 this.productPath = productPath; 83 } 84 85 @Column(name = "productOrder") 86 public Integer getProductOrder() { 87 return this.productOrder; 88 } 89 90 public void setProductOrder(Integer productOrder) { 91 this.productOrder = productOrder; 92 } 93 94 @Column(name = "productCre", length = 500) 95 public String getProductCre() { 96 return this.productCre; 97 } 98 99 public void setProductCre(String productCre) { 100 this.productCre = productCre; 101 } 102 103 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "product") 104 public Set<Disease> getDiseases() { 105 return this.diseases; 106 } 107 108 public void setDiseases(Set<Disease> diseases) { 109 this.diseases = diseases; 110 } 111 112 113 }