Reputation: 43
I have the following data hierarchy
User:
@Data
@Getter
@Setter
@Entity
public class User {
@Id
@JsonProperty("sub")
private String id;
private String name;
private String fraction;
private Integer points;
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn (name="population_id", referencedColumnName = "id")
private Population population;
}
Population:
@Data
@Getter
@Setter
@Entity
public class Population {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int total;
private int builder;
private int scientist;
private int resources;
@OneToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "population")
private User user;
}
And here is my controller method:
@GetMapping(value = "/account/accountInfo")
public String getAccountInfo(@RequestHeader(value = "Authorization") String authorization) throws JsonProcessingException {
User userInfo = userService.getUserInfo(authorization);
User userAllInfo = gameService.getUser(userInfo.getId());
return mapper.writeValueAsString(userAllInfo);
}
However when I do a request on that enpoint I receive:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
I wonder if should I change a model to not be bidirectional or maybe there is some option on serialization? What should I change to make it working?
Upvotes: 0
Views: 315
Reputation: 106
It's because you have User in Population and vice versa, I think you can put @JsonIgnore on User in Population.
Upvotes: 2