I am trying to migrate our application to use Spring 4 features. So I am slowly trying to remove XML config (web.xml, applicationContext.xml, dispatcher-servlet.xml) and replace with Java annotations. I spent lot of time on this problem, I could not find a solution in stackoverflow also. I am getting the below error:
org.springframework.web.servlet.PageNotFound - [] [line number - 1136] No mapping found for HTTP request with URI [/WP/xhStatus] in DispatcherServlet with name 'dispatcher'. POST request for "http://localhost:8080/WP/xhStatus" resulted in 404 (null); invoking error handler org.springframework.web.client.HttpClientErrorException: 404 null
From this error message, I tried to see if PageNotFound available in Spring API package org.springframework.web.servlet at the below URL, but no such class or interface available in that package: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/package-summary.html
I just added below two classes WebAppInitializer.java & WebConfig.java and removed some code from web.xml and dispatcher-servlet.xml.
WebAppInitializer.java
package com.mycompany.wp.web.config;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig");
servletContext.addListener(new ContextLoaderListener(applicationContext));
servletContext.setInitParameter("contextConfigLocation", "/WEB-INF/applicationContext.xml");
ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext));
dispatcherServlet.setLoadOnStartup(1);
dispatcherServlet.addMapping("/");
}
}
WebConfig.java
package com.mycompany.wp.web.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.mycompany.wp")
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
I removed this part from web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And also I removed the below part from dispatcher-servlet.xml:
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.mycompany.wp"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
Now dispatcher-servlet.xml has only one entry:
<mvc:annotation-driven />
I still have all XML files (web.xml, dispatcher-servlet.xml & applicationContext.xml) in my application as they still have some other info. But, there are no changes in applicationContext.xml.
I am trying to call a REST service (Spring MVC Controller) with the request mapping as /xhStatus, which I did not change anything. Earlier, my REST service was reachable at the URL mentioned above, but now I can't reach.
Before moving to Java configurations, my application used to work well with XML configuration. Probably, DispatcherServlet is not able to find my Spring Controller.
It would be great if someone can help me. Thank you.