java - WebApplicationInitializer container.addFilter() returns null -


i creating rest-based web application angularjs front end , rest-based backend (with spring 4). following code-based configuration approach found here: webapplicationinitializer

when run project on server, null value in line:

filterregistration.dynamic filter =  container.addfilter("prerender", seofilter); 

what missing? bit new creating web-apps scratch using annotations.

here class in question:

public class mywebappinitializer implements webapplicationinitializer {  @override public void onstartup(servletcontext container) throws servletexception {  xmlwebapplicationcontext appcontext = new xmlwebapplicationcontext(); appcontext.setconfiglocation("classpath:mycontext.xml");  servletregistration.dynamic dispatcher = container.addservlet("dispatcher", new dispatcherservlet(appcontext)); dispatcher.setloadonstartup(1); dispatcher.addmapping("/api/*");  com.github.greengerong.prerenderseofilter seofilter = new com.github.greengerong.prerenderseofilter(); filterregistration.dynamic filter =  container.addfilter("prerender", seofilter); filter.setinitparameter("prerendertoken", "123456789123456789"); filter.addmappingforurlpatterns(null , true, "/*");  servletregistration.dynamic initsysconfiguration         = container.addservlet("initsysconfiguration", new initsystemconfigurations()); initsysconfiguration.setloadonstartup(1); initsysconfiguration.addmapping("/initsystemconfigurations");  } 

this line give me null

com.github.greengerong.prerenderseofilter seofilter = new com.github.greengerong.prerenderseofilter(); 

i tried this,but same result

filterregistration.dynamic filter1 =  container.addfilter("prerender",  com.github.greengerong.prerenderseofilter.class); 

when method addfilter returns null means there a filter registered name.

returns:
filterregistration object may used further configure given filter, or null if servletcontext contains complete filterregistration filter given filtername or if same filter instance has been registered or servletcontext in same container

make sure don't have web.ml registers filter.

another tip instead of implementing webapplicationinitializer extend abstractdispatcherservletinitializer , implement needed methods.

public class mywebappinitializer extends abstractdispatcherservletinitializer {      @override     public void onstartup(servletcontext container) throws servletexception {         super.onstartup(container);          com.github.greengerong.prerenderseofilter seofilter = new com.github.greengerong.prerenderseofilter();         filterregistration.dynamic filter =  container.addfilter("prerender", seofilter);         filter.setinitparameter("prerendertoken", "123456789123456789");         filter.addmappingforurlpatterns(null , true, "/*");          servletregistration.dynamic initsysconfiguration                 = container.addservlet("initsysconfiguration", new initsystemconfigurations());         initsysconfiguration.setloadonstartup(1);         initsysconfiguration.addmapping("/initsystemconfigurations");      }      protected webapplicationcontext createservletapplicationcontext() {         xmlwebapplicationcontext appcontext = new xmlwebapplicationcontext();         appcontext.setconfiglocation("classpath:mycontext.xml");         return appcontext;     }      protected string[] getservletmappings() {         return new string[] {"/api/*"};     }  } 

Comments