I put jsonignore all over my entity account, and a custom serializer on the Friend field.
now that I want to recover an account via controller no more worries, however as soon as I try to recover it in service with the id or other I have the following error:
java.lang.StackOverflowError: null
Entity:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int account_id;
private String firstname;
private String lastname;
@Column(unique = true)
private String username;
private boolean active;
private String avatar;
@JsonIgnore
@ManyToMany
@JoinTable(name = "ludo",
joinColumns = @JoinColumn(name = "id_account"),
inverseJoinColumns = @JoinColumn(name = "id_boardGame"))
private List<Boardgame> boardgameList;
@JsonIgnore
@OneToMany(mappedBy = "account")
List<Friends> FriendsList;
@ManyToOne
@JoinColumn(name="FK_country_Id",nullable = true,referencedColumnName = "country_id")
private Country country;
@ManyToOne
@JoinColumn(name="FK_language_Id",nullable = true,referencedColumnName = "language_id")
private Language language;
@Column(updatable = false)
@CreationTimestamp
private LocalDateTime createdAt;
}
Serializer:
public class FriendsSerializer extends StdSerializer<Friends> {
public FriendsSerializer() {
this(null);
}
public FriendsSerializer(Class<Friends> t) {
super(t);
}
@Override
public void serialize(Friends friends, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeNumberField("account_id", friends.getFriend().getAccount_id());
jsonGenerator.writeStringField("username", friends.getFriend().getUsername());
jsonGenerator.writeStringField("createdAt", String.valueOf(friends.getCreatedAt()));
jsonGenerator.writeBooleanField("active", friends.isActive());
jsonGenerator.writeStringField("avatar",friends.getFriend().getAvatar());
jsonGenerator.writeFieldName("country");
jsonGenerator.writeObject(friends.getFriend().getCountry());
jsonGenerator.writeEndObject();
}
}
AccountService:
@Override
public ApiResponse getFriendNoConfirmation(Account account) {
if(account != null && account.getAccount_id() != 0){
// List<Friends> listIdAccount = this.friendRepository.getFriendsNoConfirmation(account.getAccount_id());
List<Integer> listIdAccount = this.friendRepository.getFriendsNoConfirmation(account.getAccount_id());
if(listIdAccount != null){
List<Account> listAccount = new ArrayList<>();
for(int idAccount: listIdAccount){
System.out.println(idAccount);
Account friendAccount = this.getAcountById(idAccount);
System.out.println(friendAccount);
listAccount.add(friendAccount);
}
return new ApiResponse(true, listIdAccount,BASE_CODE + "friendListNoConfirm.success");
}else{
return new ApiResponse(true, null,BASE_CODE + "friendListNoConfirm.error");
}
}else{
return new ApiResponse(true, null,BASE_CODE + "friendListNoConfirm.error");
}
}
@Override
public Account getAcountById(Integer id){
return this.accountRepository.getAccountById(id);
}
I removed the mapped by and to get my objects I make queries by joining the tables