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

a 夏天 提交于 2019-12-20 06:33:12

问题


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.


回答1:


  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[] {"/"};
}



回答2:


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



来源:https://stackoverflow.com/questions/41124012/java-lang-illegalargumentexception-failed-to-register-servlet-with-name-dispat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!