This is driving me crazy. I've done a good bit of Spring coding but this is the first time that I'm trying to do all configuration in Java. I've searched all over stackoverflow and other places, and while others have seen this problem, none of their solutions have worked for me.
Here are my source files:
Initializer:
public class EnhancedCandidateInfoInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
private static final Logger logger = LoggerFactory.getLogger(EnhancedCandidateInfoInitializer.class);
@Override
protected Class<?>[] getRootConfigClasses() {
logger.info("@@@@@ getRootConfigClasses called - returning null @@@@@");
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
logger.info("@@@@@ getServletConfigClasses called returning EnhancedCandidateInfoWebConfiguration.class @@@@@");
return new Class[] { EnhancedCandidateInfoWebConfiguration.class };
}
@Override
protected String[] getServletMappings() {
logger.info("@@@@@ getServletMappings called @@@@@");
return new String[] { "/" };
}
}
WebMvcConfiguration:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.mojorank.restapi"})
public class EnhancedCandidateInfoWebConfiguration extends WebMvcConfigurerAdapter {
private static final Logger logger = LoggerFactory.getLogger(EnhancedCandidateInfoWebConfiguration.class);
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
logger.info("#### My Configuration handler was called ####");
configurer.enable();
}
}
Controller:
@RestController
public class EnhanceCandidateInfoController {
@RequestMapping("/")
public String welcome() {//Welcome page, non-rest
return "Welcome to RestTemplate Example.";
}
@RequestMapping("/hello/{player}")
public Message message(@PathVariable String player) {//REST Endpoint.
Message msg = new Message(player, "Hello " + player);
return msg;
}
}
When I build and deploy the application to tomcat, I get the following exception stack trace:
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 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5274) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
As I said, I've searched through stackoverflow and other places and found others that had my same problem, but when I tried to implement the proposed fixes, my problem remained. Thanks in advance for any help.