Once I moved my test application to a productive (?) application and started testing on Tomcat, my Spring 3.1 based REST services have stopped working. Though the default index.jsp is displayed, my application (http://myhost:myport/test-webapp/myrestservice) cannot be accessed and I get The requested resource (/test-webapp/myrestservice) is not available.
Here is what I did:
Controller:
package com.test.webapp.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;
import com.test.webap.entity.Account;
@Controller
@RequestMapping("/myrestservice")
public class AccountController{
@RequestMapping(method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Account getEntity() {
// ... <simplified>
return new Account(//...);
}
}
Dispatch Servlet configuration:
package com.test.webapp.web;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
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 com.test.webapp.config.AppConfig;
public class AppInit implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext container) throws ServletException {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(dispatcherContext));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet(
DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Configuration class:
package com.test.webapp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan( basePackages = {"com.test.webapp.controller"})
public class AppConfig{
// Nothing here. Once I start using the beans, I will put them here
}
And, web.xml has not much in it:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Test Web App</display-name>
</web-app>
There is nothing apart from this in my project. Am I missing something? The old project which was doing a bit more like logging the incoming JSON requests etc (which got deleted now) was working, but I do not have it anymore :( So, lots of newbie blues. Please help me here. Thanks a lot!!
Update Issue is resolved. Check the answer
What version of Tomcat are you using in your production environment? It looks like you will need Tomcat 7 to get the Servlet 3.0 spec implementation needed to support your WebApplicationInitializer.
Issue resolved. I felt that this is a Servlet container 3.0 issue and may be something in my Tomcat 7 config is messed up. But, I did some minor changes to web.xml
and it works fine now:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<display-name>Test Web App</display-name>
</web-app>
Apparently, I had to explicitly tell Tomcat to scan for the implementations of ServletContainerInitializer and not everything is in web.xml via the attribute metadata-complete="false"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With