杰克逊:JsonBackReference-无限递归问题 [英] Jackson: JsonBackReference - infinite recursion problem

查看:361
本文介绍了杰克逊:JsonBackReference-无限递归问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JSON的了解有限,希望您能为我指明正确的方向.

My knowledge with JSON is limited so hopefully you can point me in the right direction.

我正在尝试发布一个对象(库),但是由于无限递归而引发了异常

I'm trying to post an object (Library) but this throws an exception due to infinite recursion

public class Library{
    private List<Book> books= new 
    ArrayList<Book>();
    //omitting rest of attributes, constructor, 
   and getter/setters
}

public class Book{
    private List<Author> authors= new 
    ArrayList<Author>();
    //omitting rest of attributes, constructor, 
    and getter/setters
}

public class Author{
    private Book book;
    //omitting rest of attributes, constructor, 
    and getter/setters
}

如您所见,这导致无法序列化的无限递归:

As you can see, this results in an infinite recursion that cannot be serialized:

Library -> Book -> Author -> Book -> Author...

这是设计较差的,但我没有选择更改模型的选项.

This is poorly designed but I do not have the option to change the model.

我尝试使用JsonManagedReferenceJsonBackReference解决此问题.

I have tried to solve this making use of the JsonManagedReference and JsonBackReference.

public class Library{
    @JsonManagedReference(value="book")
    private List<Book> books= new 
    ArrayList<Book>();
    //omitting rest of attributes, constructor, 
   and getter/setters
}

public class Author{
    @JsonBackReference(value="book")
    private Book book;
    //omitting rest of attributes, constructor, 
    and getter/setters
}

但是,这不起作用,并且出现错误:

However, this is not working and I get error:

由以下原因引起:java.lang.IllegalArgumentException:无法处理 托管/后备参考书':从以下位置找不到后备参考书属性 类型[集合类型;类java.util.List,包含[简单类型, 类....... model.Book]]

Caused by: java.lang.IllegalArgumentException: Can not handle managed/back reference 'book': no back reference property found from type [collection type; class java.util.List, contains [simple type, class .......model.Book]]

如果删除@JsonManagedReference注释并保留BackReference,则能够序列化该对象.但是,序列化的对象将Author中的Book实例设置为null,从而在尝试将其持久化到数据库中时导致异常.

I am able to serialize the object if I remove the @JsonManagedReferenceannotation and leave the BackReference. However, the serialized object sets Book instance in Author to null, resulting in an exception when attempting to persists this in the database.

我将注解放置在正确的位置吗?有什么建议吗?

Am I placing my annotations in the right place? Any suggestions?

编辑:如答复中所建议,我的注释被错误地放置了.以下工作可序列化对象,但Author丢失了对Book的引用

As suggested in the responses, my annotations were wrongly placed. The following works to serialize the object but Author is losing the reference to Book

public class Book{
@JsonManagedReference
private List<Author> authors= new 
ArrayList<Author>();
}

public class Author{
@JsonBackReference
private Book book;
}

这解决了无限递归的问题.但是,由于无法插入空值,因此出现SQL异常.

This resolves the issue of infinite recursion. However, I get SQL exception as cannot insert a null value.

Library -> Book -> Author X Book

author表包含对该书的一个不可为null的引用.

author table contains a nun-nullable reference to the book.

谢谢

推荐答案

看起来像您的注释在错误的位置.

Looks like your annotations in the wrong place.

为什么在防止AuthorBook之间的递归时Library的书具有ManagedReference?

Why the Library has ManagedReference for books while you're preventing recursion between Author and Book?

这可能是应该的方式:

public class Author {
    @JsonBackReference
    private Book book;
}

public class Book {
    @JsonManagedReference
    private List<Author> authors = new ArrayList<Author>();
}

此外,肯定存在错误的模型,即Author与一本书具有一对一的关系.逻辑上Author可能拥有许多books.

More over, there is definitely a wrong model that Author has one-to-one relationship with a book. Logically an Author may own many books.

这篇关于杰克逊:JsonBackReference-无限递归问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆