我有一個 spring 啟動程式,現在我的 PutMapping 功能似乎沒有按預期作業。現在,如果我在郵遞員中更新該欄位,它會顯示沒有錯誤,但是當我檢查更新是否發生時,則沒有任何更改。
控制器:
@PutMapping(path = "{bookingId}")
public void updateBooking (
@PathVariable("bookingId") Long bookingId,
@RequestParam(required = false) Integer tickets ) {
bookingService.updateBooking(bookingId, tickets);
}
服務:
public void updateBooking(Long bookingId, Integer tickets) {
// checks if event id exists
Booking booking = bookingRepository.findById(bookingId).orElseThrow(() -> new IllegalStateException(
"Booking with id " bookingId " does not exists. "));
if (tickets != null && !Objects.equals(booking.getTickets(), tickets)) {
booking.setTickets(tickets);
booking.setAmount(booking.getAmount());
}
else {
throw new IllegalStateException(
"The update was unsuccessful" );
}
}
模型:
public Integer getTickets() {
return tickets;
}
public void setTickets(Integer tickets) {
this.tickets = tickets;
}
uj5u.com熱心網友回復:
您沒有在資料庫中保存更新的物件。
public void updateBooking(Long bookingId, Integer tickets) {
// checks if event id exists
Booking booking = bookingRepository.findById(bookingId).orElseThrow(() -> new IllegalStateException(
"Booking with id " bookingId " does not exists. "));
if (tickets != null && !Objects.equals(booking.getTickets(), tickets)) {
booking.setTickets(tickets);
booking.setAmount(booking.getAmount());
// save the updated booking in the database
bookingRepository.save(booking)
}
else {
throw new IllegalStateException(
"The update was unsuccessful" );
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368438.html
