Reputation: 3350
In my request handler method in web mvc I have done something like this:
@RequestMapping(value = "/{day}", method = RequestMethod.GET)
@ResponseBody
public String dateValidation(
@PathVariable @DateTimeFormat(pattern = "dd-mm-yyyy") Date day) {
System.out.println("date is " + day);
return "date";
}
As per the spring docs says here
it will automatically convert String to Date type.
Why I need to add Initbinder in my controller as
@InitBinder
public void initBinder(WebDataBinder binder) {
// Although spring says it is automatic
binder.addCustomFormatter(new DateFormatter("dd-mm-yyyy"));
}
Am i missing something ?
Thanks
Upvotes: 1
Views: 846
Reputation: 27496
Because you are passing date in the custom pattern so the Spring don't know how to parse it to the correct date object. So that's why you need to add custom formatter, that will Spring use to convert your pattern into the Date
object.
Upvotes: 1