WebService通過HTTP協議完成遠程調用: (深入分析) – RPC
WebService只採用HTTP POST方式傳輸數據,不使用GET方式; -- 握手,WSDL-get,
•普通http post的contentType爲
•application/x-www-form-urlencoded
•WebService的contentType爲-即在Http的基礎上發SOAP協議
•text/xml 這是基於soap1.1協議。
•application/soap+xml 這是基於soap1.2協議。
WebService從數據傳輸格式上作了限定。WebService所使用的數據均是基於XML格式的。目前標準的WebService在數據格式上主要採用SOAP協議。SOAP協議實際上就是一種基於XML編碼規範的文本協議。
SOAP – Simple Object Access protocol 簡單對像訪問協議。是運行在HTTP協議基礎之上的協議。其實就是在HTTP協議是傳輸XML文件,就變成了SOAP協議。
SOAP1.1和SOAP1.2的 namespace不一樣。可以通過查看類
•javax.xml.ws.soap.SOAPBinding來查看裏面的常量
•默認情況下,Jdk1.6只支持soap1.1
•即:@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING)
SOAP1.1和SOAP1.2的對比
WebService和Web服務器的區別
WebService和Web服務器有什麼區別呢?我們可以把WebService看作是Web服務器上應用;反過來說,Web服務器是WebService運行時所必需的容器。這就是它們的區別和聯繫。
使用JDK1.6發佈的簡單Web服務,其內部其實是使用Socket實現。可以查看:SUN公司未對外公佈的API類com.sun.xml.internal.ws.transport.http.server.
ServerMgr獲知,請使用反編譯工具。
WebService的特點
•WebService通過HTTP POST方式接受客戶的請求
•WebService與客戶端之間一般使用SOAP協議傳輸XML數據.
•它本身就是爲了跨平臺或跨語言而設計的。
WSDL
發佈
/**
*
*
* @author 姜沂
*
* WebService 將 Java 類標記爲實現 Web Service,或者將 Java 接口標記爲定義 Web Service 接口
*
*/
@WebService
public class HelloService {
public static void main(String[] args) {
/**
* 靜態的和final的方法 不能被髮布 參數1:服務的發佈地址 參數2:服務的實現者
*/
Endpoint.publish("http://127.0.0.1:9999/hello", new HelloService());
System.out.println("Server ready...");
}
public String sayHello(String name) {
System.err.println("----------------");
return "hello" + name;
}
@WebMethod(exclude=true)//不暴露
public String sayHello2(String name) {
return "hello " + name;
}
}
訪問
http://127.0.0.1:9999/hello?wsdl