I\'m having trouble when Using @ControllerAdvice and @Valid annotations together in a REST controller.
I have a rest controller dec
You are on the right track, but you need to override the handleMethodArgumentNotValid() instead of the handleException()
method, e.g.
@ControllerAdvice
public class RestErrorHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity
From the JavaDoc of MethodArgumentNotValidException:
Exception to be thrown when validation on an argument annotated with @Valid fails.
In other words, a MethodArgumentNotValidException
is thrown when the validation fails. It is handled by the handleMethodArgumentNotValid()
method provided by the ResponseEntityExceptionHandler
, that needs to be overridden if you would like a custom implementation.