I am building a web application based on hotel room booking and testing my REST API through postman and this @Future annotation from import jakarta.validation.constraints.Future; is giving me 500 response, while I tried to hit the link mentioned in the screenshot,I tried again by removing this @Future
annotation and it is working perfectly, so please tell me if there is any alternative (for validating future checkOutDate should be greater than checkInDate) or solution for this issue.
I have used this @Future
over one variable of entity, below is the code.
Booking.java
<code>package com.Yash.Astoria.entities;
import jakarta.validation.constraints.Future;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
@Table(name = "bookings")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull(message = "CheckIn Date is required")
private LocalDate checkInDate;
@Future(message = "check out date must be in the future")
private LocalDate checkOutDate;
@Min(value = 1, message = "Atleast 1 adult should be selected")
@Min(value = 0, message = "Number of Childrens should not be less then 0")
private int numOfChildren;
private int totalNumOfGuests;
private String bookingConfirmationCode;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
public void getTotalNumberOfGuests(){
this.totalNumOfGuests = this.numOfAdults + this.numOfChildren;
public void setNumOfAdults(int numOfAdults) {
this.numOfAdults = numOfAdults;
getTotalNumberOfGuests();
public void setNumOfChildren(int numOfChildren) {
this.numOfChildren = numOfChildren;
getTotalNumberOfGuests();
public String toString() {
", checkInDate=" + checkInDate +
", checkOutDate=" + checkOutDate +
", numOfAdults=" + numOfAdults +
", numOfChildren=" + numOfChildren +
", totalNumOfGuests=" + totalNumOfGuests +
", bookingConfirmationCode='" + bookingConfirmationCode + ''' +
<code>package com.Yash.Astoria.entities;
import jakarta.validation.constraints.Future;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.time.LocalDate;
@Data
@Entity
@Table(name = "bookings")
public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "CheckIn Date is required")
private LocalDate checkInDate;
@Future(message = "check out date must be in the future")
private LocalDate checkOutDate;
@Min(value = 1, message = "Atleast 1 adult should be selected")
private int numOfAdults;
@Min(value = 0, message = "Number of Childrens should not be less then 0")
private int numOfChildren;
private int totalNumOfGuests;
private String bookingConfirmationCode;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
private Room room;
public void getTotalNumberOfGuests(){
this.totalNumOfGuests = this.numOfAdults + this.numOfChildren;
}
public void setNumOfAdults(int numOfAdults) {
this.numOfAdults = numOfAdults;
getTotalNumberOfGuests();
}
public void setNumOfChildren(int numOfChildren) {
this.numOfChildren = numOfChildren;
getTotalNumberOfGuests();
}
@Override
public String toString() {
return "Booking{" +
"id=" + id +
", checkInDate=" + checkInDate +
", checkOutDate=" + checkOutDate +
", numOfAdults=" + numOfAdults +
", numOfChildren=" + numOfChildren +
", totalNumOfGuests=" + totalNumOfGuests +
", bookingConfirmationCode='" + bookingConfirmationCode + ''' +
'}';
}
}
</code>
package com.Yash.Astoria.entities;
import jakarta.validation.constraints.Future;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.time.LocalDate;
@Data
@Entity
@Table(name = "bookings")
public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "CheckIn Date is required")
private LocalDate checkInDate;
@Future(message = "check out date must be in the future")
private LocalDate checkOutDate;
@Min(value = 1, message = "Atleast 1 adult should be selected")
private int numOfAdults;
@Min(value = 0, message = "Number of Childrens should not be less then 0")
private int numOfChildren;
private int totalNumOfGuests;
private String bookingConfirmationCode;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
private Room room;
public void getTotalNumberOfGuests(){
this.totalNumOfGuests = this.numOfAdults + this.numOfChildren;
}
public void setNumOfAdults(int numOfAdults) {
this.numOfAdults = numOfAdults;
getTotalNumberOfGuests();
}
public void setNumOfChildren(int numOfChildren) {
this.numOfChildren = numOfChildren;
getTotalNumberOfGuests();
}
@Override
public String toString() {
return "Booking{" +
"id=" + id +
", checkInDate=" + checkInDate +
", checkOutDate=" + checkOutDate +
", numOfAdults=" + numOfAdults +
", numOfChildren=" + numOfChildren +
", totalNumOfGuests=" + totalNumOfGuests +
", bookingConfirmationCode='" + bookingConfirmationCode + ''' +
'}';
}
}
BookingController.java
<code>package com.Yash.Astoria.controllers;
import com.Yash.Astoria.dto.Response;
import com.Yash.Astoria.entities.Booking;
import com.Yash.Astoria.services.Interface.IBookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/bookings")
public class BookingController {
private IBookingService bookingService;
@PostMapping("/book-room/{roomId}/{userId}")
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> saveBookings(@PathVariable Long roomId,
@PathVariable Long userId,
@RequestBody Booking bookingRequest){
Response response = bookingService.saveBooking(roomId, userId, bookingRequest);
return ResponseEntity.status(response.getStatusCode()).body(response);
@PreAuthorize("hasAuthority('ADMIN')")
public ResponseEntity<Response> getAllBookings(){
Response response = bookingService.getAllBookings();
return ResponseEntity.status(response.getStatusCode()).body(response);
@GetMapping("/get-by-confirmation-code/{confirmationCode}")
public ResponseEntity<Response> getBookingByConfirmationCode(@PathVariable String confirmationCode){
Response response = bookingService.findBookingByConfirmationCode(confirmationCode);
return ResponseEntity.status(response.getStatusCode()).body(response);
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> cancelBooking(@PathVariable Long bookingId){
Response response = bookingService.cancelBooking(bookingId);
return ResponseEntity.status(response.getStatusCode()).body(response);
<code>package com.Yash.Astoria.controllers;
import com.Yash.Astoria.dto.Response;
import com.Yash.Astoria.entities.Booking;
import com.Yash.Astoria.services.Interface.IBookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/bookings")
public class BookingController {
@Autowired
private IBookingService bookingService;
@PostMapping("/book-room/{roomId}/{userId}")
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> saveBookings(@PathVariable Long roomId,
@PathVariable Long userId,
@RequestBody Booking bookingRequest){
Response response = bookingService.saveBooking(roomId, userId, bookingRequest);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@GetMapping("/all")
@PreAuthorize("hasAuthority('ADMIN')")
public ResponseEntity<Response> getAllBookings(){
Response response = bookingService.getAllBookings();
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@GetMapping("/get-by-confirmation-code/{confirmationCode}")
public ResponseEntity<Response> getBookingByConfirmationCode(@PathVariable String confirmationCode){
Response response = bookingService.findBookingByConfirmationCode(confirmationCode);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@DeleteMapping
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> cancelBooking(@PathVariable Long bookingId){
Response response = bookingService.cancelBooking(bookingId);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
}
</code>
package com.Yash.Astoria.controllers;
import com.Yash.Astoria.dto.Response;
import com.Yash.Astoria.entities.Booking;
import com.Yash.Astoria.services.Interface.IBookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/bookings")
public class BookingController {
@Autowired
private IBookingService bookingService;
@PostMapping("/book-room/{roomId}/{userId}")
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> saveBookings(@PathVariable Long roomId,
@PathVariable Long userId,
@RequestBody Booking bookingRequest){
Response response = bookingService.saveBooking(roomId, userId, bookingRequest);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@GetMapping("/all")
@PreAuthorize("hasAuthority('ADMIN')")
public ResponseEntity<Response> getAllBookings(){
Response response = bookingService.getAllBookings();
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@GetMapping("/get-by-confirmation-code/{confirmationCode}")
public ResponseEntity<Response> getBookingByConfirmationCode(@PathVariable String confirmationCode){
Response response = bookingService.findBookingByConfirmationCode(confirmationCode);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
@DeleteMapping
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
public ResponseEntity<Response> cancelBooking(@PathVariable Long bookingId){
Response response = bookingService.cancelBooking(bookingId);
return ResponseEntity.status(response.getStatusCode()).body(response);
}
}
postman screenshot 500 internal server error
Expectation: validating future checkOutDate should be greater than
checkInDate
Also, I have tried @FutureOrPresent
annotation, and it give the same error