Java对象转JSON:忽略循环 [英] Java Object to JSON: Ignore Cycle

查看:236
本文介绍了Java对象转JSON:忽略循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下签名的实体:

I've got an entity with the following signature:

public class Record extends BaseEntity<Long, Record> {...}

现在,我想使用JSON通过REST发送它.问题是,自我参考.因此,我得到以下错误.

Now I want to send it via REST using JSON. Problem is, the self reference. Because of that I get the following error.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:直接 自参考导致循环(通过参考链: 记录["this"])

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: Record["this"])

我该怎么办,例如哪个注释可以将该对象转换为JSON?

What can I do, e.g. which annotation, to convert this object to JSON?

推荐答案

您可以在BaseEntity class的通用实例变量上方使用@JsonIgnore批注,该实例变量引用记录类.它忽略了关系的某一方面,从而打破了这一链条.

You can use @JsonIgnore annotation in your BaseEntity class above the generic instance variable which is referring to record class. It ignore one of the sides of the relationship, thus breaking the chain.

或者,您可以使用@JsonManagedReference, @JsonBackReference.您可以在此处

Alternatively, you can use @JsonManagedReference, @JsonBackReference. You can read more about it here

这篇关于Java对象转JSON:忽略循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆