I am using Spring Boot 3.0.2 and I have implemented @ControllerAdvice to handle exceptions. This is my code for validtion errors:
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
List<ExceptionDescription> errors = new ArrayList<>();
for (ObjectError err : ex.getBindingResult().getAllErrors()) {
if (err instanceof FieldError) {
FieldError fieldError = (FieldError) err;
errors.add(new ExceptionDescription(getMessageCodes().get(fieldError.getCode()), fieldError.getDefaultMessage(), fieldError.getField(), ex.getStatusCode()));
} else {
errors.add(new ExceptionDescription(err.getDefaultMessage(), field(ex), ex.getStatusCode()));
}
}
return handleExceptionInternal(ex, errors, headers, status, request);
}
The problem I'm having is that the response is being overwritten by the BasicErrorController and I'm not seeing the array of ExceptionDescriptions in my json response.
Weirdly, this is working fine in another project.
Any ideas?
So, the behaviour I was seeing was a result of the ExceptionDescription class not having public getter methods and an exception was thrown that took me sometime to discover.
This explains the reasong of the exception - Jackson Mapping Exceptions