javaspringpostmandatetime-format

Pass date through postman


This is my controller:

@GetMapping("/checkMeetingRoomAvailability")
public @ResponseBody ResponseDto checkMeetingRoomAvailability(@DateTimeFormat(pattern = "dd-M-yyyy hh:mm:ss") Date begin,
        @DateTimeFormat(pattern = "dd-M-yyyy hh:mm:ss") Date end, @RequestParam Integer capacity) {
    return meetingRoomServiceLocal.checkMeetingRoomAvailability(begin, end, capacity);
}

When I pass input using postman its giving 400 bad request.

meetingRoomController/checkMeetingRoomAvailability?begin=30-9-2020 14:30:00&end=30-9-2020 15:30:00&capacity=10

I am unable to figure out why I am getting this error.


Solution

  • Either of the following solutions will work:

    1. Recommended solution: Replace dd-M-yyyy hh:mm:ss with dd-M-yyyy'T'hh:mm:ss in the code and then you can test it like meetingRoomController/checkMeetingRoomAvailability?begin=30-9-2020T14:30:00&end=30-9-2020T15:30:00&capacity=10.
    2. Encode the URL so that the space between date and time can be encoded appropriately e.g. as a test, you can encode meetingRoomController/checkMeetingRoomAvailability?begin=30-9-2020 14:30:00&end=30-9-2020 15:30:00&capacity=10using some encoding tool e.g. https://www.urlencoder.org/ and use the encoded URL in Postman. I do not recommend this as it will force your clients to encoded URL before they can send a request to your server.