spring embedded jetty + cxf

时间:2010-09-17 14:47:16

标签: spring jetty cxf

我正在弹簧ioc容器内运行嵌入式码头。 spring ioc还包含一个嵌入式hsqldb,它使整个配置成为一个漂亮而完整的Web应用程序开发环境(在单个JVM上)。现在我正在尝试将apache CXF添加到此环境中,以使jetty主机不仅是servlet而且还是Web服务。

不幸的是,在尝试访问http://127.0.0.1:8080/cxf/ *时,我收到了NullPointerException(servlet和静态内容服务正常)。知道什么是错的吗?

2010-09-17 15:37:20,099 [btpool0-1 - /cxf/] DEBUG org.mortbay.log - REQUEST /cxf/ on org.mortbay.jetty.HttpConnection@2d14d18f
2010-09-17 15:37:20,100 [btpool0-1 - /cxf/] DEBUG org.mortbay.log - servlet=cxf-servlet-holder
2010-09-17 15:37:20,100 [btpool0-1 - /cxf/] DEBUG org.mortbay.log - chain=null
2010-09-17 15:37:20,100 [btpool0-1 - /cxf/] DEBUG org.mortbay.log - servelet holder=cxf-servlet-holder
2010-09-17 15:37:20,101 [btpool0-1 - /cxf/] ERROR org.mortbay.log - /cxf/
java.lang.NullPointerException
    at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:367)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at org.mortbay.jetty.Server.handle(Server.java:281)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:502)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:821)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:208)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:378)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:368)
    at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

我正在用以下内容初始化spring:

public static void main(String[] args) throws Exception {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "jetty-beans.xml" , "cxf-beans.xml" });
    applicationContext.getBean("web-server", Server.class).join();
}

码头-beans.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="web-server" class="org.mortbay.jetty.Server"
        init-method="start">
        <property name="connectors">
            <list>
                <bean class="org.mortbay.jetty.nio.SelectChannelConnector">
                    <property name="host" value="localhost" />
                    <property name="port" value="8080" />
                </bean>
            </list>
        </property>
        <property name="handlers">
            <list>
                <ref bean="web-server-context-static" />
                <ref bean="web-server-context-servlet" />
                <ref bean="web-server-context-cxf" />
            </list>
        </property>
    </bean>

    <bean name="web-server-context-static" class="org.mortbay.jetty.servlet.Context">
        <property name="contextPath" value="/static" />
        <property name="handler">
            <bean class="org.mortbay.jetty.handler.ResourceHandler">
                <property name="resourceBase" value="static" />
            </bean>
        </property>
    </bean>

    <bean name="web-server-context-servlet" class="org.mortbay.jetty.servlet.Context">
        <property name="contextPath" value="/servlet" />
        <property name="handler">
            <bean class="org.mortbay.jetty.servlet.ServletHandler">
                <property name="servlets">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletHolder">
                            <property name="name" value="servlet-holder" />
                            <property name="servlet">
                                <bean class="test.TestServlet" />
                            </property>
                        </bean>
                    </list>
                </property>
                <property name="servletMappings">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletMapping">
                            <property name="servletName" value="servlet-holder" />
                            <property name="pathSpec" value="/*" />
                        </bean>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

    <bean name="web-server-context-cxf" class="org.mortbay.jetty.servlet.Context">
        <property name="contextPath" value="/cxf" />
        <property name="handler">
            <bean class="org.mortbay.jetty.servlet.ServletHandler">
                <property name="servlets">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletHolder">
                            <property name="name" value="cxf-servlet-holder" />
                            <property name="servlet">
                                <bean class="org.apache.cxf.transport.servlet.CXFServlet">
                                </bean>
                            </property>
                        </bean>
                    </list>
                </property>
                <property name="servletMappings">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletMapping">
                            <property name="servletName" value="cxf-servlet-holder" />
                            <property name="pathSpec" value="/*" />
                        </bean>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

</beans>

CXF-beans.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="test-ws-impl" class="test.TestWSImpl" />

    <jaxws:endpoint id="test-ws-endpoint" implementor="#test-ws-impl"
        address="/testWS" />

</beans>

类路径:

activation-1.1.jar
commons-logging-1.1.1.jar
cxf-2.2.10.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jcl-over-slf4j-1.6.1.jar
jetty-6.1.3.jar
jetty-annotations-6.1.3.jar
jetty-util-6.1.3.jar
log4j-1.2.16.jar
neethi-2.0.4.jar
org.springframework.aop-3.0.3.RELEASE.jar
org.springframework.asm-3.0.3.RELEASE.jar
org.springframework.beans-3.0.3.RELEASE.jar
org.springframework.context-3.0.3.RELEASE.jar
org.springframework.context.support-3.0.3.RELEASE.jar
org.springframework.core-3.0.3.RELEASE.jar
org.springframework.expression-3.0.3.RELEASE.jar
servlet-api-2.5-6.1.3.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar

TestWSImpl.java:

package test;

import javax.jws.WebService;

@WebService(endpointInterface = "test.TestWSInterface")
public class TestWSImpl implements TestWSInterface {

    @Override
    public String doTest(String testSubject) {
        return "successfully tested:" + testSubject;
    }

}

TestWSInterface.java

package test;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface TestWSInterface {

    String doTest(@WebParam String testSubject);

}

1 个答案:

答案 0 :(得分:3)

我发现上面的配置错了:

我没有使用任何WebApplicationContext,必须在CXFServlet之前进行初始化。简单的解决方案是使用非弹簧版本的CXFServlet - 它不需要任何ApplicationContext(我不想为Web服务创建单独的ApplicationContext,因为我需要在它们之间共享DataSource)但它也是已初始化(Web服务必须在代码中发布)。

在解决了Context问题后,我发现jetty 6.1没有以正确的方式初始化servlet上下文,更新到7.x后一切正常。

在编写自定义WebApplicationContext之后,我能够使用main / root ApplicationContext作为WebApplicationContext,允许我使用大约6行胶水代码的弹簧版CXFServlet。