开发者

ServletContext is null when using spring to load jetty with jersey/jax-ws

We're using spring to configure jetty and load everything up, and I'm trying to use jersey for web services and use the jetty/spring library so that I can autowire some service classes into my jersey resources, but I'm having trouble starting up the server because it always says that the ServletContext is null.

Here's my spring config xml:

        <property name="handler">
        <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerList">
            <property name="handlers">
                <list>
                    <bean class="org.eclipse.jetty.servlet.ServletContextHandler">
                        <property name="eventListeners">
                            <list>
                                <bean class="org.springframework.web.context.ContextLoaderListener" />
                                <bean
                                    class="org.springframework.web.context.request.RequestContextListener" />
                            </list>
                        </property>
                        <property name="initParams">
                            <map>
                                <entry>
                                    <key>
                                        <value>contextConfigLocation</value>
                                    </key>
                                    <value>classpath:applicationContext.xml</value>
                                </entry>
                            </map>
                        </property>
                    </bean>
                    <bean class="org.eclipse.jetty.servlet.ServletHandler">



                        <property name="servlets">
                            <list>
                                <bean class="org.eclipse.jetty.servlet.ServletHolder">
                                    <property name="name" value="Jersey" />
                                    <property name="servlet">
                                        <bean
                                            class="com.sun.jersey.spi.spring.container.servlet.SpringServlet" />
                                    </property>
                                    <property name="initParameters">
                                        <map>
                                            <entry>
                                                <key>
                                                    <value>com.sun.jersey.config.property.packages</value>
                                                </key>
                                                <value>com.mycompany.ws.resource</value>
                                            </entry>
                                            <entry>
                                                <key>
                                                    <value>contextConfigLocation</value>
                                                </key>
                                                <value>classpath:applicationContext.xml</value>
                                            </entry>
                                        </map>
                                    </property>

                                </bean>
                            </list>
                        </property>

I followed the instructions here: http://blogs.oracle.com/enterprisetechtips/entry/jersey_and_spring to integrate jersey and spring, but the difference is that i have an embedded jetty server configured with spring.

I'm not sure why i keep getting the following error. I thought the jetty ServletContextHandler was the servlet context from their documentation.

2010-11-30 23:19:05,177 [Thread-0] INFO  [org.springframework.web.context.ContextLoader]: Root WebApplicationContext: initialization completed in 177 ms
Nov 30, 2010 11:19:05 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.traveladnetwork.ws.resource.ad.server
Nov 30, 2010 11:19:05 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Root resource classes found:
  class com.mycompany.ws.resource.TargetResource
  class com.mycompany.ws.resource.ReportResource
  class com.mycompany.ws.resource.HelloWorldResource
Nov 30, 2010 11:19:05 PM com.sun.jersey.api.core.PackagesResou开发者_开发问答rceConfig init
INFO: Provider classes found:
Nov 30, 2010 11:19:05 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
INFO: Creating new child context from classpath:applicationContext.xml
Nov 30, 2010 11:19:05 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
**SEVERE: Exception occurred when intialization
java.lang.IllegalArgumentException: ServletContext must not be null**
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:96)
    at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:86)
    at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:68)
    at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getDefaultContext(SpringServlet.java:128)
    at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getChildContext(SpringServlet.java:119)
    at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getContext(SpringServlet.java:113)
    at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:99)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:253)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:521)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:199)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:308)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:471)
    at javax.servlet.GenericServlet.init(GenericServlet.java:241)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:421)
    at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:245)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:699)
    at org.eclipse.jetty.servlet.ServletHandler.doStart(ServletHandler.java:155)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:165)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:92)
    at org.eclipse.jetty.server.Server.doStart(Server.java:228)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1414)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at com.mycompany.common.service.SpringServiceImpl.startService(SpringServiceImpl.java:52)
    at com.traveladnetwork.common.service.StartImpl.run(StartImpl.java:42)
    at java.lang.Thread.run(Thread.java:637)
2010-11-30 23:19:05.412:INFO::Started SelectChannelConnector@0.0.0.0:9932
2010-11-30 23:19:05,413 [Thread-0] INFO  [com.mycompany.common.service.StartImpl]: Started in 1950 ms


It could be that because you're running your servlet container within Spring (rather then the other way around) it can't find the ServletContext.

You've got two options:

  1. Try starting up Jetty with a programmatic bootstrap which loads up Spring inside it's Servlet context (Deploying a servlet programmatically with Jetty)
  2. Split your applicationContext.xml into two container definitions: those which are needed to start Jetty and those which can't start without a valid ServletContext. The ones which need a valid servlet context should probably be in the "initParams" for the bean class "ServletContextHandler", whereas currently you've got the same applicationContext.xml defined twice.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜