Web Services, 即 "Web 服務", 簡稱 "WS", 其實就是"基於Web 服務" , 所謂的服務是雙方的,有服務的提供方,也有服務的需求方, 一般是服務提供方發佈服務,服務需求方調用服務java
WebServices 其實就是創建在HTTP協議上實現異構系統通信的工具, 數據都是經過HTTP進行傳輸的, 所謂的異構系統通信是指你在廣州用C#開發的應用能夠調用我在上海開發用Java開發的應用, 經過調用Java 系統對外發布的Web Services,獲取Java系統中的數據,在開始以前我簡單說下Web Services中的術語:spring
WSDL: (Web Services Description Language) Web Services 描述語言瀏覽器
SOAP:(Simple Object Access Protocol) 簡單對象訪問協議工具
若是想知道更多Web Services的概念和術語, 能夠參考: http://baike.baidu.com/view/67105.htm spa
第一步: 寫一個服務接口.net
@WebService public interface HelloServices { String sayHello(String name); }
在接口放一個@WebService註解, 說明該接口是一個Web Services接口code
第二步: 實現WebService 接口, 在實現類中完成具體業務邏輯xml
@WebService(serviceName = "HelloService",portName = "HelloServicePort",endpointInterface = "com.HelloServices") public class HelloServicesImpl implements HelloServices { public String sayHello(String name) { return "hello " + name; } }
第三步: 寫一個Server類, 用於發佈WebServices, 用JDK提供的工具便可實現htm
public class Server { public static void main(String[] args) { String address = "http://localhost:8080/ws/soap/hello"; HelloServices helloServices = new HelloServicesImpl(); Endpoint.publish(address, helloServices); System.out.println("Web Services is published"); } }
運行Server類中的main方法 會在控制檯看到"Web Services is published" 的提示,說明Web Services 成功發佈
對象
第四步:打開瀏覽器,在地址欄中輸入如下地址:
http://localhost:8080/ws/soap/hello?wsdl
注意:以上地址後面有一個 ?wsdl
後綴,在 Server
類中的 address 裏卻沒有這個後綴。此時,在瀏覽器中會看到以下 XML 文檔:
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://com/"name="HelloService"> <types> <xsd:schema> <xsd:import namespace="http://com/" schemaLocation="http://localhost:8080/ws/soap/hello?xsd=1"/> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello"/> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"/> </message> <portType name="HelloServices"> <operation name="sayHello"> <input wsam:Action="http://com/HelloServices/sayHelloRequest" message="tns:sayHello"/> <output wsam:Action="http://com/HelloServices/sayHelloResponse" message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloServicePortBinding" type="tns:HelloServices"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloService"> <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> <soap:address location="http://localhost:8080/ws/soap/hello"/> </port> </service> </definitions>
經過前面的介紹, 咱們知道能夠使用 JDK發佈Web Services, 那麼爲了讓Web Services 的開發與使用變得更加簡單, 更加輕量級,咱們隆重介紹一下CXF, CXF的前身是XFire, 不只用於開發基於SOAP(Simple Object Access Protocol)的Web Services,同時也適用於開發基於REST(Representational State Transfer) 的Web Services, 下一篇我將介紹一下CXF與spring的集成