Spring MVC - 使用 PUT 更新控制器中的实体

Spring MVC - Update entity in controller with PUT

我有以下实体:

@Entity
public class Booking {
    @Id
    @GeneratedValue
    private int id;
    @OneToOne
    private Price pricePerDay;
    private Date startDate;
    private Date endDate;
    @OneToOne
    private Room room;
    @OneToOne
    private Customer customer;



@Entity
public class Customer {
    @Id
    @GeneratedValue
    private int id;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;
    @OneToOne
    private CustomerAddress customerAddress;
    private PaymentMethod paymentMethod;

@Entity
public class Room {
    @Id
    @GeneratedValue
    private int roomNumber;
    private RoomType roomType;
    private BedType bedType;
    private int bedNumber = 1;
    private boolean oceanView = false;
    private boolean airConditioning = false;
    private boolean balcony = false;
    @OneToOne
    private Price pricePerNight;

我有 update(Booking booking) 的存储库和服务,它根据给定的 ID 更新预订。(假设代码没问题)

请帮助我构建用于更新预订中房间的控制器,方法是通过 id 调用预订:

@RequestMapping(value = "/bookings/update/{id}", method = RequestMethod.PUT)

如果需要更多信息,我会提供。 提前谢谢你。

下面的代码是一个示例控制器方法。

   @RequestMapping(value = "/booking/{id}", method = RequestMethod.PUT)
public String list(@PathVariable String id){
     Booking booking= bookingRepository.findOne(id);

    //code

      bookingService.update(booking);
}

RequestMethod.PUT 表示我们正在更新现有预订,因此我们不需要在路径中提供 update