Java Spring boot JPA-JSON,不带嵌套对象和OneToMany关系

Java Spring boot JPA-JSON,不带嵌套对象和OneToMany关系,java,json,hibernate,orm,spring-boot,Java,Json,Hibernate,Orm,Spring Boot,我有一个项目,它处理一些对象的ORM映射(有一些@OneToMany关系等) 我使用REST接口处理这些对象,并使用SpringJPA在API中管理它们 这是我的一个POJO示例: @Entity public class Flight { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; private String dateOfDepartu

我有一个项目,它处理一些对象的ORM映射(有一些
@OneToMany
关系等)

我使用REST接口处理这些对象,并使用SpringJPA在API中管理它们

这是我的一个POJO示例:

@Entity
public class Flight {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;
  private String name;
  private String dateOfDeparture;
  private double distance;
  private double price;
  private int seats;

  @ManyToOne(fetch = FetchType.EAGER)
  private Destination fromDestination;

  @ManyToOne(fetch = FetchType.EAGER)
  private Destination toDestination;

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "flight")
  private List<Reservation> reservations;
}
我更喜欢的是,实际上指定引用对象的id,而不是它们的整个主体,如下所示:

{
  "id": 0,
  "reservations": [
    {}
  ],
  "name": "string",
  "dateOfDeparture": "string",
  "distance": 0,
  "price": 0,
  "seats": 0,
  "from": 1,
  "to": 2
}
{
    "id": 0,
    "reservations": [
        {}
    ],
    "name": "string",
    "dateOfDeparture": "string",
    "distance": 0,
    "price": 0,
    "seats": 0,
    "from": 0,
    "to": 1
}
{
    "from": 3
}

这可能吗?有人能告诉我怎么做吗?我只找到了关于如何做相反的事情的教程(我已经有了解决方案)。

您只能使用@JsonIgnore注释忽略您的JSON内容。 要隐藏在JSON中的字段,可以使用@JsonIgnore对其进行注释。 您可以这样更改JSON:

{
    "id": 0,
    "reservations": [
        {}
    ],
    "name": "string",
    "dateOfDeparture": "string",
    "distance": 0,
    "price": 0,
    "seats": 0,
    "from": {
        "id": 0
    },
    "to": {
        "id": 0
    }
}
但你不能喜欢这样:

{
  "id": 0,
  "reservations": [
    {}
  ],
  "name": "string",
  "dateOfDeparture": "string",
  "distance": 0,
  "price": 0,
  "seats": 0,
  "from": 1,
  "to": 2
}
{
    "id": 0,
    "reservations": [
        {}
    ],
    "name": "string",
    "dateOfDeparture": "string",
    "distance": 0,
    "price": 0,
    "seats": 0,
    "from": 0,
    "to": 1
}
{
    "from": 3
}
是的,这是可能的

为此,您应该对实体模型使用一对Jackson注释:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
protected Location from;  
序列化的JSON将显示以下内容:

{
    "from": {
        "id": 3,
        "description": "New-York"
    } 
}
像这样:

{
  "id": 0,
  "reservations": [
    {}
  ],
  "name": "string",
  "dateOfDeparture": "string",
  "distance": 0,
  "price": 0,
  "seats": 0,
  "from": 1,
  "to": 2
}
{
    "id": 0,
    "reservations": [
        {}
    ],
    "name": "string",
    "dateOfDeparture": "string",
    "distance": 0,
    "price": 0,
    "seats": 0,
    "from": 0,
    "to": 1
}
{
    "from": 3
}
如中所述:

@JsonIdentityReference—可用于 自定义引用对象的详细信息“对象” 已启用“标识”(请参阅)

alwaysAsId=true
用作标记,指示是否引用了所有 值将被序列化为ID(true)

注意如果使用'true'值,反序列化可能需要额外的上下文信息,并且可能需要使用自定义id 解析程序-默认处理可能不够


你可以试着发现这一点很有用——我认为第二部分并不像另一个答案所显示的那样正确。我同意@Amit Khandurit如果一个请求需要嵌套数据,而另一个请求不需要,那么这是行不通的。假设我有一家商店和一件商品。我想要一个项目名称,我有,我想检索它与夫妇的商店细节。您知道在spring boot中实现这一点的方法吗?这里需要@JsonIdentityInfo吗?