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>
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服務,輸出: