New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
customizing spring mvc auto configuration does not work for interceptors #2870
Comments
little bit confused, following is not working for you? @configuration
|
It works for me: package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class Gh2870Application {
public static void main(String[] args) {
SpringApplication.run(Gh2870Application.class, args);
}
@Bean
public WebMvcConfigurerAdapter adapter() {
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
System.out.println("Adding interceptors");
super.addInterceptors(registry);
}
};
}
}
Can you share a sample project that illustrates the problem? |
The following does not work for me: In my main app i have an import on this configuration class: @Configuration
public class CustomWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ZuniCommRequestInterceptor(zuniCommLogService)).addPathPatterns("/zunicomm/**");
super.addInterceptors(registry);
}
} @esodot: This is not my use case, i do not want to use the @EnableWebMvc as is stated in my first post. |
Ok, getting closer, disabling SecurityAutoConfiguration.class -> adding interceptors works again. |
Still works for me. I added a |
Thanks for your investigation. I am using the swagger-springmvc project, when using the @EnableSwagger annotation, my addInterceptors method is not called anymore. Will post the issue in the swagger-springmvc project. |
Thanks for the update. I'm going to close this for now. If further investigation suggests that it's a problem with Spring Boot after all, please comment and we can reopen the issue. |
Hi, even I am facing similar issue with spring boot, and I found similar issues on four systems of widely different configuration. Following are the details of the issue
Only console message that I get is
PS: I am not using any third party spring project other than spring-boot |
@AmeyKamat Are you able to provide a sample application that demonstrates the issue? Also I don't understand what you mean by "The observation noted in 4 only works on one out of four cases that I have seen", can you clarify? |
I have created a sample app that fails to work on some system: Sorry...it was typo... I meant that renaming of file from WebConfig.class to MvcConfig.class works only in one of the systems. |
@philwebb Is there a temporary workaround for this? I have posted corresponding question at stack overflow: http://stackoverflow.com/questions/30910642/webmvcconfigureradapter-does-not-work |
@AmeyKamat I would expect the code that you've shared to fail on every system. By default, |
Ok...I dint know that...bt still in my original project that I am working on...main package is called app and WebConfig file is in app.config package. Still I will try this on sample code. |
@wilkinsona nope....it doesn't...I will update sample code. |
@AmeyKamat Ok, let me know once you've made the updates and I'll take another look. |
@wilkinsona I have pushed the changes |
Why did you change As an aside, you should probably also remove the |
Following @philwebb 's suggestion I removed @EnableWebMvc and the demo app started working. Then I tried stripping down project files one by one to see at what point error disappears. I found that I had two classes in the project, one, extended from WebMvcConfigurationSupport and the second, from WebMvcConfigurerAdapter. removing the former class from the project fixed the error. What I want to know is, why did this happen? Secondly, why doesnt this error appear on all systems? |
Glad to hear that you've got things working.
It's impossible to say without seeing some code that reproduces the problem. I would guess that, as I explained above, it's because you were configuring things twice and one of those configurations has to "win".
Assuming that my guess about the duplicate configuration is correct, the order in which they processed is probably undefined (you could fix the order using |
|
@liuyuyu extending I don't know if your comment is supposed to show a solution to this issue - because in fact it is not. |
Thanks Reply. Although not particularly elegant, this approach does solve my problem, code address: https://github.com/liuyuyu/shadow/blob/master/shadow-metrics/src/main/java/io/github/liuyuyu/shadow/metrics/config/ShadowMetricsMVCConfiguration.java. Do you have any better suggestions? |
Yes, asking a question on StackOverflow. We're usually answering questions there because it helps the broader community. |
@bclozel Sorry, I thought this issue was the same as discussing the issue with me. The situation I encountered is different. I will sort out the code that can reproduce the problem and put it in the new issue later. |
I added my own WebMvcConfigurerAdapter as stated in the documentation:
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.
The addInterceptors method never gets called. I did some debugging and found out that the delegates are empty at the point in time when addInterceptors is called on DelegatingWebMvcConfiguration.
The text was updated successfully, but these errors were encountered: