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

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2

3

4

5 com.fasterxml.jackson.core

6 jackson-core

7 2.8.1

8

9

10

11 com.fasterxml.jackson.core

12 jackson-annotations

13 2.8.1

14

15

16

17

18 com.fasterxml.jackson.core

19 jackson-databind

20 2.8.1

21

22

23 jackson-core

24 com.fasterxml.jackson.core

25

26

27 jackson-annotations

28 com.fasterxml.jackson.core

29

30

31

32

33

34

35 com.google.code.gson

36 gson

37 2.7

38

39

40

41 net.sf.json-lib

42 json-lib

43 2.4

44 jdk15

45

46

47

48 commons-lang

49 commons-lang

50 2.5

51

52

53 commons-beanutils

54 commons-beanutils

55 1.9.2

56

57

58 commons-collections

59 commons-collections

60 3.2.1

61

62

63 commons-logging

64 commons-logging

65 1.2

66

View Code

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

Product.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.agen.entity;2

3 importjava.util.HashSet;4 importjava.util.Set;5

6 importjavax.persistence.CascadeType;7 importjavax.persistence.Column;8 importjavax.persistence.Entity;9 importjavax.persistence.FetchType;10 importjavax.persistence.GeneratedValue;11 importjavax.persistence.Id;12 importjavax.persistence.OneToMany;13 importjavax.persistence.Table;14 importjavax.persistence.Transient;15

16 importorg.hibernate.annotations.GenericGenerator;17

18 importcom.fasterxml.jackson.annotation.JsonIgnore;19 importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;20 importcom.google.gson.annotations.Expose;21

22 /**

23 * Product entity.@authorMyEclipse Persistence Tools24 */

25 @Entity26 @Table(name = "product", catalog = "biologyinfo")27

28 public class Product implementsjava.io.Serializable {29

30 private static final long serialVersionUID = 1L;31 privateString productId;32 privateString productName;33 privateString productPath;34 privateInteger productOrder;35 privateString productCre;36 private Set diseases = new HashSet(0);37

38 //Constructors

39

40 /**default constructor*/

41 publicProduct() {42 }43

44 /**full constructor*/

45 publicProduct(String productName, String productPath,46 Integer productOrder, String productCre, Setdiseases) {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 @Id57 @GeneratedValue(generator = "generator")58 @Column(name = "productId", unique = true, nullable = false, length = 36)59 publicString getProductId() {60 return this.productId;61 }62

63 public voidsetProductId(String productId) {64 this.productId =productId;65 }66

67 @Column(name = "productName", length = 30)68 publicString getProductName() {69 return this.productName;70 }71

72 public voidsetProductName(String productName) {73 this.productName =productName;74 }75

76 @Column(name = "productPath", length = 200)77 publicString getProductPath() {78 return this.productPath;79 }80

81 public voidsetProductPath(String productPath) {82 this.productPath =productPath;83 }84

85 @Column(name = "productOrder")86 publicInteger getProductOrder() {87 return this.productOrder;88 }89

90 public voidsetProductOrder(Integer productOrder) {91 this.productOrder =productOrder;92 }93

94 @Column(name = "productCre", length = 500)95 publicString getProductCre() {96 return this.productCre;97 }98

99 public voidsetProductCre(String productCre) {100 this.productCre =productCre;101 }102

103 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "product")104 public SetgetDiseases() {105 return this.diseases;106 }107

108 public void setDiseases(Setdiseases) {109 this.diseases =diseases;110 }111

112

113 }

View Code

Disease.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.agen.entity;2

3 importjava.util.HashSet;4 importjava.util.Set;5

6 importjavax.persistence.CascadeType;7 importjavax.persistence.Column;8 importjavax.persistence.Entity;9 importjavax.persistence.FetchType;10 importjavax.persistence.GeneratedValue;11 importjavax.persistence.Id;12 importjavax.persistence.JoinColumn;13 importjavax.persistence.ManyToOne;14 importjavax.persistence.OneToMany;15 importjavax.persistence.Table;16

17 importorg.hibernate.annotations.GenericGenerator;18

19 importcom.fasterxml.jackson.annotation.JsonIgnore;20 importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;21 importcom.google.gson.annotations.Expose;22

23 /**

24 * Disease entity.@authorMyEclipse Persistence Tools25 */

26 @Entity27 @Table(name = "disease", catalog = "biologyinfo")28 @JsonIgnoreProperties(value = {"product"})29 public class Disease implementsjava.io.Serializable {30

31

32 /**

33 *34 */

35 private static final long serialVersionUID = 1L;36 privateString diseaseId;37 private transientProduct product;38 privateString diseaseName;39 privateString diseasePath;40 privateInteger diseaseOrder;41 privateString diseaseCre;42 private Set filelists = new HashSet(0);43 private Set genes = new HashSet(0);44

45 //Constructors

46

47 /**default constructor*/

48 publicDisease() {49 }50

51 /**minimal constructor*/

52 publicDisease(Product product) {53 this.product =product;54 }55

56 /**full constructor*/

57 publicDisease(Product product, String diseaseName, String diseasePath,58 Integer diseaseOrder, String diseaseCre, Setfilelists,59 Setgenes) {60 this.product =product;61 this.diseaseName =diseaseName;62 this.diseasePath =diseasePath;63 this.diseaseOrder =diseaseOrder;64 this.diseaseCre =diseaseCre;65 this.filelists =filelists;66 this.genes =genes;67 }68

69 //Property accessors

70 @GenericGenerator(name = "generator", strategy = "uuid.hex")71 @Id72 @GeneratedValue(generator = "generator")73 @Column(name = "diseaseId", unique = true, nullable = false, length = 36)74 publicString getDiseaseId() {75 return this.diseaseId;76 }77

78 public voidsetDiseaseId(String diseaseId) {79 this.diseaseId =diseaseId;80 }81

82 @ManyToOne(fetch =FetchType.LAZY)83 @JoinColumn(name = "productId", nullable = false)84 publicProduct getProduct() {85 return this.product;86 }87

88 public voidsetProduct(Product product) {89 this.product =product;90 }91

92 @Column(name = "diseaseName", length = 30)93 publicString getDiseaseName() {94 return this.diseaseName;95 }96

97 public voidsetDiseaseName(String diseaseName) {98 this.diseaseName =diseaseName;99 }100

101 @Column(name = "diseasePath", length = 200)102 publicString getDiseasePath() {103 return this.diseasePath;104 }105

106 public voidsetDiseasePath(String diseasePath) {107 this.diseasePath =diseasePath;108 }109

110 @Column(name = "diseaseOrder")111 publicInteger getDiseaseOrder() {112 return this.diseaseOrder;113 }114

115 public voidsetDiseaseOrder(Integer diseaseOrder) {116 this.diseaseOrder =diseaseOrder;117 }118

119 @Column(name = "diseaseCre", length = 500)120 publicString getDiseaseCre() {121 return this.diseaseCre;122 }123

124 public voidsetDiseaseCre(String diseaseCre) {125 this.diseaseCre =diseaseCre;126 }127

128 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "disease")129 public SetgetFilelists() {130 return this.filelists;131 }132

133 public void setFilelists(Setfilelists) {134 this.filelists =filelists;135 }136

137 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "disease")138 public SetgetGenes() {139 return this.genes;140 }141

142 public void setGenes(Setgenes) {143 this.genes =genes;144 }145

146 }

View Code

第一种方法:

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

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

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

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 @RequestMapping("/disease")2 publicString checkdisease(String productId, ModelMap model)3 throwsIOException {4 Product product =productService.get(productId);5 //将对象转化为JSON字符串6 //方法1 com.fasterxml.jackson.databind.ObjectMapper 使用3号架包

7 ObjectMapper mapper = newObjectMapper();8 String json =mapper.writeValueAsString(product);9 model.addAttribute("product", json);10

11 //将JSON字符串 转化为对象[只是在此将方法列出,并无实际意义]12 //com.fasterxml.jackson.databind.ObjectMapper 使用3号架包

13 Product product2 = mapper.readValue(json, Product.class);14

15 return "/geneinfo/disease/disease";16 }

View Code

第二种方法:

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

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

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

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

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 @RequestMapping("/disease")2 publicString checkdisease(String productId, ModelMap model)3 throwsIOException {4 Product product =productService.get(productId);5 //将对象转化为JSON字符串6 //方法1 com.google.gson.Gson 使用4号架包

7 String json1 = newGson().toJson(product);8 model.addAttribute("product", json1);9 return "/geneinfo/disease/disease";10 }

View Code

声明:

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

但是由于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了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java语言中的关键字是指具有特殊含义的单词,这些单词在Java程序中具有特定的用途,不能作为标识符或变量名使用。Java中共有50个关键字,其中包括48个保留关键字和2个特殊关键字。 以下是Java中的各种关键字: 1. abstract:用于定义抽象类和抽象方法。 2. assert:用于调试程序时进行断言判断,如果条件不成立将会抛出AssertionError异常。 3. boolean:用于定义布尔类型变量,只能取值true或false。 4. break:用于跳出循环语句。 5. byte:用于定义字节类型变量,取值范围为-128到127。 6. case:用于在switch语句中匹配选项。 7. catch:用于捕获异常。 8. char:用于定义字符类型变量。 9. class:用于定义类。 10. const:Java虽然保留了此关键字,但并没有使用,因此不能用于定义常量。 11. continue:用于跳过循环中的某个迭代。 12. default:用于switch语句中的默认选项。 13. do:用于定义do-while循环。 14. double:用于定义双精度浮点类型变量。 15. else:用于if语句中条件不成立时执行的代码块。 16. enum:用于定义枚举类型。 17. extends:用于继承一个类或实现一个接口。 18. final:用于定义常量或不可变的变量,或者修饰类、方法、变量等,表示其不可再被继承、重写或修改。 19. finally:用于定义无论是否有异常发生都需要执行的代码块。 20. float:用于定义单精度浮点类型变量。 21. for:用于定义for循环。 22. goto:Java虽然保留了此关键字,但并没有使用,因此不能跳转到标签。 23. if:用于定义条件语句。 24. implements:用于实现一个接口。 25. import:用于导入其他类的定义。 26. instanceof:用于判断一个对象是否属于某个类或实现了某个接口。 27. int:用于定义整型变量。 28. interface:用于定义接口。 29. long:用于定义长整型变量。 30. native:用于调用本地方法。 31. new:用于创建一个对象。 32. package:用于定义包。 33. private:用于定义私有成员,只能在当前类中访问。 34. protected:用于定义受保护的成员,只能在当前类及其子类和同一个包中访问。 35. public:用于定义公共成员,可以被任何类访问。 36. return:用于从方法中返回值。 37. short:用于定义短整型变量。 38. static:用于定义静态成员,只有一个拷贝,可以通过类名直接访问。 39. strictfp:用于声明浮点数计算具有严格的规范化行为。 40. super:用于引用父类的成员。 41. switch:用于定义switch语句。 42. synchronized:用于定义同步方法或同步代码块。 43. this:用于引用当前对象。 44. throw:用于抛出异常。 45. throws:用于声明方法可能抛出的异常。 46. transient:用于声明不需要持久化的变量。 47. try:用于定义异常处理代码块。 48. void:用于定义无返回值的方法。 49. volatile:用于声明变量是易变的,即每次访问都需要从主存中读取。 50. while:用于定义while循环。 以上就是Java中的各种关键字,这些关键字Java程序中起着非常重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值