-
Notifications
You must be signed in to change notification settings - Fork 38.5k
ConstraintViolationExceptionHandler @ControllerAdvice doesn't work in the controller layer validation #33133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The controller and service are both handled by different components. The service is proxied by the For the controller however this is not the case and validation is called as part of the regular web method execution and done as part of data binding. Historically this threw a You would need to handle the |
Thanks @mdeinum @ozooxo see also the documentation. |
I think I worked it out. The story is slightly different. First, since my application is using spring-webflux (not spring-mvc), the default message
is in Line 53 in abcad5d
MethodArgumentNotValidException Line 60 in abcad5d
Second, to further handle
which is saying that Line 131 in abcad5d
ResponseEntityExceptionHandler , for me to define another @ExceptionHandler(WebExchangeBindException::class) will cause a conflict.
Add another individual @RestControllerAdvice
class AdditionalHandler {
@ExceptionHandler(WebExchangeBindException::class)
protected fun handleException(
ex: WebExchangeBindException
): ProblemDetail {
return ProblemDetail.forStatusAndDetail(
HttpStatus.UNAUTHORIZED,
"override to worse worse worse"
)
}
} |
Uh oh!
There was an error while loading. Please reload this page.
It looks for me that the
ConstraintViolationExceptionHandler
with@ControllerAdvice
doesn't work in the controller layer validation. It works for the service layer validation though.See demo code in https://github.com/ozooxo/spring-jakarta-controller-advise-bug/
ConstraintViolationException
to raise 401 instead of 400 with error messageoverride to bad bad bad
Run the application by
./gradlew bootRun
.Call the endpoint with service layer validation. The return is as expected.
However, if I call the controller layer validation, I got
The expected behavior is I get
instead, because I have advised the controller to use 401 when it sees
ConstraintViolationException
.Also, if I comment out this line and
Which indicates my controller endpoint is setting up correctly.
The text was updated successfully, but these errors were encountered: