question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

xml and javaConfig are used in the same interceptor, javaConfig didn't work on swagger /v2/api-doc

See original GitHub issue

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());
	}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
minggencommented, Apr 21, 2020

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());
0reactions
rstoyanchevcommented, Feb 2, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found