【问题标题】:Want to include relationships in Spring REST API想要在 Spring REST API 中包含关系
【发布时间】:2015-11-02 06:20:19
【问题描述】:

我是春天的新手。

我有三个实体Invoice, Line and Product

(1) 如果我打电话给GET /invoices,那么我想要像这样的输出,

{
    "invoices": [
        {
            "id": "101",
            "date": "2013-02-14"
        },
        {
            "id": "102",
            "date": "2013-02-18"
        }
    ]
}

我已经这样做了,我可以这样做,但无法在获取请求中包含关系。

(2) 我想 GET 调用类似这样的包含关系的东西。

GET /invoices?include=invoice.lines:embed,invoice_line.product:sideload

我想要像这样的输出

{
    "invoices": [
        {
            "id": "101",
            "date": "2013-02-14",
            "lines": [
                {
                    "id": "201",
                    "product_id": "301",
                    "amount": 100
                },
                {
                    "id": "201",
                    "product_id": "302",
                    "amount": 100
                }
            ]
        },
        {
            "id": "102",
            "date": "2013-02-18",
            "lines": [
                {
                    "id": "203",
                    "product_id": "301",
                    "amount": 100
                }
            ]
        }
    ],
    "products": [
        {
            "id": "301",
            "name": "Ice Cream"
        },
        {
            "id": "302",
            "name": "Waffles"
        }
    ]
}

我陷入了(2)包含关系,我想实现这样的目标 需要帮忙。 提前致谢。

【问题讨论】:

  • 最好的方法是在你的控制器中处理这个问题,使用@RequestParam('include'),然后用你想要的正确格式发送一个响应

标签: java hibernate spring-mvc spring-boot


【解决方案1】:

你可以通过使用hibernate注解来做到这一点。

您的 Invoice 实体类应如下所示:

@Entity
@Table(name = "invoices") // your table name
public class Invoice implements java.io.Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "date")
    private String date;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "invoices")
    private Set<Line> line = new HashSet<Line>(0);

    // getter and setter method

您的 Line 实体应如下所示:

@Entity
@Table(name = "lines")
public class Line implements java.io.Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "id", nullable = false) // Invoice id
    private Invoice invoice;

    // product_id and amount and getter and setter method

并确保您的数据库中有正确的关系。 您可以查看here 以获得完整的教程。

希望对您有所帮助并祝您编码愉快。

更新:

发票数据传输对象:

public class InvoiceDTO {

    private Integer id;
    private String date;

    // getter and setter methods
}

然后创建另一个返回 InvoiceDTO 的服务:

    Invoice invEntity = em.find(Invoice.class, id);
    InvoiceDTO invDTO = new InvoiceDTO();
    invDTO.setId(invEntity.getId());
    invDTO.setDate(invEntity.Date());

    return invDTO;

然后,当您需要时,您可以在关系中返回 Invoice,也可以返回 InvoiceDTO 对象。

【讨论】:

  • 我相信这会导致关系总是转换为 JSON,而不是可选地基于输入参数
  • @user3422401 - 每次我调用 GET /invoices 时它总是返回发票和行,我不想要这个我希望它由 URL 驱动,当我发送包含行时,只有它应该返回这个.
  • @AnshulLonkar 您可以创建另一个模型,但不能创建与调用 InvoiceDTO 的 Invoice 相同的实体。然后,您可以创建另一个服务,该服务返回此 InvoiceDTO 而不是关系中的 Invoice 实体。
猜你喜欢
  • 1970-01-01
  • 2021-04-13
  • 2017-07-28
  • 2018-04-28
  • 2019-10-20
  • 2021-08-07
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多