Reputation: 33
I am working with fasterxml, and I have two objects that have a relationship "one to many" or "many to one". For example the class Author and the class Book, one author has many books, and one book has one author. Can I tell fasterxml not to serialize the author of the book, only when the book is in it's author's books collection, but when the book is on itself, to serialize it.
Author class:
public class Author{
public int id;
public string name;
@JsonManagedReference
public Set<Book> books;
}
Book class:
public class Book{
public int id;
public string name;
@JsonBackReference
public Author author;
}
That setup works just fine if I want to get only the author, because the books are in place and theirs's author property isn't being serialized, but if I want to serialize only the book, it's author again isn't being serialized, because of the "@JsonBackReference" annotation. Is there any workaround in the said situation? Here are some more examples if you are not getting what I mean...
When I serialize an Autor:
{
id:3,
name: "Ivan Vazov"
books:[
{
id:5,
name: "Under the Yoke"
}
]
}
And that is what I want here.
When I serialize a Book:
{
id:5,
name: "Under the Yoke"
}
But i don't want this, I want this:
{
id:5,
name: "Under the Yoke",
author: {
id:3,
name: "Ivan Vazov"
}
}
Any thoughts on the matter would be great! Thanks.
Upvotes: 3
Views: 2127
Reputation: 1
In the Book schema, write:
@JsonIgnoreProperties("books") // Ignore le champ books dans author lors de la sérialisation
private Author author;
In the Author schema, write:
@JsonIgnoreProperties("author") // Ignore le champ author dans book lors de la sérialisation
private Set<Books> books = new HashSet<>();
Upvotes: 0
Reputation: 10075
DTOs are the way to go in more complex scenarios, especially on the inbound side. For dynamic filtering of simpler use cases i wrote an addon for jackson to use antpath style filtering. Probably it helps you:
https://github.com/Antibrumm/jackson-antpathfilter
Upvotes: 0
Reputation: 18899
If you want the references to be serialized from both sides(Book, Author) then jackson faces the issue with circular reference where it get's stuck in an endless loop of references when serializing one of those 2 objects.
The workaround was with @JsonManagedReference
and @JsonBackReference
where jackson ignored 1 side and serialized only the other side in order to avoid circular reference.
The solution to your problem (when you want to serialize both sides) is to create seperate DTO objects(AuthorDto, BookDto) and instead of returning from your controller a Author to be serialized you return an AuthorDto. Same with Book. Then circlular reference problem does not exist any more and both sides serialize the problematic references.
Upvotes: 1
Reputation: 788
You have add fetch type in ManyToOne relationship side as follow.
To fetch all book entity then add fetch type Eager.
@ManyToOne( fetch = FetchType.EAGER, optional = false )
To fetch only book entity then add fetch lazy.
@ManyToOne( fetch = FetchType.LAZY, optional = false )
Upvotes: -1