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

电商网站怎么推广免费广告推广平台

电商网站怎么推广,免费广告推广平台,自己做衣服网站,大连建设工业产品网站使用 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/185416.html

相关文章:

  • 网站开发有什么职位seo推广软件代理
  • 网站后台做链接aso100官网
  • 惠州建设网站公司温州seo外包公司
  • 做智能网站平台企业网站推广优化公司
  • 网站怎么做播放窗口百度浏览器网页
  • 织梦网站后台logo删除凡科建站代理
  • 深圳企业网站建设搜索引擎优化是什么意思
  • 广州市建设注册中心网站武汉seo网站优化技巧
  • 校园网站怎么做无锡网站建设公司
  • 做网站实验体会谷歌seo排名优化
  • 2个女人做暧暧网站班级优化大师使用指南
  • 网站建设维护的方案推广公司产品
  • 腾讯云 wordpress建站百度站长平台快速收录
  • wordpress怎么弄网站网站怎么被收录
  • 香港网站建设展览怎么制作个人网页
  • 有专门做英文字幕的网站吗模板免费下载网站
  • 滨州内做网站系统的公司站长之家权重查询
  • 建设企业网站对公廊坊网络推广优化公司
  • 公司网站兰州建设需要多少钱提高网站流量的软文案例
  • 嘉善在线做网站吗合肥网站建设优化
  • 福州网站开发私人公司管理培训课程大全
  • 做网站的公司成本谷歌浏览器下载手机版官网中文
  • 网站风格介绍适合女生去的培训机构
  • 只有一个人网站开发商品标题seo是什么意思
  • 企业网站 appseo做的好的网站
  • 设计感网站买号链接
  • 重庆工厂网站建设适合口碑营销的产品
  • 网站建设解决方网站seo排名
  • 雅虎做网站推广英文网站设计公司
  • 湘潭做网站选择磐石网络网址大全导航