Skip to content
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

Closed
spring-issuemaster opened this issue Dec 5, 2018 · 5 comments
Labels

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Dec 5, 2018

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:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiV1 { }

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiV2 { }

 

Here are the controller implementations with the custom version annotations:

@ApiV1
@RequestMapping("/path")
@RestController
public class XYZControllerImpl implements com.xyz.XYZController { ... }

@ApiV2
@RequestMapping("/v2/path")
@RestController
public class XYZControllerV2Impl implements com.xyz.v2.XYZController { ... }

 

And here are the custom exception handlers regarding the version declarations:

@RestControllerAdvice(annotations = ApiV1.class)
public class ExceptionHandlerForV1 {      
    private static final String ONLY_APPLICATION_JSON_SUPPORTED = "only 'application/json' supported";     
    ...        
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)    
    @ResponseStatus(HttpStatus.BAD_REQUEST)    
    public CustomResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {        
        return new CustomResponse(CustomResponse.Status.INVALID_REQUEST, ONLY_APPLICATION_JSON_SUPPORTED);    
    }     
    ... 
}

@RestControllerAdvice(annotations = ApiV2.class) 
public class ExceptionHandlerForV2 { ... }

 

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 6, 2018

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 7, 2018

volkanto commented

Hi Rossen Stoyanchev, I've updated the description.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 12, 2018

Rossen Stoyanchev commented

Thanks, @ExceptionHandler methods, including those from @ControllerAdvice should be used before the DefaultHandlerExceptionResolver, assuming you're using something like @EnableWebMvc. So it should work as expected. I'm not sure what the issue is. Could you extract a small sample that demonstrates the issue?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Feb 1, 2019

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Feb 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.