I have a controller in spring boot application, which validate the input using Hibernate Validator. When ever I call the rest endpoint, it validate correctly, but my controller advice doesn't catch it, to customise the message. We only get 404 code with empty payload.
@RestController
public class Controller {
@RequestMapping(method = RequestMethod.POST, path = RestPaths.LOAD_DATA)
public void loadCostCenterData(@RequestBody @Valid ClientDto dto) {
}
}
@RestControllerAdvice
public class WickesGlobalExceptionMapper extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity handleOtherUnexpectedException(Exception ex, WebRequest request) {
}
}
Here is a working ControllerAdvise that uses ModelView instead of ResponseEntity:
@ControllerAdvice
public class WickesGlobalExceptionMapper {
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleInvalidArgument(IllegalArgumentException ex) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setView(new MappingJackson2JsonView());
modelAndView.addObject("errorMessage", format("{0}", errorMessage));
return modelAndView;
}
}
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