1

我需要注册一个没有 Spring 配置的 SOAP 服务。我知道在 RESTFUL 服务的情况下可以做到这一点(通过使用 CXFNonSpringJaxrsServlet 并在 web.xml 中进行配置),但我怎么能对 SOAP 服务使用类似的东西呢?这是我原来的 applicationContext.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:cxf="http://cxf.apache.org/core"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
   http://cxf.apache.org/core
   http://cxf.apache.org/schemas/core.xsd
   http://cxf.apache.org/jaxws
   http://cxf.apache.org/schemas/jaxws.xsd
   http://cxf.apache.org/jaxrs
   http://cxf.apache.org/schemas/jaxrs.xsd"
   default-autowire="byName">


<!-- Load CXF modules from cxf.jar -->
<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"/>
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

<bean id="businessServiceabilityService" 
      class="XXXXXXXXXXXXXXXXXXServiceImpl"
      autowire="autodetect"/>
<bean id="jaxbBean"
      class="org.apache.cxf.jaxb.JAXBDataBinding"
      scope="prototype"/>
<bean id="jaxws-and-aegis-service-factory"
      class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"
      scope="prototype">
    <property name="dataBinding" ref="jaxbBean"/>
    <property name="serviceConfigurations">
        <list>
            <bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>
            <bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>
            <bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>
        </list>
    </property>
</bean>

<!-- JAX-RS -->
<jaxrs:server id="businessServiceabilityServiceJaxRsServer" address="/bs/rest">
    <jaxrs:serviceBeans>
        <ref bean="businessServiceabilityService"/>
    </jaxrs:serviceBeans>
</jaxrs:server>

<!-- JAX-WS -->
<jaxws:endpoint implementor="#businessServiceabilityService" address="/bs/soap">
    <jaxws:serviceFactory>
        <ref bean="jaxws-and-aegis-service-factory"/>
    </jaxws:serviceFactory>
</jaxws:endpoint>

如何注册 SOAP 服务?我在 web.xml 中找不到这样做的任何可能性。web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">
<display-name></display-name>

<servlet>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>jaxrs.providers</param-name>
        <param-value>XXXXXXXXXXXXXXXXXXXServiceImpl</param-value>
    </init-param>
    <init-param>
        <param-name>jaxrs.serviceClasses</param-name>
        <param-value>XXXXXXXXXXXXXXXXXXXServiceImpl</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <url-pattern>/cxf/rest/*</url-pattern>
</servlet-mapping>

先感谢您!五。

4

2 回答 2

3

Spring 框架对于使用 CXF 创建 web 服务是可选的,它将显着减少 pacakge 大小,因为您只需要以下 jars [截至使用 CXF 2.7.3]

        commons-logging-1.1.1.jar
        cxf-2.7.3.jar
        httpasyncclient-4.0-beta3.jar
        httpclient-4.2.1.jar
        httpcore-4.2.2.jar
        httpcore-nio-4.2.2.jar
        neethi-3.0.2.jar
        wsdl4j-1.6.2.jar
        xmlschema-core-2.0.3.jar

以下是 JAX-WS 服务的示例

http://www.javatips.net/blog/2012/10/cxf-soap-without-spring

以下是 JAX-RS 服务的示例

http://www.javatips.net/blog/2012/10/cxf-rest-without-spring

于 2014-02-15T03:46:54.750 回答
2

我找到了解决方案。

public class MyServiceServlet extends CXFNonSpringServlet {

private static final long serialVersionUID = 1L;

@Override
// Called at startup time to register this web service.
public void loadBus(ServletConfig servletConfig) throws ServletException {
    super.loadBus(servletConfig);

    Bus bus = getBus();
    BusFactory.setDefaultBus(bus);

    createFactoryBean(servletConfig);
}

private void createFactoryBean(ServletConfig servletConfig) {
    String address = servletConfig.getInitParameter("jaxws.address");
    String provider = servletConfig.getInitParameter("jaxws.provider");
    String serviceClass = servletConfig.getInitParameter("jaxws.serviceClass");

    JaxWsServerFactoryBean fb = new JaxWsServerFactoryBean();
    fb.setAddress(address);
    try {
        fb.setServiceBean(Class.forName(provider).newInstance());
    } catch (Exception e) {
        throw new RuntimeException("The class '"+provider+"' cannot be instantiated!", e);
    }
    try {
        fb.setServiceClass(Class.forName(serviceClass).getClass());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("The class '"+serviceClass+"' cannot be found!", e);
    } 
    fb.create();
}
}

和 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

<display-name></display-name>

<!-- CXF config -->
<servlet>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>jaxrs.providers</param-name>
        <param-value>xxx.BusinessServiceabilityServiceImpl</param-value>
    </init-param>
    <init-param>
        <param-name>jaxrs.serviceClasses</param-name>
        <param-value>xxx.BusinessServiceabilityServiceImpl</param-value>
    </init-param>
</servlet>

<servlet>
    <servlet-name>MyServiceServlet</servlet-name>
    <servlet-class>xxx.MyServiceServlet</servlet-class>
    <init-param>
        <param-name>jaxws.address</param-name>
        <param-value>/almafa</param-value>
    </init-param>
    <init-param>
        <param-name>jaxws.serviceClass</param-name>
        <param-value>com.infoaxon.bpt.services.OACCostingServiceSoap</param-value>
    </init-param>
    <init-param>
        <param-name>jaxws.provider</param-name>
        <param-value>xxx.BusinessServiceabilityServiceImpl</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
    <url-pattern>/cxf/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>MyServiceServlet</servlet-name>
    <url-pattern>/cxf/soap/*</url-pattern>
</servlet-mapping>

</web-app>
于 2012-05-30T16:29:52.107 回答