欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jackson庫進行JSON?序列化時遇到了無限遞歸(Infinite?Recursion)的問題及解決方案

 更新時間:2025年03月19日 14:23:33   作者:王小工  
使用Jackson庫進行JSON序列化時遇到了無限遞歸(Infinite?Recursion)問題,這是因為兩個實體類ComPointQuotaEntity和?ComPointEntity之間存在雙向關(guān)聯(lián)point和pointQuota相互引用,本文給大家介紹解決方案,感興趣的朋友一起看看吧

使用 Jackson 庫進行 JSON 序列化時遇到了 ‌無限遞歸(Infinite Recursion)‌ 問題,這是因為兩個實體類 ComPointQuotaEntity 和 ComPointEntity 之間存在雙向關(guān)聯(lián)(point 和 pointQuota 相互引用),導(dǎo)致序列化時陷入死循環(huán)。以下是解決方案:

解決方案‌

1. 使用 @JsonIgnore 忽略一個方向的引用

在其中一個實體類的關(guān)聯(lián)字段上添加 @JsonIgnore 注解,直接阻止某一方的序列化:

// ComPointQuotaEntity.java
public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    private ComPointEntity point;  // 保留此字段的序列化
    // 其他字段...
}
// ComPointEntity.java
public class ComPointEntity {
    @OneToMany(mappedBy = "point")
    @JsonIgnore  // 忽略此字段的序列化
    private List<ComPointQuotaEntity> pointQuota;
    // 其他字段...
}

2. 使用 @JsonManagedReference 和 @JsonBackReference

通過注解明確父子關(guān)系,Jackson 會序列化父級(@JsonManagedReference),但忽略子級(@JsonBackReference):

// ComPointQuotaEntity.java
public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    @JsonManagedReference  // 標(biāo)記為父級(序列化)
    private ComPointEntity point;
    // 其他字段...
}
// ComPointEntity.java
public class ComPointEntity {
    @OneToMany(mappedBy = "point")
    @JsonBackReference  // 標(biāo)記為子級(不序列化)
    private List<ComPointQuotaEntity> pointQuota;
    // 其他字段...
}

3. 使用 DTO 替代直接序列化實體

創(chuàng)建 Data Transfer Object (DTO),只暴露需要的字段,避免直接序列化 JPA 實體:

// ComPointQuotaEntity.java
public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    @JsonManagedReference  // 標(biāo)記為父級(序列化)
    private ComPointEntity point;
    // 其他字段...
}
// ComPointEntity.java
public class ComPointEntity {
    @OneToMany(mappedBy = "point")
    @JsonBackReference  // 標(biāo)記為子級(不序列化)
    private List<ComPointQuotaEntity> pointQuota;
    // 其他字段...
}

4. 配置 Jackson 忽略循環(huán)引用

在 application.properties 或 application.yml 中配置 Jackson:

# application.properties
spring.jackson.serialization.fail-on-empty-beans=false
spring.jackson.serialization.fail-on-self-references=false

或在代碼中配置 ObjectMapper:

@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
    }
}

5. 自定義序列化器(高級)

為關(guān)聯(lián)字段自定義序列化邏輯,跳過循環(huán)引用:

public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    @JsonSerialize(using = ComPointEntitySerializer.class)
    private ComPointEntity point;
    // 其他字段...
}
public class ComPointEntitySerializer extends JsonSerializer<ComPointEntity> {
    @Override
    public void serialize(ComPointEntity value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value != null) {
            gen.writeStartObject();
            gen.writeNumberField("id", value.getId());
            // 僅序列化需要的字段,跳過 pointQuota
            gen.writeEndObject();
        }
    }
}

‌ 總結(jié)‌

  • 推薦方案 2(@JsonManagedReference 和 @JsonBackReference)‌:簡單且能保持雙向關(guān)聯(lián)。
    ‌>- 推薦方案 3(DTO)‌:徹底解耦序列化邏輯與數(shù)據(jù)庫實體,適合復(fù)雜場景。
  • 避免直接序列化 JPA 實體,尤其是涉及雙向關(guān)聯(lián)時。

到此這篇關(guān)于Jackson庫進行JSON 序列化時遇到了 ‌無限遞歸(Infinite Recursion)的問題及解決方案的文章就介紹到這了,更多相關(guān)Jackson JSON 序列化無限遞歸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論