You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document options to add interceptors to ResourceHandlerRegistry handler mapping [SPR-10655] #15283
Our use case is this: we need to add a handler interceptor to a resource handler so it can set the content type header for some static resources. We would like to test this with Spring MVC Test, so we want to have this happen in the DispatcherServlet handling chain (i.e. a Filter isn't a great solution).
HandlerInterceptor's you mean? ResourceHandlerRegistry creates a SimpleUrlHandlerMapping with one or more ResourceHttpRequestHandler mapped by pattern. You can get a hold of this SimpleUrlHandlerMapping by extending WebMvcConfigurationSupport and then set some interceptors on it. Those can be of type HandlerInterceptor or MappedInterceptor if you want them limited to specific patterns.
What if your application already has a WebMvcConfigurationSupport and you don't want to (or can't) modify it? WHat is the reason for the existing handler mapping not using interceptors, or why can't we change it?
Okay I see what you mean now. Yeah we could add an InterceptorRegistry to ResourceHandlerRegistry and set the resulting interceptors on the HandlerMapping it creates.
Dave Syer, just confirming this is still needed or if you perhaps found another way? Also, I am wondering would a mapped interceptor meet your use case? A MappedInterceptor declared as a bean is auto-detected in AbstracHandlerMapping.
Hi! In case you still need this, here is my fully functional code. The key part is the overriding of the resourceHandlerMapping method of the WebMvcConfigurationSupport class(this is spring 3.2) :
Adding a MappedInterceptor bean works brilliantly, thanks.
It did cost us the better part of a day to debug this problem though!
It is very counterintuitive that
addInterceptors and addResourceHandlers don't play nice together
there is no way (that we found) to fix this by overriding the methods already present in the WebMvcConfigurationSupport
It would be nice if this were documented better or (even better) simply fixed.
Well, simply plugging in all configured interceptors into the ResourceHandler mapping at this stage will almost certainly cause a regression somewhere. It's also not unreasonable to me that interceptors for static resources should be separate from interceptors for other reqests.
I'm turning this into a documentation task since this is indeed not obvious at all. I've modified the title accordingly (was "Add support for interceptors in ResourceHandlerRegistry handler mapping").
Activity
spring-projects-issues commentedon Jun 21, 2013
Rossen Stoyanchev commented
HandlerInterceptor's you mean? ResourceHandlerRegistry creates a SimpleUrlHandlerMapping with one or more ResourceHttpRequestHandler mapped by pattern. You can get a hold of this SimpleUrlHandlerMapping by extending WebMvcConfigurationSupport and then set some interceptors on it. Those can be of type HandlerInterceptor or MappedInterceptor if you want them limited to specific patterns.spring-projects-issues commentedon Jul 3, 2013
Dave Syer commented
What if your application already has a WebMvcConfigurationSupport and you don't want to (or can't) modify it? WHat is the reason for the existing handler mapping not using interceptors, or why can't we change it?
spring-projects-issues commentedon Jul 3, 2013
Rossen Stoyanchev commented
Okay I see what you mean now. Yeah we could add an InterceptorRegistry to ResourceHandlerRegistry and set the resulting interceptors on the HandlerMapping it creates.
spring-projects-issues commentedon Jul 3, 2013
Rossen Stoyanchev commented
I've set the fix version to 4.0 backlog since I probably won't get to it for M2 but feel free to send a PR if you need it sooner.
spring-projects-issues commentedon May 1, 2014
Rossen Stoyanchev commented
Dave Syer, just confirming this is still needed or if you perhaps found another way? Also, I am wondering would a mapped interceptor meet your use case? A
MappedInterceptordeclared as a bean is auto-detected inAbstracHandlerMapping.spring-projects-issues commentedon Aug 14, 2014
Julian Ramirez commented
Hi! In case you still need this, here is my fully functional code. The key part is the overriding of the resourceHandlerMapping method of the WebMvcConfigurationSupport class(this is spring 3.2) :
spring-projects-issues commentedon Oct 13, 2014
Rossen Stoyanchev commented
Note that you can also declare a MappedInterceptor bean for the resource URLs.
spring-projects-issues commentedon Nov 24, 2014
Fleur Kelpin commented
Adding a MappedInterceptor bean works brilliantly, thanks.
It did cost us the better part of a day to debug this problem though!
It is very counterintuitive that
addInterceptorsandaddResourceHandlersdon't play nice togetherIt would be nice if this were documented better or (even better) simply fixed.
spring-projects-issues commentedon Nov 24, 2014
Rossen Stoyanchev commented
Well, simply plugging in all configured interceptors into the ResourceHandler mapping at this stage will almost certainly cause a regression somewhere. It's also not unreasonable to me that interceptors for static resources should be separate from interceptors for other reqests.
I'm turning this into a documentation task since this is indeed not obvious at all. I've modified the title accordingly (was "Add support for interceptors in ResourceHandlerRegistry handler mapping").
spring-projects-issues commentedon Dec 2, 2014
Rossen Stoyanchev commented
See commit 7aa5e6.