In our application we override handleBindException from ResponseEntityExceptionHandler to improve the output of error message.
@Override
protected ResponseEntity<Object> handleBindException(
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
final List<String> errors = new ArrayList<String>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() + " has an invalid value: " + error.getRejectedValue());
}
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
}
apiError.setStatus(HttpStatus.BAD_REQUEST);
apiError.setMessage(errors.toString());
apiError.generateDate();
return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
Since Spring 6.0 the method is marked as deprecated, removal is planned for 6.1. I tried to override handleMethodArgumentNotValid Method, but that has no impact. For example if several arguments are not valid, then a BindException is thrown instead How can I deal with this problem, so that my solution works with the next upgrade?
Comment From: rstoyanchev
It looks like there is one more place in ModelAttributeMethodProcessor
that wasn't switched to use MethodArgumentNotValidException
as part of #29251. Could you confirm that the BindException
you're seeing is raised from here?
Comment From: tinah-1973
Yes I can confirm that the BindException is initiated there!