xml and javaConfig are used in the same interceptor, javaConfig didn't work on swagger /v2/api-doc
See original GitHub issuexml与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>
您好,我在使用 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());
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Apache camel 2.18.0 Release
The access in the Rest DSL has been deprecated and no longer in use - its not part of swagger specification anymore. Camel-NATS...
Read more >Spring REST + Swagger 2 Example - ConcretePage.com
Here on this page we will provide complete example to integrate Swagger 2 with Spring REST web service using JavaConfig as well as...
Read more >How to Enable Cross Origin Requests for a RESTful Web Service
The main() method uses Spring Boot's SpringApplication.run() method to launch an application. Did you notice that there was not a single line of...
Read more >6.4. Routing Red Hat JBoss Fuse 6.3 | Red Hat Customer Portal
CAMEL-7800, camel-swagger - Upgrade to swagger that is pure Java based ... CAMEL-8459, Java DSL - Align beanRef as bean to be similar...
Read more >Spring Data for Apache Cassandra - Reference Documentation
Note that the JavaConfig variant does not configure a package explicitly, because the package of the annotated class is used by default.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello, I found it didn’t make sense to intercept
swagger
when I usejavaConfig
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 theMappedInterceptor
class, and then put the instance intointerceptors
, but not into theapplicationContext
, so thatadaptedInterceptors
cannot be added forPropertySourcedRequestMappingHandlerMapping
. As a result, the interceptor will not take effect.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.