如何在不引起无限递归的情况下显示子级内部的实体细节



我试图从spring repo中获取json,但在客户端,我们需要子级内部的父级详细信息。

示例:(我得到的(

car:{
    make:'volvo',
    color:'blue',
    hp:225,
    extras:{
       //(...)
    }
}

示例:(我想要什么(

car:{
    make:'volvo',
    color:'blue',
    hp:225,
    extras:{
        //(...)
        car:{
             // car to which extras belong, but without the "extras" property again
        }
    }
}

有可能做到这一点而不引起无限递归吗?(最好通过注释(

OPTION 1:您可以使用

@JsonIdentityInfo(generator = PropertyGenerator.class, property = "id")

CarExtras实体上。

Option 2:使用@JsonManagedReference, @JsonBackReference

 @JsonManagedReference  part of reference that gets serialized normally.
 @JsonBackReference part of reference that will be omitted from serialization.

Option 3:

您可以使用@JsonIgnore注释来简单地忽略关系的一侧,从而断开链。

欲了解更多信息,请访问此处。

我最终找到了以下解决方法。

Extras类中:

@JsonProperty("cardetail")
public Car getCarDetail(){
    Car _car = new Car();
    // (...)Clone of the original Object, ignoring the "extras"
    return _car;
}

这几乎得到了我想要的结果,但使用了不同的名称

相关内容

最新更新