自定义序列化器Java.lang.stackoverflowerror:null
我将jsonignore放在我的实体帐户上,并在朋友字段上放置自定义序列化器。
现在,我想通过控制器恢复帐户,不再担心,但是,一旦我尝试使用ID或其他错误恢复它,我就有以下错误:
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;
}
serialializer:
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();
}
}
councorService:counltyervice:councorservice:
@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 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我删除了映射的映射并获取我的对象,我通过加入表格进行查询
I removed the mapped by and to get my objects I make queries by joining the tables