Reputation: 4932
I've got following class
@Configuration
public class WebConfiguration {
@Bean
public ServletRegistrationBean slspServlet() {
ServletRegistrationBean testServlet = new ServletRegistrationBean(new TestServlet());
testServlet.addUrlMappings("/*");
testServlet.setAsyncSupported(true);
return TestServlet;
}
@Bean
public ServletRegistrationBean aliveServlet() {
ServletRegistrationBean aliveServlet = new ServletRegistrationBean(new AliveServlet());
aliveServlet.addUrlMappings("/alive/*");
aliveServlet.setLoadOnStartup(3);
return aliveServlet;
}
@Bean
public ServletRegistrationBean templateDownloaderServlet(){
ServletRegistrationBean templateDownloader = new ServletRegistrationBean(new TemplateDownloaderServlet());
templateDownloader.addUrlMappings("/template/download/*");
templateDownloader.setLoadOnStartup(2);
return templateDownloader;
}
@Bean
public ServletRegistrationBean endOfGenerationThreadServlet(){
ServletRegistrationBean endOfGenerationThread = new ServletRegistrationBean(new EndOfGenerationThreadServlet());
endOfGenerationThread.addUrlMappings("/endofgenerationthread/*");
endOfGenerationThread.setLoadOnStartup(1);
return endOfGenerationThread;
}
@Bean
public ServletRegistrationBean dispatcherServlet(){
ServletRegistrationBean dispatcherServlet = new ServletRegistrationBean(new DispatcherServlet());
dispatcherServlet.setName("dispatcherServlet");
dispatcherServlet.addUrlMappings("/dispatcher/*");
return dispatcherServlet;
}
@Bean
public FilterRegistrationBean errorPageFilter(){
FilterRegistrationBean errorPageFilter = new FilterRegistrationBean(new ErrorPageFilter(), dispatcherServlet());
errorPageFilter.addUrlPatterns("/test/*");
return errorPageFilter;
}
@Bean
public FilterRegistrationBean characterEncodingFilter(){
FilterRegistrationBean characterEncodingFilter = new FilterRegistrationBean(new CharacterEncodingFilter(), dispatcherServlet());
characterEncodingFilter.addUrlPatterns("/test/*");
return characterEncodingFilter;
}
@Bean
public FilterRegistrationBean hiddenHttpMethodFilter(){
FilterRegistrationBean hiddenHttpMethodFilter = new FilterRegistrationBean(new HiddenHttpMethodFilter(), dispatcherServlet());
hiddenHttpMethodFilter.addUrlPatterns("/test/*");
return hiddenHttpMethodFilter;
}
}
What am I trying to do is change filter mapping of hiddenHttpMethodFilter, characterEncodingFilter, errorPageFilter so for that I have mapping /test/*. When I start application I can see that it defaults to "/*"
Mapping servlet: 'testServlet' to [/*]
Mapping servlet: 'aliveServlet' to [/alive/*]
Mapping servlet: 'templateDownloaderServlet' to [/template/download/*]
Mapping servlet: 'endOfGenerationThreadServlet' to [/endofgenerationthread/*]
Mapping servlet: 'dispatcherServlet' to [/dispatcher/*]
Mapping filter: 'errorPageFilter' to: [/*]
Mapping filter: 'characterEncodingFilter' to: [/*]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
What am I missing or what's wrong with my config? I'm on spring-boot 1.2.5.
There is another problem with filters (I think that's in filters). When I want to access request.getInputstream()
in my testServlet it's already read. I also read this question and tried to implement RequestWrapper, but it's too late in my testServlet to wrap the request, because inputstream was read. So can somebody help me how to resolve this?
Thanks
Upvotes: 1
Views: 2518
Reputation: 116111
Your FilterRegistrationBean
bean named hiddenHttpMethodFilter
is being overridden by the OrderedHiddenHttpMethodFilter
bean of the same name that's created by Spring Boot's auto-configuration. There's a log message that tells you this is happening:
2015-08-27 16:13:54.268 INFO 70942 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'hiddenHttpMethodFilter' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=sampleWebFreeMarkerApplication; factoryMethodName=hiddenHttpMethodFilter; initMethodName=null; destroyMethodName=(inferred); defined in sample.freemarker.SampleWebFreeMarkerApplication] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; factoryMethodName=hiddenHttpMethodFilter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class]]
You need to rename your hiddenHttpMethodFilter
method. Also, if you want to configure the registration of a Filter
or Servlet
that Boot auto-configures, there's no need to create the Filter
or Servlet
yourself. Instead, you should have the existing one injected into your @Bean
method:
@Bean
public FilterRegistrationBean hiddenHttpMethodFilterRegistration(
HiddenHttpMethodFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setFilter(filter);
registration.addUrlPatterns("/test/*");
return registration;
}
You might also want to consider setting the server.servlet-path
property in application.properties
as an easier way of configuring the dispatcher servlet's mapping.
Upvotes: 1