Spring web-mvc 4 java config not working

I am trying to run an elementary spring -4 web-mvc application without an xml configuration. I looked at the documentation and spring examples, but this did not work for me. My controller:

package com.nikolay.exam.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HomeController { @RequestMapping(value = "/home", method = RequestMethod.GET) @ResponseBody public String home() { return "Hello world!"; } } 

AppConfig:

 package com.nikolay.exam.config; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { } 

WebConfig:

 package com.nikolay.exam.config; import com.nikolay.exam.controller.HomeController; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Bean public HomeController homeController() { return new HomeController(); } } 

And WebInitializer:

 package com.nikolay.exam.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.setConfigLocation("com.nikolay.exam.config"); servletContext.addListener(new ContextLoaderListener(root)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(root)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); } } 

But when I run my application on tomcat, I get an error message:

14-Feb-2015 11: 35: 29.825 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping was found for the HTTP request with URI [/ home /] in DispatcherServlet with named 'dispatcher' 14-Feb-2015 11: 35: 32.766 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying the web application directory / home / nikolay / apache-tomcat-8.0.9 / webapps / manager 14-Feb-2015 11: 35: 32.904 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying the web application directory / home / nikolay / apache-tomcat-8.0.9 / webapps / manager ended in 136 ms 14-Feb-2015 11: 35: 34.888 ATTENTION [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/ ho me /] on a DispatcherServlet named 'shipper'

+1
source share
2 answers

I think this is the way you incorrectly registered your configuration class.

Try using AnnotationConfigWebApplicationContext # register instead.

 AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.register(WebConfig.class); 
+1
source

Maybe you need to change

 dispatcher.addMapping("/*"); 

to

 dispatcher.addMapping("/"); 
-1
source

Source: https://habr.com/ru/post/982547/


All Articles