It feels like there should be an @Interceptor annotation the way that spring has @Service, @Repository, etc. I think it would be a good idea to include such an annotation with values equivalent to .addPathPatterns, .excludePathPatterns, maybe something like order to facilitate chaining and so on and have spring boot auto config take care of registering it instead of having to create a WebMvcConfigurer
Comment From: philwebb
I'm not sure exactly what you're trying to do, but perhaps @ControllerAdvice or @RestControllerAdvice might be helpful? If not, please open a new issue at https://github.com/spring-projects/spring-framework/ with some more details so that they can consider the idea.
Comment From: lukalomidze
The idea is that in spring boot spirit, i.e minimal manual configuration, it would be nice to have an @Interceptor annotation that would take care of registering an annotated interceptor it in InterceptorRegistry
Comment From: philwebb
Given that the HandlerInterceptor beans aren't all that common, I'm not sure that we should add an additional annotation.
I guess you're looking for something like this:
@Interceptor(pathPatterns="/foo/*")
class MyInterceptor implements HandlerInterceptor {
...
}
Which is a little more concise than:
@Bean
public WebMvcConfigurer myConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(new MyInterceptor()).addPathPatterns("/foo/*");
}
};
}
This still feels like a Spring Framework feature to me, especially as the @Controller annotations are there.
I'll flag this one for team-attention to see what other folks in the team think.
Comment From: wilkinsona
This feels like a Spring Framework issue to me too. I don't think we should have a Boot-specific annotation for automatic registration of Framework's HandlerInterceptor contract.
Comment From: bclozel
Thanks for the proposal, but this is indeed a Spring Framework enhancement request. This was already requested and declined in the past. In the meantime, there is less focus on the HandlerInterceptor contract these days.
Closing as a duplicate of spring-projects/spring-framework#9447