bharath bhushan
bharath bhushan

Reputation: 217

Getting : Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException

I am creating a gym application

Following Entities ClientEntity

enter image description here

TrainerEntity

enter image description here

clientController

enter image description here

trainerController

enter image description here

ClientApi

enter image description here

trainer Api enter image description here

I have created a bi directional mapping b/w trainer Entity and client entity

When I do findall for client or trainer I an getting nested json as in the Client api and is same with trainer api and is giving JSON: Infinite recursion (StackOverflowError);

Here I want from client api

[
    {
        "id": 1,
        "name": "Bharath",
        "address": "#34324 Address",
        "trainerEntity": {
            "id": 1,
            "name": "Niranjan",
         }
    }
]

trainer api:

[
    {
        "id": 1,
        "name": "Niranjan",
        "clientEntity": {
            "id": 1,
            "name": "Bharath",
            "address": "#34324 Address"
         }
     }
]

Upvotes: 0

Views: 2124

Answers (1)

Tomz
Tomz

Reputation: 83

You can use @JsonIgnoreProperties , there is a similar question already asked Infinite Recursion with Jackson JSON and Hibernate JPA issue You can use @JsonIgnoreProperties(value ="clientEntity" ) Private TrainerEntity trainerEntity and similarly for the the other side too

A better option is to use Dto's instead of entities to send the response as json.

Upvotes: 1

Related Questions