user5096772
user5096772

Reputation:

java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name

My initializer class

public class HomeServlet extends 
AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {

    return null;
}

@Override
protected Class<?>[] getServletConfigClasses() {

    return new Class<?>[]{SpringContextConfig1.class};
}

@Override
protected String[] getServletMappings() {

    return new String[] {"/home"};
}

}

Configuration Class

@ComponentScan(basePackages={"spittr.controllers"})
@Configuration
@EnableWebMvc
public class SpringContextConfig1 extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver ivr=new InternalResourceViewResolver();
    ivr.setPrefix("/WEB-INF/jsp/");
    ivr.setSuffix(".jsp");
    ivr.setExposeContextBeansAsAttributes(true);
    return ivr;
}

}

Controller

@Controller
public class HomeController {


@RequestMapping(value="/home",method=RequestMethod.GET)
public String home(){
    return "home";
}

}

This is a very simple program, I wrote to test the JavaConfig of Spring MVC.I exactly followed all the steps from "Spring in Action" book.

When I run this code, I'm getting this error

09:41:37,854 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 72) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./spittr: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./spittr: java.lang.RuntimeException: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name. at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) at org.jboss.threads.JBossThread.run(JBossThread.java:320) Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name. at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:236) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82) ... 6 more Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name. at org.springframework.util.Assert.notNull(Assert.java:115) at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98) at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:71) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:186) at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:171) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)

The hightlight of the error is that " Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name. at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85) "

Please help me to solve this problem. I using WildFly-10 on eclipse.

Upvotes: 0

Views: 9104

Answers (3)

Achinda Mihiruk
Achinda Mihiruk

Reputation: 11

you can do a few things to overcome that issue.

  1. delete the target directory, and re-deploy the web server.the target directory
  2. If your project is a maven project, you can hit "maven clean package" within the pom.xml file and re-deploy the web server.mvn clean package

Upvotes: 0

kai
kai

Reputation: 86

My friend encountered the same problem. The problem was that the correct package name was not set for the class. AAAAnd clean the project before packaging!!!

Here is the code on GitHub

Upvotes: 0

fantaghirocco
fantaghirocco

Reputation: 4878

  1. use the configuration class as follows:

    @ComponentScan(basePackages={"spittr.controllers"})
    @Configuration
    @EnableWebMvc
    public class SpringContextConfig1 extends WebMvcConfigurerAdapter{
    
        @Override            
        public void configureViewResolvers(ViewResolverRegistry registry) {
            InternalResourceViewResolver ivr=new InternalResourceViewResolver();
            ivr.setPrefix("/WEB-INF/jsp/");
            ivr.setSuffix(".jsp");
            ivr.setExposeContextBeansAsAttributes(true);
            registry.viewResolver(ivr);
        }
    }
    

    Basically you're extending WebMvcConfigurerAdapter without inheriting any of its methods (in my 4.3.3 Spring version al least).

  2. since you have a single DispatcherServlet here, you can add the SpringContextConfig1 class to the root context and leave the servlet context empty: switch the body of the method getServletConfigClasses() under the getRootConfigClasses() and vice versa - see A Bit on ApplicationContext Hierarchies.


Moreover, the DispatcherServlet mapping is more likely / instead of /home:

protected String[] getServletMappings() {
    return new String[] {"/"};
}

Upvotes: 0

Related Questions