Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion…
- User
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.cgg.wsp.dto.ServerDto["srvDatabaseList"]->java.util.ArrayList[0]->com.cgg.wsp.dto.DatabaseDto["dbServer"]->com.cgg.wsp.dto.ServerDto["srvDatabaseList"]->java.util.ArrayList[0]->com.cgg.wsp.dto.DatabaseDto["dbServer"]->com.cgg.wsp.dto.ServerDto["srvDatabaseList"]->java.util.ArrayList[0]->com.cgg.wsp.dto.DatabaseDto["dbServer"]->com.cgg.wsp.dto.ServerDto["srvDatabaseList"]->java.util.ArrayList[0]->com.cgg.wsp.dto.DatabaseDto["dbServer"]->com.cgg.wsp.dto.ServerDto["srvDatabaseList"]
- Assistant
This error is caused by an infinite loop or circular reference within the JSON data being deserialized. In this specific case, it seems that the
ServerDtoobject contains a list ofDatabaseDtoobjects, and eachDatabaseDtoobject contains a reference to theServerDtoobject. This creates an endless cycle of references between these classes, causing the JSON deserialization process to throw aStackOverflowError.To fix this issue, you should remove the circular reference between these classes or use Jackson annotations to handle the circular reference. One way to handle this is by annotating one of the fields involved in a circular reference with
@JsonIgnoreor@JsonBackReference. This tells Jackson to ignore the field or only include it during serialization, thus breaking the circular reference. - User
show me an example
- Assistant
Sure, here's an example of one way to handle a circular reference using Jackson annotations:
Assume we have the following two classes:
codepublic class ServerDto { private String name; private List<DatabaseDto> databases; // getters and setters } public class DatabaseDto { private String name; private ServerDto server; // getters and setters }To handle the circular reference between
ServerDtoandDatabaseDto, we can add the@JsonBackReferenceannotation to theserverfield inDatabaseDto, like this:codepublic class DatabaseDto { private String name; @JsonBackReference private ServerDto server; // getters and setters }