【碼】webservice+ApacheCXF基礎入門

聲明:基於某智播客課堂講義及講師課堂筆記,若有問題或者涉及到侵權,請聯繫刪除。html

  1. 介紹

  1. webService入門(瞭解)
  2. 基於jdk1.7開發webservice服務(瞭解)
  3. Apache CXF框架入門(掌握)
  4. 基於CXF框架搭建CRM系統(掌握)
  5. 基於eclipse+tomcat+Oracle10g+maven
  6. 本機同時運行客戶端和服務端,須要同時配置兩個tomcat,端口地址不能同樣,不然會端口衝突打開失敗

 

  1. webService入門

Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可以使用開放的XML標準通用標記語言下的一個子集)標準描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操做應用程序[1] java

 

 

Webservice跨平臺跨語言;c++

Java 客戶端 -----》 .net服務端web

  1. 基於jdk調用網絡上服務

http://www.webxml.com.cn/zh_cn/index.aspx :提供功能服務spring

 

  1. 找到服務的wsdl地址
  2. 經過jdk提供命令wsimport命令生成java調用代碼

  1. 將生成代碼拷貝到本地,測試代碼:

  1. soap協議:簡單對象訪問協議

規範wbeservice請求,響應數據格式:都是xml格式 sql

 

Request:數據庫

POST /WebServices/MobileCodeWS.asmx HTTP/1.1 apache

Host: ws.webxml.com.cn 編程

Content-Type: text/xml; charset=utf-8 tomcat

Content-Length: length

SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

 

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

<getMobileCodeInfo xmlns="http://WebXml.com.cn/">

<mobileCode>string</mobileCode>

<userID>string</userID>

</getMobileCodeInfo>

</soap:Body>

</soap:Envelope>

Response:

 

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

 

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

<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">

<getMobileCodeInfoResult>string</getMobileCodeInfoResult>

</getMobileCodeInfoResponse>

</soap:Body>

</soap:Envelope>

SOAP 1.2

  1. wsdl webservice描述語言

俗稱"web服務使用說明書"

 

網絡服務描述/定義語言:每個webservice服務都有本身wsdl

wsdl是標準xml文件,wsdl(xml文件)包含服務名稱,服務中包含方法名方法參數(參數類型),方法返回類型

 

經過jdk提供命令wsimport,解析wsdl(本質就是xml文件),生成客戶端java調用代碼(生成代碼方法名稱,方法參數,方法返回類型)。

 

WSDL地址:服務地址+?WSDL

  1. wsimport命令

 

 

  1. WebService入門基於jdk1.7發佈服務(瞭解)

    1. 基於jdk基於1.7調用網絡上服務

http://www.webxml.com.cn/zh_cn/index.aspx :提供功能服務

 

  1. 找到服務的wsdl地址

 

  1. 經過jdk提供命令wsimport命令生成java調用代碼

  1. 將生成代碼拷貝到本地,測試代碼:

  1. wsimpot命令

  1. soap簡單對象訪問協議

規範wbeservice請求數據,響應數據格式:xml -- 跨語言調用

 

Request:

POST /WebServices/MobileCodeWS.asmx HTTP/1.1

Host: ws.webxml.com.cn

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

 

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

<getMobileCodeInfo xmlns="http://WebXml.com.cn/">

<mobileCode>string</mobileCode>

<userID>string</userID>

</getMobileCodeInfo>

</soap:Body>

</soap:Envelope>

Response:

 

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

 

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

<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">

<getMobileCodeInfoResult>string</getMobileCodeInfoResult>

</getMobileCodeInfoResponse>

</soap:Body>

</soap:Envelope>

SOAP 1.2

  1. wsdl

webservice 描述語言。

俗稱:web服務使用說明書

 

網絡服務描述語言:每個webservice服務都有本身wsdl

Wsdl是標準xml文件,包含服務名稱,包含方法,方法參數,方法返回類型

 

經過命令wsimport,解析wsdl,生成java調用代碼。

 

  1. jdk1.7服務端發佈webservice服務(瞭解)

  1. 建立類,建立若干方法
  2. 在類上使用註解@WebService

  1. 發佈服務

  1. wsdl地址: 服務地址+?wsdl

  1. 客戶端調用

    1. wsimport生成java調用代碼

獲取服務的wsdl地址: 服務地址+?wsdl

 

wsimport – d <生成class文件目錄> -s<生成java文件目錄> -p <生成代碼包路徑> wsdl地址

 

解析wsdl(xml),生成本地代碼

 

  1. 經過命令解析wsdl生成代碼

  1. 將生成的java文件拷貝項目中
  2. 測試代碼

  1. Apache cxf框架入門

Apache CXF = Celtix + Xfire 支持多種協議: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker Architecture公共對象請求代理體系結構,早期語言使用的WS;C,c++,C#) 並能夠與Spring進行快速無縫的整合 靈活的部署:能夠運行在Tomcat,Jboss,Jetty(內置),IBMWS,BeaWL上面。

<!-- 導入webservice依賴 -->

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-jaxws</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-http</artifactId>

            <version>${cxf.version}</version>

        </dependency>

 

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-jaxrs</artifactId>

            <version>${cxf.version}</version>

        </dependency>

 

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-rs-client</artifactId>

            <version>${cxf.version}</version>

        </dependency>    

        

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-rs-extension-providers</artifactId>

            <version>${cxf.version}</version>

        </dependency>

 

  1. 服務端

  1. 建立web項目
  2. 課後資料中,cxf開發jar包(跟spring整合),導入項目中

  1. 配置約束位置

  1. 搭建CXF環境
    1. 提供cxf的配置文件(spring配置文件須要導入jaxws名稱空間)

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

 

 

</beans>

  1. Web.xml中配置Servlet

<!-- 配置cxf提供servlet 一、發佈webservice服務 二、處理客戶端訪問請求 -->

<servlet>

    <servlet-name>cxf</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

    <!—暫時使用初始化參數加載cxf配置文件(spring配置文件),後期使用spring監聽器加載spring配置文件 -->

    <init-param>

        <param-name>config-location</param-name>

        <param-value>classpath:applicationContext.xml</param-value>

    </init-param>

</servlet>

 

  1. 建立服務接口,實現類,在接口上使用註解@webService註解
  2. 在cxf配置文件中發佈服務

  1. 查看wsdl地址:

 

  1. 客戶端調用

經過命令生成本地調用java代碼:

 

  1. Jdk的方式(瞭解)

  1. wsdl地址:服務地址+?wsdl

  1. 將上面生成全部java文件拷貝到項目
  2. 測試代碼

  1. CXF方式(掌握

經過spring工廠產生代理對象;

 

  1. 建立新的工程
  2. 將cxf的相關jar包到入項目
  3. 提供cxf配置文件:spring配置文件
  4. 只須要拷貝生成的服務接口到項目中,在cxf配置文件中進行配置

  1. 測試代碼

  1. 總結

解決兩個項目直接互相調用;

 

  1. 基於cxf搭建CRM項目環境

 

CRM系統只是爲了提供webservice服務供其餘兩個項目:後臺管理系統,前臺系統調用;沒有提供對客戶數據模塊維護頁面;

CRM項目中使用技術:spring+spring-data-jpa +CXF +oracle數據庫

 

 

  1. 搭建數據庫環境

  1. 建立表空間
  2. 建立用戶,用戶受權

--建立表空間

create tablespace itcast271_crm

datafile 'c:\itcast271_crm.dbf'

size 200m

autoextend on

next 10m;

 

 

 

--建立用戶

create user xiaolongnv identified by xiaolongnv

default tablespace itcast271_crm;

 

--受權

grant resource,connect to xiaolongnv;

  1. 搭建項目環境

Spring+spring-data-jpa+CXF

  1. 建立maven –project 父工程爲:common_parent

  1. CRM框架環境

Spring+spring-data-jpa+CXF

 

  1. 搭建spring環境
    1. 配置文件:applicationContext.xml
    2. Web.xml配置監聽器
  2. Spring整合jpa
  3. Spring-data-jpa :2,3配置去複製bos_web項目下 注意:包掃描路徑改成crm

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xmlns:soap="http://cxf.apache.org/bindings/soap"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:jpa="http://www.springframework.org/schema/data/jpa"

    xmlns:task="http://www.springframework.org/schema/task"

    xsi:schemaLocation="

                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd

                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

                        http://www.springframework.org/schema/data/jpa

                        http://www.springframework.org/schema/data/jpa/spring-jpa.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">

                        

    <!-- 鏈接池 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />

        <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.133.10:1521:ORCL" />

        <property name="user" value="xiaolongnv" />

        <property name="password" value="xiaolongnv" />

    </bean>

    

    <!--

        jap:entityManger操做數據庫 相對於hibernate中Session對象

        JPA是統一規範,hibernate實現JPA標準 -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <!-- 掃描實體類包:包含子包 -->

        <property name="packagesToScan" value="cn.itcast.crm.domain" />

        <!-- 指定持久層提供者爲Hibernate -->

        <property name="persistenceProvider">

            <bean class="org.hibernate.ejb.HibernatePersistence" />

        </property>

        <property name="jpaVendorAdapter">

            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

                <!-- 自動建表 -->

                <property name="generateDdl" value="true" />

                <property name="showSql" value="true" />

                <property name="database" value="ORACLE" />

                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />

            </bean>

        </property>

        <property name="jpaDialect">

            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

        </property>

    </bean>

    

    <!-- 事務管理器 -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

        <property name="entityManagerFactory" ref="entityManagerFactory" />

    </bean>

    

    <!-- 註解方式管理事務 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>

    

    <!-- spring-Data整合jpa:解放持久層

        base-package:設置爲dao接口包,指定到父包,當項目啓動時候,對接口產生代理對象。經過代理對象調用CRUD方法

     -->

    <jpa:repositories base-package="cn.itcast.crm.dao"></jpa:repositories>

    

    <!-- service,action對象建立使用註解方式 -->

    <context:component-scan base-package="cn.itcast.crm"></context:component-scan>

</beans>

  1. 服務端CXF環境
    1. Web.xml配置servlet

<!-- 配置cxfServlet 處理客戶端請求 -->

 

<servlet>

    <servlet-name>cxf</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

<!--     <init-param>以前經過初始化參數加載spring配置文件,發佈服務;如今已經配置監聽器</init-param> -->

</servlet>

 

<servlet-mapping>

    <servlet-name>cxf</servlet-name>

    <url-pattern>/service/*</url-pattern>

</servlet-mapping>

五、Web.xml

<!-- spring的監聽器 -->

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

 

    <!-- cxfServlet:處理客戶端請求 二、發佈服務 -->

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

  1. 發佈查詢全部的客戶數據服務

添加新server:

 

  1. 將資料中客戶實體導入bos_crm項目中

二、啓動項目完成自動建表

  1. 執行資料中sql腳本,添加測試客戶數據

  1. 數據庫中數據

  1. 實現案例

  1. 建立服務接口,在接口上使用註解@WebService,建立實現類

  1. Cxf配置文件中,配置webservice服務端對象

  1. 啓動項目:查詢webservice服務

Wsdl地址:服務地址+?wsdl

  1. 完善服務端查詢全部客戶方法
    1. 建立dao繼承jparepository
    2. 將dao對象注入service中
      1. 調用服務戶端方法

  2. 獲取wsdl地址:http://localhost:8082/bos_crm/service/customer?wsdl 生成代碼

  1. 將接口拷貝到bos-service項目中

  1. 在客戶端spring配置文件中,配置客戶端調用對象

  1. 在須要調用CRM的代碼中注入客戶端對象

 

相關資料連接:https://pan.baidu.com/s/16IhLwfKGabTQp6SSUMSP_g 密碼:a6c1

idea環境下相關問題處理:

沒法使用@Autowired , could not autowire   :    http://www.cnblogs.com/Vgger/p/8596020.html

相關文章
相關標籤/搜索