Java 如何在子级中显示实体细节而不引起有限递归
我试图从SpringRepo中获取json,但在客户端,我们需要在子级本身中包含父级详细信息 示例:(我得到的) 示例:(我想要的)Java 如何在子级中显示实体细节而不引起有限递归,java,recursion,jackson,spring-data,Java,Recursion,Jackson,Spring Data,我试图从SpringRepo中获取json,但在客户端,我们需要在子级本身中包含父级详细信息 示例:(我得到的) 示例:(我想要的) 是否可以在不引起无限递归的情况下执行此操作?(最好通过注释)选项1:您可以使用 @JsonIdentityInfo(generator = PropertyGenerator.class, property = "id") 在您的汽车上和附加实体上 选项2: 使用@JsonManagedReference、@JsonBackReference @JsonMan
是否可以在不引起无限递归的情况下执行此操作?(最好通过注释)
选项1:
您可以使用
@JsonIdentityInfo(generator = PropertyGenerator.class, property = "id")
在您的汽车上
和附加实体上
选项2:
使用@JsonManagedReference、@JsonBackReference
@JsonManagedReference part of reference that gets serialized normally.
@JsonBackReference part of reference that will be omitted from serialization.
选项3:
@JsonManagedReference part of reference that gets serialized normally.
@JsonBackReference part of reference that will be omitted from serialization.
您可以使用@JsonIgnore
注释忽略关系的一方,从而打破链条
有关更多信息,我最后介绍了以下解决方法
在Extras
类中:
@JsonProperty("cardetail")
public Car getCarDetail(){
Car _car = new Car();
// (...)Clone of the original Object, ignoring the "extras"
return _car;
}
这几乎就是我想要的,但名字不同
@JsonProperty("cardetail")
public Car getCarDetail(){
Car _car = new Car();
// (...)Clone of the original Object, ignoring the "extras"
return _car;
}