Jackson庫進行JSON?序列化時遇到了無限遞歸(Infinite?Recursion)的問題及解決方案
使用 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)文章希望大家以后多多支持腳本之家!
- Jackson使用示例-Bean、XML、Json之間相互轉(zhuǎn)換
- 一篇文章了解Jackson注解@JsonFormat及失效解決辦法
- Java中對象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
- 利用Jackson解決Json序列化和反序列化問題
- Java利用Jackson輕松處理JSON序列化與反序列化
- Jackson中json格式的字符串與對象的互相轉(zhuǎn)換方式
- 如何自定義Jackson序列化?@JsonSerialize
- JSON中fastjson、jackson、gson如何選擇
- jackson 如何將實體轉(zhuǎn)json json字符串轉(zhuǎn)實體
- 使用jackson實現(xiàn)對象json之間的相互轉(zhuǎn)換(spring boot)
- 使用Jackson-json解析一個嵌套的json字符串
相關(guān)文章
Java實現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle 的相關(guān)資料,需要的朋友可以參考下2016-06-06java 通過cmd 調(diào)用命令啟動tomcat的操作
這篇文章主要介紹了java 通過cmd 調(diào)用命令啟動tomcat的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11idea每次新打開的項目窗口maven都要重新設(shè)置問題
這篇文章主要介紹了idea每次新打開的項目窗口maven都要重新設(shè)置問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11java.lang.ExceptionInInitializerError初始化程序中的異常錯誤的解決
java.lang.ExceptionInInitializerError?異常在?Java?中表示一個錯誤,該錯誤發(fā)生在嘗試初始化一個類的靜態(tài)變量、靜態(tài)代碼塊或枚舉常量時,本文就來介紹并解決一下,感興趣的可以了解一下2024-05-05Spring Bean生命周期之BeanDefinition的合并過程詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之BeanDefinition的合并過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03