简体   繁体   中英

GWT + Spring: NullPointerException on getServletContext() call

I'm currently experimenting with GWT and Spring. More specifically I wanted to make the GreetingService sample to work with Spring on the server side. There are a couple of articles available for realizing this (I'm linking them here since some of you may be interested):

Now I followed the mentioned instructions and when launching everything in the GWT hosted mode, the service on the server-side is also called successfully. But then before the response is being sent back to the client, I get the a NullPointerException when the getServletContext() is being called internally by some Spring framework class. The stacktrace is the following:

WARNING: Nested in org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException:
java.lang.NullPointerException
    at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:163)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.doUnexpectedFailure(RemoteServiceServlet.java:284)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:99)
    at com.jsdev.devbook.server.GWTController.handleRequest(GWTController.java:51)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:556)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:121)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:313)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at org.mortbay.jetty.Server.handle(Server.java:313)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:844)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:644)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
    at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

Here's the list of libs I deploy on my server / GWT hosted mode directory:

antlr-3.0.1.jar
appengine-api-1.0-sdk-1.2.5.jar
appengine-api-labs-1.2.5.jar
commons-logging.jar
datanucleus-appengine-1.0.3.jar
datanucleus-core-1.1.5.jar
datanucleus-jpa-1.1.5.jar
geronimo-jpa_3.0_spec-1.1.1.jar
geronimo-jta_1.1_spec-1.1.1.jar
geronimo-servlet_2.5_spec-1.2.jar
gwt-servlet.jar
jdo2-api-2.3-eb.jar
org.springframework.asm-3.0.0.RC1.jar
org.springframework.beans-3.0.0.RC1.jar
org.springframework.context-3.0.0.RC1.jar
org.springframework.context.support-3.0.0.RC1.jar
org.springframework.core-3.0.0.RC1.jar
org.springframework.expression-3.0.0.RC1.jar
org.springframework.orm-3.0.0.RC1.jar
org.springframework.web-3.0.0.RC1.jar
org.springframework.web.servlet-3.0.0.RC1.jar
spring-dao.jar

Even more info:
developing on Mac OSX, Java version 1.6.0_15.

Does someone have a clue what could be the problem here?

Thx a lot.

Just found the issue. The GWTController implementation has to be made Servlet context aware. This can be done by implementing the ServletContextAware interface and overriding the getServletContext() and setServletContext(..) methods for letting the context be injected.

I just published a blog post that describes the issue and provides the solution in more detail.

We had a similar problem and found that the private transient ServletConfig config field on javax.servlet.GenericServlet was null. GWT's RemoteServiceServlet was calling log(...) which in turn called getServletName() which NPE'ed due to config being null.

Our solution was to overide getServletName() :

public String getServletName() {
    // Override as GenericServlet does config.getServletName() which NPEs
    // as config is null. This causes NPEs when the log(...) methods are
    // invoked.
    return service.getClass().getSimpleName();
}

You could figure out why the ServletConfig is not being set or override some methods like we did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM