自定义序列化器Java.lang.stackoverflowerror:null

发布于 2025-02-02 19:58:24 字数 3614 浏览 5 评论 0原文

我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-02-09 19:58:24

我删除了映射的映射并获取我的对象,我通过加入表格进行查询

I removed the mapped by and to get my objects I make queries by joining the tables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文