I register a DateTimeFormatterRegistrar in my class implements WebMvcConfigurer like this:
@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
registrar.registerFormatters(registry);
}
In the rest controller, i try to parse client GET params to an object:
@GetMapping("/api/url/path")
public APIResponse getPersonAttendList(@Valid SampleVO vo){}
SampleVO include field LocalDateTime time. If client offered wrong format of time param, the binding will be failed. Server will return 500, and print some log like this:
>ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] 175 - Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
nested exception is java.time.format.DateTimeParseException
My question is, how to catch this exception, and return 400 to the client? It seems ControllerAdvice is not working.