如何使用Spring Data REST返回对象字段的ID?

时间:2019-12-19 12:10:07

标签: spring-data-jpa spring-data spring-data-rest

我有两个实体。

GroupParameters:

@Entity
@Data
public class GroupParameters {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String title;
    private boolean basic;
    @ManyToOne
    private TypeReport typeReport;

}

然后键入报告:

@Data
@Entity
public class TypeReport {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String title;

    @Enumerated(EnumType.STRING)
    private Standard standard;

}

当我尝试获取groupParameters时,我得到了http://localhost:8081/api/groupParameterses/31的响应:

{
  "title": "Финансовый сектор",
  "basic": false,
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/groupParameterses/31"
    },
    "groupParameters": {
      "href": "http://localhost:8081/api/groupParameterses/31"
    },
    "typeReport": {
      "href": "http://localhost:8081/api/groupParameterses/31/typeReport"
    }
  }
}

其中的typeReport是:

{
  "title": "Отчет о прибылях и убытках",
  "standard": "IFRS",
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/typeReports/27"
    },
    "typeReport": {
      "href": "http://localhost:8081/api/typeReports/27"
    }
  }
}

但是我想获得groupParameters的typeReports ID。

我想要这个结果:

{
  "title": "Финансовый сектор",
  "basic": false,
  "typeReportsId": "27", //<== I WANT THIS ===
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/groupParameterses/31"
    },
    "groupParameters": {
      "href": "http://localhost:8081/api/groupParameterses/31"
    },
    "typeReport": {
      "href": "http://localhost:8081/api/groupParameterses/31/typeReport"
    }
  }
}

如何在Spring Data REST中做到这一点?

0 个答案:

没有答案