spring embeded jetty+cxf

2019-03-02 02:35发布

问题:

I'm running embedded jetty inside of a spring ioc container. The spring ioc contains also an embedded hsqldb which makes the whole configuration a nice and complete web application development environment (on a single JVM). Now I'm trying to add apache CXF to this environment to make the jetty host not only servlets but also web services.

Unfortunately I'm getting NullPointerException when attempting to access http://127.0.0.1:8080/cxf/* (servlets and static content are served ok). Any idea what's wrong?

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)

I'm initializing spring with:

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();
}

jetty-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>

classpath:

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:

I've found out that the above configuration was wrong:

I did not use any WebApplicationContext with has to be initialized prior to the CXFServlet. The simple solution is to use the non-spring version of CXFServlet - it does not require any ApplicationContext (I don't want to create a separate ApplicationContext for the Web Services since I need to have shared DataSource between them) but it has too be initialized (Web Services must be published in the code).

After solving the Context issue I've found out that jetty 6.1 does not initialize the servlet context in proper way, everything works fine after updating to 7.x.

After writing custom WebApplicationContext I was able to use the main/root ApplicationContext as the WebApplicationContext allowing me to use the spring version of CXFServlet with about 6 lines of glue code.



标签: spring Jetty cxf