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

ControllerAdvice with custom annotations not working [SPR-16964] #21502

Closed
spring-projects-issues opened this issue Jun 20, 2018 · 3 comments
Closed
Assignees
Labels
in: web status: declined

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jun 20, 2018

Simon Oelerich opened SPR-16964 and commented

Creating an ControllerAdvice with assignableTypes works fine.

Custom annotations are not recognized as follows:

@ControllerAdvice(annotations = AdminsOnly.class)
public class AdminControllerAdvice { ... }
@Controller
@AdminsOnly
public AdminController { ... }
public @interface AdminsOnly { }

With this type of ControllerAdvice the AdminController will not use the advice.


The following is working fine as a workaround:

@ControllerAdvice(assignableTypes = AdminsOnly.class)
public class AdminControllerAdvice { ... }
@Controller
public AdminController implements AdminsOnly { ... }
public interface AdminsOnly { }

Affects: 5.0.6, 5.0.7

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 20, 2018

Juergen Hoeller commented

Has the custom annotation been declared with @Retention(RetentionPolicy.RUNTIME)? Any other specifics that possibly makes it different from the annotations that come with Spring? Does it work for you with a custom stereotype: e.g. an @AdminController annotation that is meta-annotated with @Controller, binding the advice to AdminController.class?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 21, 2018

Simon Oelerich commented

I had no @Retention annotation and that was the problem. With @Retention(RetentionPolicy.RUNTIME) it works as expected.

The javadoc for ControllerAdvice.annotations() says:

Consider creating a special annotation or use a predefined one, like @RestController.

I was a bit surprised that there is this hint, but it won't work out of the box, as I am used to in the Spring context (mostly).

Maybe it could be added to use the RetentionPolicy RUNTIME to this part of the javadoc.

Nevertheless the mentioned @RestController uses it and I should have figured this out by myself.

I appreciate your help and thank you for the idea to create a custom @AdminController wich serves my purpose best.

  • This issue can be closed -

@spring-projects-issues spring-projects-issues added type: bug status: declined in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug label Jan 12, 2019
@fransflippo
Copy link

@fransflippo fransflippo commented Aug 26, 2020

Ran into the same issue! A hint about the retention policy in the documentation would have been really helpful.

I'll look at submitting a pull request with this added to the ControllerAdvice javadoc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web status: declined
Projects
None yet
Development

No branches or pull requests

3 participants