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
RestControllerAdvice with custom annotations is not working properly [SPR-17569] #22101
Comments
Rossen Stoyanchev commented Please fill out the description to explain what you're requesting. I've read #21502 and it sounded like you want to see a Javadoc improvement, but the subject of this ticket sounds unclear. |
volkanto commented Hi Rossen Stoyanchev, I've updated the description. |
Rossen Stoyanchev commented Thanks, |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue. |
I'm having this issue as well. It seems to me that certain exceptions that occur while deciding if an endpoint in a controller class match the request (correct http method, correct Accept and Content-Type headers, etc.) then the endpoint is never chosen, so the Controller class is never chosen, so the "annotation" property of the ControllerAdvice annotation won't be matched. This seems counter-intuitive to me, because the path has been matched well enough to know, for example, the client sent the wrong HTTP method, so it stands to reason the request should be attached to that endpoint's controller, and the advice class should be able to target an annotation from that controller. |
It seems to me, if annotations are explicitly specified on the controller handler, e.g. media type consumed or HTTP method is used, then Spring MVC takes it as part of its signature, and during the determination stage to which controller the request belongs to, it wouldn't find the match, and therefore it delegates the exception handling to the default Spring MVC handler. Hence none of the custom exception handlers will be called. It's a bit annoying, but if that's how their Servlet implementation behaves, then we just have to understand the behavior. |
volkanto opened SPR-17569 and commented
We have two different controllers for two different versions of the API. We are trying to distinguish the exception handling too. Regarding this need, we created custom annotations to distinguish the controllers.
Here are the custom annotations for the version definitions:
Here are the controller implementations with the custom version annotations:
And here are the custom exception handlers regarding the version declarations:
If a HttpMediaTypeNotSupportedException occurs, it is not handled by ExceptionHandlerForV1. We are getting Spring's exception instead of our custom exception. But if I remove annotations = ApiV1.class part from the ExceptionHandlerForV1, this class is getting a global exception handler and related exceptions caught by this handler in a correct way. And also same behavior happens for MethodNotAllowedException too. But other types of exceptions handled properly.
We are wondering why this two exceptions (we tried these two exceptions so far) not handled by our custom exception handlers. Is it a design decision or a bug? If this is a design decision, how can we handle all these in our custom exception handlers?
We also tried all these cases with Spring 5.1.3.RELEASE too.
Affects: 5.0.6
Reference URL: #21502
The text was updated successfully, but these errors were encountered: