亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Jackson庫(kù)進(jìn)行JSON?序列化時(shí)遇到了無(wú)限遞歸(Infinite?Recursion)的問(wèn)題及解決方案

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

使用 Jackson 庫(kù)進(jìn)行 JSON 序列化時(shí)遇到了 ‌無(wú)限遞歸(Infinite Recursion)‌ 問(wèn)題,這是因?yàn)閮蓚€(gè)實(shí)體類 ComPointQuotaEntity 和 ComPointEntity 之間存在雙向關(guān)聯(lián)(point 和 pointQuota 相互引用),導(dǎo)致序列化時(shí)陷入死循環(huán)。以下是解決方案:

解決方案‌

1. 使用 @JsonIgnore 忽略一個(gè)方向的引用

在其中一個(gè)實(shí)體類的關(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ò)注解明確父子關(guān)系,Jackson 會(huì)序列化父級(jí)(@JsonManagedReference),但忽略子級(jí)(@JsonBackReference):

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

3. 使用 DTO 替代直接序列化實(shí)體

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

// ComPointQuotaEntity.java
public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    @JsonManagedReference  // 標(biāo)記為父級(jí)(序列化)
    private ComPointEntity point;
    // 其他字段...
}
// ComPointEntity.java
public class ComPointEntity {
    @OneToMany(mappedBy = "point")
    @JsonBackReference  // 標(biāo)記為子級(jí)(不序列化)
    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. 自定義序列化器(高級(jí))

為關(guān)聯(lián)字段自定義序列化邏輯,跳過(guò)循環(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());
            // 僅序列化需要的字段,跳過(guò) pointQuota
            gen.writeEndObject();
        }
    }
}

‌ 總結(jié)‌

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

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

相關(guān)文章

最新評(píng)論