当前位置: 首页 > news >正文

Jackson 库进行 JSON 序列化时遇到了 ‌无限递归(Infinite Recursion)‌ 问题

使用 Jackson 库进行 JSON 序列化时遇到了 ‌无限递归(Infinite Recursion)‌ 问题,这是因为两个实体类 ComPointQuotaEntity 和 ComPointEntity 之间存在双向关联(point 和 pointQuota 相互引用),导致序列化时陷入死循环。以下是解决方案:

解决方案‌

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

在其中一个实体类的关联字段上添加 @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

通过注解明确父子关系,Jackson 会序列化父级(@JsonManagedReference),但忽略子级(@JsonBackReference):

// ComPointQuotaEntity.java
public class ComPointQuotaEntity {
    @ManyToOne
    @JoinColumn(name = "point_id")
    @JsonManagedReference  // 标记为父级(序列化)
    private ComPointEntity point;
    // 其他字段...
}

// ComPointEntity.java
public class ComPointEntity {
    @OneToMany(mappedBy = "point")
    @JsonBackReference  // 标记为子级(不序列化)
    private List<ComPointQuotaEntity> pointQuota;
    // 其他字段...
}

3. 使用 DTO 替代直接序列化实体

创建 Data Transfer Object (DTO),只暴露需要的字段,避免直接序列化 JPA 实体:

public class ComPointQuotaDTO {
    private Long id;
    private ComPointDTO point;  // 避免双向引用
    // 其他字段...
}

public class ComPointDTO {
    private Long id;
    // 不包含 pointQuota 字段
    // 其他字段...
}

4. 配置 Jackson 忽略循环引用

在 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. 自定义序列化器(高级)

为关联字段自定义序列化逻辑,跳过循环引用:

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();
        }
    }
}

总结‌

  • 推荐方案 2(@JsonManagedReference 和 @JsonBackReference)‌:简单且能保持双向关联。
    ‌>- 推荐方案 3(DTO)‌:彻底解耦序列化逻辑与数据库实体,适合复杂场景。
  • 避免直接序列化 JPA 实体,尤其是涉及双向关联时。

相关文章:

  • 嵌入式笔记 | 正点原子STM32F103ZET6 5 | 串口通信
  • kafka的文章
  • C#从入门到精通(1)
  • 路由Vue Router基本用法
  • QEMU 中 x86_cpu_realizefn 到 ept_emulation_fault 的调用流程解析(macos)
  • 数据库的两种模式
  • 国内首家,百度智能云千帆AppBuilder全面兼容MCP协议
  • dfs(二十一)46. 全排列 中等
  • 【Linux】信号:产生信号
  • 夜莺监控 v8.0 新版通知规则 | 对接飞书告警
  • 【数据分析】数据筛选与访问行列元素3
  • VLLM专题(十九)—兼容 OpenAI 的服务器
  • [极客大挑战 2019]Http_3.19BUUCTF练习day3(1)
  • 聚类算法api初步使用
  • ArcGIS10.X影像智能下载!迁移ArcGIS Pro批量智能高清影像下载工具至ArcGIS!
  • 音频进阶学习二十——DFT离散傅里叶变换
  • html相关常用语法
  • 【docker】--- 详解 WSL2 中的 Ubuntu 和 Docker Desktop 的区别和关系!
  • springboot Actuator 指标分析
  • 在 Kubernetes(k8s)部署过程中常见的问题
  • 来论|建设性推进缅北和平进程——中国的智慧与担当
  • 人民日报整版调查:中小学春秋假,如何放得好推得开?
  • 盖茨:20年内将捐出几乎全部财富,盖茨基金会2045年关闭
  • 咖啡戏剧节举办第五年,上生新所“无店不咖啡,空间皆可戏”
  • 从“重规模”向“重回报”转变,公募基金迎系统性改革
  • 一揽子十条货币政策措施出炉:降准降息,设立五千亿服务消费与养老再贷款