xml与javaConfig 配置同一个拦截器 ,javaConfig 对 swagger /v2/api-doc 不生效
javaConfig
@Bean
public SignValidInterceptor logInterceptor() {
SignValidInterceptor signValidInterceptor = new SignValidInterceptor();
signValidInterceptor.excludePathPattern("GET", "/swagger/**");
// .excludePathPattern("GET", "/");
return signValidInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor())
//.excludePathPatterns("/swagger/**")
.addPathPatterns("/**");
}
xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="signValidInterceptor" class="com.wmg.tools.interceptor.SignValidInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
https://github.com/spring-projects/spring-framework/blob/376434eb7545c39dcc7f8f87a28eeb9f26bbfd6e/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java#L130
您好,我在使用 javaConfig
配置拦截器时,发现其对swagger
接口不生效。
debug代码时,发现InterceptorRegistration
将HandlerInterceptor
包装 MappedInterceptor
时 没有把 MappedInterceptor
实例放入容器。
以至于在 org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors
中 找不到 MappedInterceptor
的实例,
org.springframework.web.servlet.handler.AbstractHandlerMapping#initApplicationContext
故而不能为PropertySourcedRequestMappingHandlerMapping
添加 adaptedInterceptors
,
使得拦截器不拦截PropertySourcedRequestMappingHandlerMapping
类型的接口,例如Swagger
的 /v2/api-docs
接口。
mappedInterceptors.addAll(
BeanFactoryUtils.beansOfTypeIncludingAncestors(
obtainApplicationContext(), MappedInterceptor.class, true, false).values());
}
Comment From: sbrannen
@minggen, can you please either edit your description to use English or provide a comment in English that explains what you find inconsistent?
Thanks
Comment From: minggen
Hello, I found it didn't make sense to intercept swagger
when I use javaConfig
on interceptor configuration.
When xml and javaConfig are used in the same interceptor, javaConfig didn't work on swagger /v2/api-doc.
Also, I create a demo about this issue https://github.com/minggen/springmvc-questions, and the issue could be reproduced when modifying com.example.demo.config.AutoConfig
.
-
When using xml configuration, there is no return value after calling http://localhost:8080/v2/api-docs .
-
When using annotation configuration, the right return value would not be intercepted properly.
When I try to figure out the reason, in this line of code, I found InterceptorRegistration
will wrap the custom interceptor into an instance of the MappedInterceptor
class, and then put the instance into interceptors
, but not into the applicationContext
, so that adaptedInterceptors
cannot be added for PropertySourcedRequestMappingHandlerMapping
. As a result, the interceptor will not take effect.
mappedInterceptors.addAll(
BeanFactoryUtils.beansOfTypeIncludingAncestors(
obtainApplicationContext(), MappedInterceptor.class, true, false).values());
Comment From: azouever
I spent a long time to set the interceptor for /v2/api-docs
by java config , but not working; eventually used xml ; spring-boot-starter-parent
2.1.4.RELEASE
Comment From: rstoyanchev
Why not declare your interceptor as an @Bean
of type MappedInterceptor
, i.e. not using the InterceptorRegistry
at all? The registry is for interceptors applied to the HandlerMapping
beans of the MVC config.
Comment From: azouever
@rstoyanchev do this ,but still not work , you can click here(i write the demo) : https://github.com/azouever/demo
Here: https://github.com/azouever/demo/blob/master/src/main/java/com/example/demo/InterceptorConfig.java
and curl localhost:8080/v2/api-docs
Comment From: rstoyanchev
That is not what I meant. I suggested to not use the InterceptorRegistry but to declare MappedInterceptor as a bean. The example does the opposite.
Please, review the Javadoc for MappedInterceptor
. When it is declared as a bean it is auto-detected directly by all sub-classes of AbstractHandlerMapping and that's an alternative to using the InterceptorRegistry.