【问题标题】:Recursive JSON response in Postman of an entity with OneToMany relationshipPostman 中具有 OneToMany 关系的实体的递归 JSON 响应
【发布时间】:2019-07-13 10:50:11
【问题描述】:

我有一个 Client 类,其中包含 OneToMany 关系中的 Cars 列表。当我尝试使用 Postman 获取所有客户端时,第一个客户端将在响应中递归打印。如何在不从 Car 响应中获取客户端的情况下,通过客户端及其相应的汽车列表获取 JSON 响应?

汽车类

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String model;
private String color;

@ManyToOne(fetch = FetchType.LAZY)
private Client client;

客户端类

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private String name;

private int age;

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "client")
private List<Car> carList;

带有 Postman 响应的图片

【问题讨论】:

  • 这取决于您使用的 Json 序列化库。

标签: java json spring hibernate postman


【解决方案1】:

假设您使用 Jackson 序列化为 JSON ,您可以使用 @JsonIgnoreProperties 打破循环:

汽车:

@Entity
@Table(name= "Car")
public class Car {

    [.....]
    @JsonIgnoreProperties("carList")
    private Client client;
    [...]
}   

客户:

@Entity
@Table(name="client")
public class Client {

    [....]
    @JsonIgnoreProperties("client")
    private List<Car> carList;
    [...]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多