分分钟带你玩转 Web Services【2】CXF

   在实践中一直在使用 JAX-WS 构建 WebService 服务,服务还是非常稳定、高效的。

   但还是比较好奇其他的 WebService 开源框架,比如:CXF/Axis2/Spring WS等。

   源于对 Apache 的信赖和喜爱, 旗下的 CXF  WebService 肯定也不会让人失望。

   所以花了点时间将 CXF 引入到项目实践当中,多一种选择也未尝不可。

   对于 WebService 和 CXF 简介这里就不赘述了,不太懂的同学请先移步:分分钟带你玩转 Web Services【1】JAX-WS

   本篇试从 Servlet 发布 CXF WebService 和  Spring 托管 CXF WebService 两种方式,带你玩转 CXF。

   Servlet 发布 CXF WebService git demo地址:http://git.oschina.net/LanboEx/cxf-demo

   Spring 托管 CXF WebService git demo地址: http://git.oschina.net/LanboEx/cxf-spring-demo

   需要有这方面实践的同学,请收藏这篇博客,到时只需将 Demo 在本地跑起来,一切就都明朗了。

   官网地址(如果你对你英文很自信的话):http://cxf.apache.org/docs/index.html

1. Servlet 发布 CXF WebService 

a.mavn 依赖 Jar:

       <!--web 容器支持-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!--apache cxf webservice-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.11</version>
        </dependency>

   依赖的 Jar 基本上都是 sum/apache/codehaus 支持,这些 Jar 已经过岁月洗涤,稳定高效。

b.服务实现:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface UserService {

    /**
     * 执行测试的WebService方法(有参)
     */
    @WebMethod
    String sayHi(@WebParam(name = "name") String name);
}
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class UserServiceImpl implements UserService {

    @WebMethod
    public String sayHi(String name) {
        return "Hi, " + name + "! ";
    }
}

c.Servlet 实现:

public class WebServicesServlet extends CXFNonSpringServlet {
    private static final long serialVersionUID = -5314312869027558456L;

    @Override
    protected void loadBus(ServletConfig servletConfig) {
        super.loadBus(servletConfig);
        Endpoint.publish("/UserService", new UserServiceImpl());
    }
}

   你没有看错 CXF 提供的不集成 Spring 的 Servlet 就叫做 CXFNonSpringServlet,是不是有点俗。

   使用起来也很简单,实现 org.apache.cxf.transport.servlet.CXFNonSpringServlet 中的 loadBus 方法即可。

d.web.xml 配置:

    <servlet>
        <servlet-name>cxfwsServlet</servlet-name>
        <servlet-class>com.rambo.cxf.demo.ws.servlet.WebServicesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxfwsServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

   启动工程,访问:http://localhost:4042/cxf-demo/ws/UserService?wsdl

   Servlet 发布 CXF WebService 方式在后续推广使用中发现,部署 weblogic 时会出现错误:

   java.lang.IllegalArgumentException: Cannot create URL for this address /XXXXXX

   而该错误需要降低 CXF 版本才能顺利解决,这已经违背了我们项目开发的原则。

   参考地址:http://git.net/ml/users-cxf-apache/2014-07/msg00027.html

2. Spring 托管 CXF WebService

a.mavn 依赖 Jar:

        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!--apache cxf webservice-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.11</version>
        </dependency>

   只需要依赖 SpringWeb 即可,其中 Spring Context 来托管 WebService 服务的实现类。

   服务实现和 web.xml 与 Servlet 发布 是一致的,不需要进行特殊的处理,这里就不贴了。

b. 将 WebService 服务托管给 Spring Context 

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

    <bean id="userService" class="com.rambo.cxf.spring.demo.impl.UserServiceImpl"/>

    <jaxws:endpoint id="userServiceWs" implementor="#userService" address="/UserService"/>
</beans>

   启动工程,访问:http://localhost:4047/cxf-spring-demo/ws/UserService?wsdl

3. 小结

   Servlet 发布 CXF WebService 依赖的开源库较少,也就是说出问题的概率较小;

   每次新增 WebService 服务需要修改 WebServicesServlet 类。

   Spring Context 托管 WebService 实现类,新增服务类可配置在 cxf-servlet.xml 中;

   有优秀的容器替你管理,你会很舒服,大型复杂 WebService 建议配合 Spring 使用。

4. CXF 客户端

   当然客户端的使用也有两种方式:

   a. 使用工具或者命令行,根据 wsdl 文档生成调用代码,进行调用访问(在上篇中描述过);

   b. 配合 spring 进行配置,无需生成客户端代码,交由 spring  统一管理;

  <!-- 方式一:基于spring的jaxws -->
    <jaxws:client id="UserService"
                  serviceClass="com.rambo.cxf.spring.demo.ws.inter.UserService"
                  address="http://localhost:4047/cxf-spring-demo/ws/UserService">
    </jaxws:client>

    <!-- 方式二:基于cxf的JaxWsProxyFactoryBean -->
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.rambo.cxf.spring.demo.inter.UserService"/>
        <property name="address" value="http://localhost:4047/cxf-spring-demo/ws/UserService"/>
    </bean>

   a 方式被调用方项目不需要添加任何依赖,即可使用客户端代码进行调用访问。

   但当服务中涉及交互的安全控制时,还是需要将所依赖添加至项目中,所依赖的 jar 相当庞大。

   涉及安全认证时使用 spring 方式配置是最妥当的,引入服务的接口层和相关的依赖即可。

 

 


posted @ 2017-04-26 23:55  Orson  阅读(1182)  评论(0编辑  收藏  举报