分分鐘帶你玩轉 Web Services【2】CXF

來源:互聯網
上載者:User

標籤:param   size   pen   servlet   ebs   引入   webp   etc   ram   

   在實踐中一直在使用 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 在本地跑起來,一切就都明朗了。

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

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>
3. 小結

   Servlet 發布 CXF WebService 依賴的開源庫較少,也就是說出問題的機率較小;

   每次新增 WebService 服務需要修改 WebServicesServlet 類。

   Spring Context 託管 WebService 實作類別,新增服務類可配置在 cxf-servlet.xml 中;

   有優秀的容器替你管理,你會很舒服,大型複雜 WebService 建議配置 Spring 使用。



分分鐘帶你玩轉 Web Services【2】CXF

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.