想要在Spring REST API中包含关系 [英] Want to include relationships in Spring REST API

查看:74
本文介绍了想要在Spring REST API中包含关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是春季新人.

我有三个实体Invoice, Line and Product.

(1)如果我呼叫GET /invoices,那么我想要输出类似

(1) If I call GET /invoices then I want output like,

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

我已经做到了,我能够做到,但是不能在get请求中包含关系.

I have done this, I am able to do this but not able to include relations in the get request.

(2)我想GET这样的包含关系的东西.

(2) I want to GET call something like this which include relations.

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)包含关系中,我想实现这样的目标 需要帮忙. 预先感谢.

I got stuck in (2) include relationship, I want to achieve something like this need help. Thanks in advance.

推荐答案

您可以通过使用休眠注释来做到这一点.

You can do it by using hibernate annotations.

您的发票实体类应如下所示:

Your Invoice entity class should look something like this:

@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实体应如下所示:

And your Line entity should look something like this:

@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

并确保您在数据库中具有正确的关系. 您可以此处完整的教程.

And make sure you have correct relation in your database. You can take a look here for a fully tutorial.

希望它对您有所帮助,并祝您编程愉快.

Hope it help and happy coding.

更新:

发票的数据传输对象:

public class InvoiceDTO {

    private Integer id;
    private String date;

    // getter and setter methods
}

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

And then create another service that return InvoiceDTO:

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

    return invDTO;

然后,在需要时可以返回关联内的发票,也可以返回InvoiceDTO对象.

Then when you need you can return your Invoice within relation or you can return InvoiceDTO object.

这篇关于想要在Spring REST API中包含关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆