I want my controllers to parse dates in RequestParams and PathVariables according to a standard format.
Is it possible to set an application-wide @DateTimeFormat without annotating every @RequestParam individually?
I want my controllers to parse dates in RequestParams and PathVariables according to a standard format.
Is it possible to set an application-wide @DateTimeFormat without annotating every @RequestParam individually?
You can do it at the controller level. Add @initBinder in your controller and it will format the date according to the given formatter.
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
// true passed to CustomDateEditor constructor means convert empty String to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
It might be possible by Spring AOP but how would you tell your code if you have different keys for date in request. but check following link for more derails: