Web service using CXF, Jetty and Spring

2019-04-14 18:44发布

问题:

I try to create simple web service with CXF, Jetty and Spring. I create service interface

@WebService
public interface AnonymousService {

    @WebMethod
    public String getVersion();
}

and its implementation

@Service("anonymousService")
@WebService(
    serviceName = "AnonymousService",
    targetNamespace = "http://ws.test/",
    endpointInterface = "test.ws.AnonymousService"
)
public class AnonymousServiceImpl {
    public String getVersion() {
        return "Version";
    }
}

Next step I add to Spring context configuration file endpoint declaration

<jaxws:endpoint id="anonymousServiceEndpoint"
    implementor="#anonymousService" address="/anonymous" />

and configure Jetty bean

<bean id="server" class="org.mortbay.jetty.Server" destroy-method="stop">
    <property name="connectors">
    <list>
            <bean id="connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
                <property name="port" value="${server.port}" />
            </bean>
        </list>
    </property>

    <property name="handlers">
        <list>
            <ref bean="workflowContext" />
        </list>
    </property>
 </bean>

<bean name="workflowContext" class="org.mortbay.jetty.servlet.Context">
    <property name="contextPath" value="/" />

        <property name="servletHandler">
            <bean class="org.mortbay.jetty.servlet.ServletHandler">
                <property name="servlets">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletHolder">
                            <property name="name" value="services" />
                            <property name="servlet">
                                <bean class="org.apache.cxf.transport.servlet.CXFServlet" />
                                <!--<bean class="org.apache.cxf.transport.servlet.CXFNonSpringServlet" />-->
                            </property>
                        </bean>
                    </list>
                </property>

                <property name="servletMappings">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletMapping">
                            <property name="servletName" value="services" />
                            <property name="pathSpec" value="/services/*" />
                        </bean>
                    </list>
                </property>
            </bean>
        </property>
</bean>

Unfortunately, when I start my application I get following stack trace

25.09.2010 14:48:07 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://ws.test/}AnonymousService from class name.krestjaninoff.application.interfaces.AnonymousService
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anonymousServiceEndpoint': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at name.krestjaninoff.application.Server.main(Server.java:20)
Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
    at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:275)
    at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:209)
    at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:404)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1536)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    ... 12 more
Caused by: org.apache.cxf.service.factory.ServiceConstructionException
    at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:139)
    at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:167)
    at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:346)
    at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:259)
    ... 21 more
Caused by: java.net.MalformedURLException: no protocol: /anonymous
    at java.net.URL.<init>(URL.java:583)
    at java.net.URL.<init>(URL.java:480)
    at java.net.URL.<init>(URL.java:429)
    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.<init>(JettyHTTPDestination.java:93)
    at org.apache.cxf.transport.http_jetty.JettyHTTPTransportFactory.createDestination(JettyHTTPTransportFactory.java:116)
    at org.apache.cxf.transport.http_jetty.JettyHTTPTransportFactory.getDestination(JettyHTTPTransportFactory.java:103)
    at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:90)
    at org.apache.cxf.endpoint.ServerImpl.<init>(ServerImpl.java:69)
    at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:118)
    ... 24 more

What I do wrong?

回答1:

My problem solved by adding cxf.xml configuration file into root of classpath

<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-3.0.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"/>

</beans>

I didn't do this before, because I didn't understand what should be in META_INF/cxf/.xml files. In fact, these files do not need to be created - they are already exists in cxf-rt- packages!



回答2:

Did you see the last exception in the chain:

Caused by: java.net.MalformedURLException: no protocol: /anonymous

The "/anonymous" string is in the jaxws:endpoint tag, so it probably uses another syntax there.