JAVAEE——BOS物流項目07:WebService入門、apache CXF入門、基於CXF發布CRM服務,javaeecxf

來源:互聯網
上載者:User

JAVAEE——BOS物流項目07:WebService入門、apache CXF入門、基於CXF發布CRM服務,javaeecxf
1 學習計劃

1、WebService入門

n 什麼是WebService

n 調用網路上的WebService服務

n SOAP和WSDL概念

n 基於JDK1.7發布一個簡單的WebService服務

n 使用JDK的wsimport命令產生本地代碼調用WebService服務

2、apache CXF入門

n CXF簡介

n CXF入門案例(服務端開發)

n CXF入門案例(用戶端開發)

1)使用WSDL2Java命令產生本地代碼調用

2)在設定檔中註冊代理對象調用

3、基於CXF發布CRM服務

n 建立CRM資料庫和CRM客戶表

n 發布服務

 

 

2 WebService入門2.1 什麼是WebService

Web service是一個平台獨立的,低耦合的,自包含的、基於可程式化的web的應用程式,可使用開放的XML(標準通用標記語言 (SGML)下的一個子集)標準來描述、發布、發現、協調和配置這些應用程式,用於開發分布式的互操作的應用程式。[1]  

Web Service技術, 能使得運行在不同機器上的不同應用無須藉助附加的、專門的第三方軟體或硬體, 就可相互交換資料或整合。依據Web Service規範實施的應用之間, 無論它們所使用的語言、 平台或內部協議是什麼, 都可以相互交換資料。Web Service是自描述、 自包含的可用網路模組, 可以執行具體的業務功能。Web Service也很容易部署, 因為它們基於一些常規的產業標準以及已有的一些技術,諸如標準通用標記語言 (SGML)下的子集XML、HTTP。Web Service減少了應用介面的花費。Web Service為整個企業甚至多個組織之間的商務程序的整合提供了一個通用機制。

 

 

l WebService的特點

  • WebService通過HTTP POST方式接受客戶的請求
  • WebService與用戶端之間一般使用SOAP協議傳輸XML資料
  • 它本身就是為了跨平台或跨語言而設計的

 

2.2 調用網路上的WebService服務

http://webxml.com.cn/

 

2.3 SOAP和WSDL概念2.3.1 SOAP(Simple Object Access Protocol):簡易物件存取通訊協定 (SOAP)

n SOAP作為一個基於XML語言的協議用於在網上傳輸資料。

n SOAP = 在HTTP的基礎上+XML資料。

n SOAP是基於HTTP的。

  u SOAP的組成如下:

    l Envelope – 必須的部分。以XML的根項目出現。

    l Headers – 可選的。

    l Body – 必須的。在body部分,包含要執行的伺服器的方法。和發送到伺服器的資料。

 

樣本:

POST /WebServices/IpAddressSearchWebService.asmx HTTP/1.1Host: ws.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getCountryCityByIp"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getCountryCityByIp xmlns="http://WebXml.com.cn/">      <theIpAddress>string</theIpAddress>    </getCountryCityByIp>  </soap:Body></soap:Envelope>

 

 

2.3.2 WSDL Web服務描述語言

WSDL(WebService Description Language):web 服務描述語言

就是一個xml文檔,用於描述當前服務的一些資訊(服務名稱、服務的發布地址、服務提供的方法、方法的參數類型、方法的傳回值類型等)

 

3 基於jdk1.7發布一個WebService服務3.1 服務端發布

第一步:建立一個Java項目

第二步:建立一個類,加入Webservice註解

第三步:提供一個方法sayHello

第四步:在main方法中調用jdk提供的發布服務的方法

第五步:訪問服務的wsdl文檔(服務的發布地址+?wsdl)http://192.168.115.87:8080/hello?wsdl

@WebServicepublic class HelloService {    public String sayHello(String name,int i){        System.out.println("服務端的sayHello方法被調用了。。。。");        return "helle" + name;    }        public static void main(String[] args) {        String address = "http://192.168.115.87:8080/hello";        Object implementor = new HelloService();        Endpoint.publish(address, implementor);    }}

 

 

3.2 用戶端調用3.2.1 jdk中wsimport命令使用

作用:解析wsdl檔案,產生用戶端本地代碼

 

 

3.2.2 用戶端調用

 1、使用wsimport命令解析wsdl檔案產生本地代碼

 2、通過本地代碼建立一個代理對象

 3、通過代理對象實現遠程調用

 

/** * 1、使用wsimport命令解析wsdl檔案產生本地代碼 * 2、通過本地代碼建立一個代理對象 * 3、通過代理對象實現遠程調用 * @author zhaoqx * */public class App {    public static void main(String[] args) {        HelloServiceService ss = new HelloServiceService();        //建立用戶端代理對象,用於遠程調用        HelloService proxy = ss.getHelloServicePort();        String ret = proxy.sayHello("小明", 10);        System.out.println(ret);    }}

 

 

 

4 apache CXF入門4.1 下載

官網:cxf.apache.org

 

下載CXF的開發包:

 

 

解壓上面的zip檔案:

 

 

 

l Apache CXF = Celtix + Xfire

l 支援多種協議:

  • SOAP1.1,1.2
  • XML/HTTP
  • CORBA(Common Object Request Broker Architecture公用對象請求代理體繫結構,早期語言使用的WS。C,c++,C#)
  • 並可以與Spring進行快速無縫的整合
  • 靈活的部署:可以運行在Tomcat,Jboss,Jetty(內建),IBMWS,BeaWL上面。

 

4.2 入門案例(服務端開發)

第一步:建立動態web項目

第二步:匯入CXF相關jar包

 

 

第三步:在web.xml中配置CXF架構提供的一個Servlet

  <!-- 配置CXF架構提供的Servlet -->  <servlet>      <servlet-name>cxf</servlet-name>      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>      <!-- 通過初始化參數指定CXF架構的設定檔位置 -->      <init-param>          <param-name>config-location</param-name>          <param-value>classpath:cxf.xml</param-value>      </init-param>  </servlet>  <servlet-mapping>      <servlet-name>cxf</servlet-name>      <url-pattern>/service/*</url-pattern>  </servlet-mapping>

 

 

第四步:在類路徑下提供cxf.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"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beans                     http://www.springframework.org/schema/beans/spring-beans.xsd                    http://cxf.apache.org/bindings/soap                     http://cxf.apache.org/schemas/configuration/soap.xsd                    http://cxf.apache.org/jaxws                     http://cxf.apache.org/schemas/jaxws.xsd">    <!-- 引入CXF Bean定義如下,早期的版本中使用 -->    <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>

 

第五步:開發一個介面和實作類別

 

 

第六步:在cxf.xml中註冊服務

 

 

4.3 入門案例(用戶端開發)

l 方式一:使用jdk提供的wsimport命令產生本地程式碼完成調用

 

 

l 方式二:使用CXF提供的方式(重點)

第一步:建立Java項目並匯入CXF相關jar包

第二步:使用wsimport或者CXF提供wsdl2java產生本地代碼,只需要產生介面檔案

 

 

第三步:將介面檔案複製到項目中

 

 

第四步:提供spring設定檔,註冊用戶端代理對象

 

 

第五步:讀取spring設定檔,建立spring工廠,從工廠中擷取代理對象,實現遠程調用

 

 

5 基於CXF開發crm服務5.1 資料庫環境搭建

 

 

執行sql指令碼:

 

 

5.2 web項目環境搭建

第一步:建立動態web項目

第二步:匯入CXF相關jar包

第三步:配置web.xml

 <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:cxf.xml</param-value>  </context-param>    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <!-- 配置CXF架構提供的Servlet -->  <servlet>      <servlet-name>cxf</servlet-name>      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name>cxf</servlet-name>      <url-pattern>/service/*</url-pattern>  </servlet-mapping>

第四步:在類路徑下提供cxf.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"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beans                     http://www.springframework.org/schema/beans/spring-beans.xsd                    http://cxf.apache.org/bindings/soap                     http://cxf.apache.org/schemas/configuration/soap.xsd                    http://cxf.apache.org/jaxws                     http://cxf.apache.org/schemas/jaxws.xsd">    <!-- 引入CXF Bean定義如下,早期的版本中使用 -->    <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>

第五步:針對t_customer表建立一個Customer客戶實體類

 

 

Customer的空參方法需要保留,在wsimport產生代碼的時候才會有相應的java

 

第六步:開發一個介面和實作類別

 

 

第七步:配置cxf.xml

    <!-- 配置資料來源 -->    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql:///crm_heima32"/>        <property name="username" value="root"/>        <property name="password" value="root"/>    </bean>        <!-- 交易管理員 -->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>        <!-- 支援事務註解 -->    <tx:annotation-driven transaction-manager="txManager"/>        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"/>    </bean>        <bean id="customerService" class="com.itheima.crm.service.CustomerServiceImpl">        <property name="jdbcTemplate" ref="jdbcTemplate"/>    </bean>        <!-- 註冊服務 -->    <jaxws:server id="myService" address="/customer">        <jaxws:serviceBean>            <ref bean="customerService"/>        </jaxws:serviceBean>    </jaxws:server>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.