Want to include relationships in Spring REST API

非 Y 不嫁゛ 提交于 2020-01-05 14:10:10

问题


I am new in Spring.

I have three entity Invoice, Line and Product.

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

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

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

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

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

I want output like

{
    "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"
        }
    ]
}

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


回答1:


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

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.

UPDATE:

Data Transfer Object for Invoice:

public class InvoiceDTO {

    private Integer id;
    private String date;

    // getter and setter methods
}

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;

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



来源:https://stackoverflow.com/questions/33471797/want-to-include-relationships-in-spring-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!