How to handle bidirectional one-to-one sleeps with Jackson

I use @JsonIgnoreAnnotation to prevent endless loops when creating Json for answers, and it works fine for what I need, but I would like to know if there is some alternative where the property is not actually ignored but also prevents the endless loop.

For example, I have entityA with the following properties:

int id
String name
EntityB example;

and entityB has

int id
String something
EntityA entityAExample //(this one goes with the JsonIgnore)

So, if I get all the registers in entityA, the answer will look like this:

[{
    "id":"1",
    "name": "name",
    "entityB": {
                 "id":"1",
                 "something": "text"
               }
}]

And entityB would look like this:

[{
   "id":"1",
   "something": "text"
}]

, , , B entityA ( , " " ), :

[{
    "id":"1",
    "something": "text",
    "entityAExample": {
                      "id":"1",
                      "name": "name"
                      }
}]

, , , .

+4
1

json.

, - @JsonIdentityInfo. :

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class EntityA{
    ...
}

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class EntityB{
    ...
}

, , , , (EntityA), , , .

, , id.

, , , , .

, :

[{
    "id":"1",
    "name": "name",
    "entityB": {
                 "id":"2",
                 "something": "text"
                 "entityAExample": "1"                                       
               }
}]

EntityB , :

[{
    "id":"1",
    "name": "name",
    "entityB": {
                 "id":"2",
                 "something": "text"
                 "entityAExample": {
                                    "id": "1",
                                    "name": "name",
                                    "entityBExample": "2"                                         
                                   }
               }
}]

, "id" . wiki.

, , .

+2

Source: https://habr.com/ru/post/1622234/


All Articles