WebService(jax-ws)三要素
SOAP: 基於HTTP協議,采用XML格式,用來傳遞信息的格式。
WSDL: 用來描述如何訪問具體的服務。(相當於說明書)
UDDI: 用戶自己可以按UDDI標准搭建UDDI服務器,用來管理,分發,查詢WebService 。其他用戶可以自己注冊發布WebService調用。(現在基本廢棄)
1.SOAP(通訊協議)
Simple Object Accrss Protocol,簡單對象訪問協議是在分散或分布式的環境中交換信息的簡單的協議,是一個基於XML的協議,它包括四個部分:
(1)SOAP封裝(envelop),封裝定義了一個描述消息中的內容是什么,是誰發送的,誰應當接受並處理它,以及如何處理它們的框架;
(2)SOAP編碼規則,用於表示應用程序需要使用的數據類型的實例。
(2)SOAP RPC 表示遠程過程調用和應答的協定。
(4)SOAP綁定(binding),使用底層協議交換信息。
(1)請求的協議體
POST /weather HTTP/1.1
Accept: text/xml, multipart/related
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://weather.itheima.com/WeatherInterface/getWeatherByCityNameRequest"
User-Agent: JAX-WS RI 2.2.4-b01
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 235
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getWeatherByCityName xmlns:ns2="http://weather.itheima.com">
<cityName>北京</cityName>
</ns2:getWeatherByCityName>
</S:Body>
</S:Envelope>
(2)響應的協議體
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
Date: Fri, 09 Oct 2015 07:23:29 GMT
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getWeatherByCityNameResponse xmlns:ns2="http://weather.itheima.com">
<WeatherInfo>今天風很大</WeatherInfo>
</ns2:getWeatherByCityNameResponse>
</S:Body>
</S:Envelope>
(3)發布基於soap1.2協議的服務
在SEI(service endpoint interface 服務端點接口)實現類上添加如下注解
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
發布基於soap1.2協議的服務,需要jax-ws包要求2.2.8以上版本。
(4)soap1.2請求的協議體
POST /weather HTTP/1.1
Accept: application/soap+xml, multipart/related
Content-Type: application/soap+xml; charset=utf-8;
action="http://weather.itheima.com/WeatherInterface/getWeatherByCityNameRequest"
User-Agent: JAX-WS RI 2.2.4-b01
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 233
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body>
<ns2:getWeatherByCityName xmlns:ns2="http://weather.itheima.com">
<cityName>北京</cityName>
</ns2:getWeatherByCityName>
</S:Body>
</S:Envelope>
(5)soap1.2響應的協議體
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: application/soap+xml; charset=utf-8
Date: Fri, 09 Oct 2015 07:54:53 GMT
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body>
<ns2:getWeatherByCityNameResponse xmlns:ns2="http://weather.itheima.com">
<WeatherInfo>今天風很大</WeatherInfo>
</ns2:getWeatherByCityNameResponse>
</S:Body>
</S:Envelope>
2.WSDL
WSDL(Web Service Description Language),是一個用來描述Web服務(Web Service)和說明如何與Web服務通信的XML語言。因為是基於XML的,
所以WSDL既是機器可閱讀的,又是人可閱讀的,這將是一個很大的好處。

(1)wsdl的閱讀方法
是從下往上讀。
每個wsdl有且只有一個Service節點。
1、先找Service節點
2、Service節點中找port節點。每個port對應一個PortType。
3、Port節點對應一binding節點。每個binding節點對應一個PortType
4、PortType中有operation 節點就是服務的方法。
5、operation 中有Input(參數)和output(返回值)
6、Input(參數)和output(返回值)對應message節點
7、Message對應element節點。Element節點對應complexType節點描述了參數及返回值的數據類型。
(2)使用注解規范wsdl
@WebService: 定義服務,在public class上邊。
targetNamespace:指定命名空間。
name:portType的名稱。
portName:port的名稱。
serviceName:服務名稱。
@WebResult:定義返回值。
name:返回結果值的名稱。
@WebParam:定義參數。
name:指定參數的名稱。
@WebMethod
exclude:排除指定的方法。
默認情況下,SEI實現類中所以的public方法會發布成服務方法。
如果不想把public方法發布成服務方法,需要使用@WebMethod注解。把此方法排除。在實現類中至少有一個方法發布為服務方法。

http://127.0.0.1:12345/weather?wsdl

http://127.0.0.1:12345/weather?xsd=1

作用: 通過注解,可以更加形像的描述Web服務。對自動生成的wsdl文檔進行修改,為使用者提供一個更加清晰的wsdl文檔。 當修改了WebService注解之后,會影響客戶端生成的代碼。調用的方法名和參數名也發生了變化。 3.UDDI UDDI 是一種目錄服務,通過它,企業可注冊並搜索 Web services。企業將自己提供的Web Service注冊在UDDI,也可以使用別的企業在UDDI注冊的web service服務,從而達到資源共享。 UDDI旨在將全球的webservcie資源進行共享,促進全球經濟合作。 但是使用webservice並不是必須使用UDDI,因為用戶通過WSDL知道了web service的地址,可以直接通過WSDL調用webservice。(現已廢棄)