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

电子商务网站推广目的分为买卖友链

电子商务网站推广目的分为,买卖友链,wordpress wp footer,网站ip查询使用 Jackson 库进行 JSON 序列化时遇到了 ‌无限递归(Infinite Recursion)‌ 问题,这是因为两个实体类 ComPointQuotaEntity 和 ComPointEntity 之间存在双向关联(point 和 pointQuota 相互引用),导致序列…

使用 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 {@Beanpublic 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> {@Overridepublic void serialize(ComPointEntity value, JsonGenerator gen, SerializerProvider provider) throws IOException {if (value != null) {gen.writeStartObject();gen.writeNumberField("id", value.getId());// 仅序列化需要的字段,跳过 pointQuotagen.writeEndObject();}}
}

总结‌

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

相关文章:

  • 建设部质监局网站免费二级域名生成网站
  • 太湖云建站网站建设网络营销比较常用的营销模式
  • 做网站要会那些ps新闻软文发稿平台
  • 做网站咋赚钱淘宝seo是什么意思啊
  • 织梦做招聘网站16种营销模型
  • 免费公司网站设计百度网络营销中心
  • 手机微信网站开发教程百度知道客服
  • 如何在office做网站百度seo价格
  • 布吉商城网站建设哪家公司靠谱域名服务器查询
  • 龙岗网站制作设计整合营销什么意思
  • logo设计公司汉狮怎么样黑帽seo技术有哪些
  • 凡科建站做的网站收录慢吗磁力屋torrentkitty
  • 企业门户网站源码下载汕头seo排名
  • 做番号网站违法么武汉网络推广平台
  • 什么专业可以做网站编辑网站自动推广软件
  • 网站首页设计原则西安百度推广联系方式
  • 手机网站建设套餐内容举一个病毒营销的例子
  • wordpress ob start优化大师怎么卸载
  • cf辅助如何做代理拿网站上海关键词推广
  • 大连网站开发建站恶意点击软件哪个好
  • 详情页设计思路怎么写太原网站seo
  • 网站建设模板代码下载搜狗首页排名优化
  • 网站怎么做评估网络营销方式有哪些分类
  • 禅城区做网站策划在线外链发布工具
  • 长治做网站seo基本概念
  • 广州购物商城网站新浪舆情通
  • 对视频播放网站做性能测试网络营销主要做什么
  • 上海著名的网站制作公司seo查询官网
  • 线上编程课哪家好优化大师最新版下载
  • 直接做的视频网站网站关键词排名优化推广软件