在带有 spring-boot 应用程序的 JSON POST 请求中使用 id 而不是完整实体

问题描述 投票:0回答:0

我创建了一个 spring-boot rest 服务,它有 post 方法

@PostMapping("/")
public ResponseEntity<Order> createOrder(@RequestBody Order order) {

订单实体看起来像

@Getter
@Setter
@Entity
@Table(name = "order")
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private long id;
    @ManyToOne
    @JoinColumn(name="destination")
    private Address destination;
    private String receiverLastName;
    private String receiverPhone;
    private float weight;
    private int height;
    private int length;
    private int width;
    private String description;
    @Enumerated(EnumType.STRING)
    private Status status;
    @ManyToOne
    @JoinColumn(name="courier")
    private User courier;
    @ManyToOne
    @JoinColumn(name="customer")
    private User customer;
}

如果我尝试发布这个 json:

{
    "destination":1,
    "receiverLastName": "Nieman",
    "receiverPhone": "+1145127789",
    "weight": 3.25,
    "height": 75,
    "length": 15,
    "width": 20,
    "description": "very fragile",
    "status": "CREATED",
    "courier":2,
    "customer":3
}

我收到错误的请求错误。例如,我需要使用完整的实体,而不是“目的地”:1。那么,是否可以使用仅包含 id 的“扁平”json 而不是包含整个字段集的完整实体?

json spring-boot rest post
© www.soinside.com 2019 - 2024. All rights reserved.