使用CXF實現WebService

 

1、首先建立一個maven項目,引入相應的jar包html

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xw</groupId>
    <artifactId>xw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <cxf.version>2.2.3</cxf.version>
    </properties>

    <dependencies>
    <!-- CXF Dependencies -->
    <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>
    <!-- Jetty is needed if you're are not using the CXFServlet -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    </dependencies>

    <build>
        <finalName>spirngMVC</finalName>
        <plugins>
            <!-- 編碼和編譯和JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2、服務端web

  一、公開Web服務的接口IHelloServiceapache

@WebService
public interface IHelloService {

    Customer selectMaxIdCustomer(Customer c1,Customer c2);

    Customer selectLongNameCustomer(Customer c1,Customer c2);
}

  使用@WebService註解表示公開爲Web服務,使用這個註解的接口將公開全部的方法,若是你想屏蔽某個方法,可使用方法註解@WebMethod(exclude = true)設置爲true表示屏蔽該方法。app

  二、實現類HelloServiceImplfrontend

public class HelloServiceImpl implements IHelloService {
    @Override
    public Customer selectMaxIdCustomer(Customer c1, Customer c2) {
        if(c1.getId() >= c2.getId() ){
            return c1;
        }else{
            return c2;
        }
    }

    @Override
    public Customer selectLongNameCustomer(Customer c1, Customer c2) {
        if(c1.getName().length() >= c2.getName().length() ){
            return c1;
        }else{
            return c2;
        }
    }
}

  實現了Web服務的具體功能。maven

  三、Customer類ide

@XmlRootElement(name = "Customer")
public class Customer {
    private long id;
    private String name;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

  這個類是公開爲Web服務的接口中的參數類型和返回值,所以你須要使用@XmlRootElement(name = "Customer")註解告訴CXF如何在XML和Java Object之間處理。ui

  四、發佈Web服務this

public class ServerSimple {

    public static void main(String[] args) throws Exception{
        Endpoint.publish("http://localhost:8080/helloWorld", new HelloServiceImpl());
    }
}

  使用CXF自帶的Jetty運行服務端,設置URL和服務類就行編碼

 

3、查看WSDL

  訪問http://localhost:8080/helloWorld?wsdl地址能夠看到很長的XML,就是WSDL,若是看到WSDL就表明Web服務發佈成功了

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cxf/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloServiceImplService" targetNamespace="http://cxf/">
<wsdl:types>
<xs:schema xmlns:tns="http://cxf/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://cxf/" version="1.0">
<xs:element name="Customer" type="tns:customer"/>
<xs:element name="selectLongNameCustomer" type="tns:selectLongNameCustomer"/>
<xs:element name="selectLongNameCustomerResponse" type="tns:selectLongNameCustomerResponse"/>
<xs:element name="selectMaxIdCustomer" type="tns:selectMaxIdCustomer"/>
<xs:element name="selectMaxIdCustomerResponse" type="tns:selectMaxIdCustomerResponse"/>
<xs:complexType name="selectLongNameCustomer">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:customer"/>
<xs:element minOccurs="0" name="arg1" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectLongNameCustomerResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectMaxIdCustomer">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:customer"/>
<xs:element minOccurs="0" name="arg1" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="selectMaxIdCustomerResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:customer"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="selectLongNameCustomer">
<wsdl:part element="tns:selectLongNameCustomer" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectLongNameCustomerResponse">
<wsdl:part element="tns:selectLongNameCustomerResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectMaxIdCustomer">
<wsdl:part element="tns:selectMaxIdCustomer" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="selectMaxIdCustomerResponse">
<wsdl:part element="tns:selectMaxIdCustomerResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IHelloService">
<wsdl:operation name="selectLongNameCustomer">
<wsdl:input message="tns:selectLongNameCustomer" name="selectLongNameCustomer"></wsdl:input>
<wsdl:output message="tns:selectLongNameCustomerResponse" name="selectLongNameCustomerResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectMaxIdCustomer">
<wsdl:input message="tns:selectMaxIdCustomer" name="selectMaxIdCustomer"></wsdl:input>
<wsdl:output message="tns:selectMaxIdCustomerResponse" name="selectMaxIdCustomerResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServiceImplServiceSoapBinding" type="tns:IHelloService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="selectLongNameCustomer">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectLongNameCustomer">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectLongNameCustomerResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectMaxIdCustomer">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectMaxIdCustomer">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectMaxIdCustomerResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloServiceImplService">
<wsdl:port binding="tns:HelloServiceImplServiceSoapBinding" name="HelloServiceImplPort">
<soap:address location="http://localhost:8080/helloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

4、WSDL的構成

  一個 WSDL 文檔的主要結構是相似這樣的:

<definitions>

<types>
  data type definitions........
</types>

<message>
  definition of the data being communicated....
</message>

<portType>
  set of operations......
</portType>

<binding>
  protocol and data format specification....
</binding>

</definitions>
  • <wsdl:definitions>是WSDL的根元素,須要關注的元素有name="HelloServiceImplService",屬性值爲公開的Web服務的實現類+Service,targetNamespace="http://cxf/"指定目標名稱空間,還有xmlns:tns="http://cxf/"的屬性值就是targetNamespace的屬性值,默認使用實現類的包名的反綴,因此其實我該貼一張目錄就顯而易見了

  

  • <wsdl:types>經過<xs:element>定義Web Service使用的數據類型,<xs:element name="Customer" type="tns:customer"/>其中name是這個複雜類型的JAXB註解的name屬性值,type是tns:+JAXB註解的name屬性值全小寫形式,再往下會看到XXX元素和XXXResponse元素,其中XXX是方法名稱,是對方法參數的封裝,XXXResponse是對返回值的封裝。</xs:complexType>將對XXX和XXXResponse指定封裝的內容
  • </wsdl:message>將輸入參數和響應結果、受檢查的異常信息包裝爲消息
  • </wsdl:portType>描述了 web service、可被執行的操做,以及相關的消息。</wsdl:input></wsdl:output>指定輸入和輸出
  • </wsdl:binding>每一個端口定義消息格式和協議細節
  • 最後的</wsdl:service>name屬性指定服務的名稱,</wsdl:port>的name指定port名稱,<soap:address location="http://localhost:8080/helloWorld"/>指定Web服務的地址

5、客戶端

  具體代碼:

public final class Client {

    public static void main(String[] args) throws Exception{
        JaxWsProxyFactoryBean factory = new  JaxWsProxyFactoryBean ();
        factory.setAddress("http://localhost:8080/helloWorld");
        factory.setServiceClass(IHelloService.class);

        IHelloService o = (IHelloService) factory.create();

        Customer c1 = new Customer();
        c1.setId(1);
        c1.setName("A");

        Customer c2= new Customer();
        c1.setId(2);
        c1.setName("AC");

        System.out.println(o.selectMaxIdCustomer(c1,c2).getName());

    }
}

  使用JaxWsProxyFactoryBean客戶端代理工廠調用Web服務,輸出:

相關文章
相關標籤/搜索