解决hibernate 序列化死循环的问题
用ie8时,请求json,eclipse直接死机!!!!
调试时,可以用chrome,看到无限循环的报错。。。类似
{"empty":true,"total":4,"firstResult":0,"pageSize":10,"rows":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":[{"name":"admin","roles":[{"users":
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
User 与 Role 是hibernate的多对多的关系!!!!
jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归(infinite recursion)问题。这些标注均可用在属性或对应的get、set方法中。
@JsonBackReference和@JsonManagedReference:这两个标注通常配对使用,通常用在父子关系中。@JsonBackReference标注的属性在序列化(serialization,即将对象转换为json数据)时,会被忽略(即结果中的json数据不包含该属性的内容)。@JsonManagedReference标注的属性则会被序列化。在序列化时,@JsonBackReference的作用相当于@JsonIgnore,此时可以没有@JsonManagedReference。但在反序列化(deserialization,即json数据转换为对象)时,如果没有@JsonManagedReference,则不会自动注入@JsonBackReference标注的属性(被忽略的父或子);如果有@JsonManagedReference,则会自动注入自动注入@JsonBackReference标注的属性。
@JsonIgnore:直接忽略某个属性,以断开无限递归,序列化或反序列化均忽略。当然如果标注在get、set方法中,则可以分开控制,序列化对应的是get方法,反序列化对应的是set方法。在父子关系中,当反序列化时,@JsonIgnore不会自动注入被忽略的属性值(父或子),这是它跟@JsonBackReference和@JsonManagedReference最大的区别。