I have controller that is accepting the request with LocalDateTime
as query parameter
@GetMapping("test")
public void test(@RequestParam(value = "date", required = false, defaultValue = ?) LocalDateTime date) {
System.out.println("The date is : "+date);
}
I know we can set the default value for String
and Integer
using defaultValue
in @RequestParam
, but how to set the default value for LocalDateTime
?
LocalDateTime is an immutable date-time object that represents a date-time with default format as yyyy-MM-dd-HH-mm-ss.
The easiest way to create an instance of the LocalDateTime class is by using the factory method of(), which accepts year, month, day, hour, minute, and second to create an instance of this class. A shortcut to create an instance of this class is by using atDate() and atTime() method of LocalDate and LocalTime class.
By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision.
You can use the spring expression language to set the default value for any object
@GetMapping("test")
public void test(@RequestParam(value = "date", required = false, defaultValue = "#{T(java.time.LocalDateTime).now()}") LocalDateTime date) {
System.out.println("The date is : " + date);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With