Web Service 運行原理詳細介紹
利用清明小假期,溫習了一遍Web Service的相關內容,對其工作原理進行了簡要總結。以供有需求的朋友和自己日后參考。文章若有不當之處,敬請朋友們提出寶貴建議,以求共勉。
Web服務中,我們應該首先了解相關的術語含義:WSDL、UDDI....相關術語方面的介紹在此不再贅述,重點放在原理上。
在Web服務中,存在三個角色:服務提供者、服務請求者和服務中介,三者之間的關系如圖1-1所示
實現(xiàn)一個完整的Web服務包括以下步驟:
◆ Web服務提供者設計實現(xiàn)Web服務,并將調試正確后的Web服務通過Web服務中介者發(fā)布,并在UDDI注冊中心注冊; (發(fā)布)
◆ Web服務請求者向Web服務中介者請求特定的服務,中介者根據(jù)請求查詢UDDI注冊中心,為請求者尋找滿足請求的服務; (發(fā)現(xiàn))
◆ Web服務中介者向Web服務請求者返回滿足條件的Web服務描述信息,該描述信息用WSDL寫成,各種支持Web服務的機器都能閱讀;(發(fā)現(xiàn))
◆ 利用從Web服務中介者返回的描述信息(WSDL)生成相應的SOAP消息,發(fā)送給Web服務提供者,以實現(xiàn)Web服務的調用;(綁定)
◆ Web服務提供者按SOAP消息執(zhí)行相應的Web服務,并將服務結果返回給Web服務請求者。(綁定)
圖1-1 Web service的體系結構
注:WSDL的作用就是一個Web服務說明書。服務請求者根據(jù)此WSDL生成相應的SOAP消息,服務提供者在收到SOAP請求消息后,進行服務的綁定。
以下代碼是在web.xml中的servlet配置
<!-- 在向servlet或JSP頁面制定初始化參數(shù)或定制URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。 --> <servlet> <servlet-name>UserService</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <!-- 標記容器是否在啟動的時候就加載這個servlet(實例化并調用其init()方法;正數(shù)的值越小,該servlet的優(yōu)先級越高,應用啟動時就越先加載 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 服務器一般為servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。 但是,常常會更改這個URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。 --> <servlet-mapping> <servlet-name>UserService</servlet-name> <!-- 描述了相對于Web應用的根目錄的URL。url-pattern元素的值必須以斜杠(/)起始。 --> <url-pattern>/user</url-pattern> </servlet-mapping> 紅色代碼部分很重要,會在Web容器啟動的時候加載相應的servlet。綠色部分為該服務的外部接口。以此找到相應的jax-ws.xml文件(如下所示) <endpoint name="UserPort" implementation="cn.ujn.service.UserService" url-pattern="/user"> </endpoint>
進而綁定到相關的相應的實現(xiàn)類cn.ujn.service.UserService中??蛻舳税l(fā)送的SOAP請求消息消息體body中包含有客戶端所請求的方法名和參數(shù)信息。
以下為客戶端封裝的soap消息體(以Json方式與服務端進行數(shù)據(jù)傳輸)(SOAP Rerquest Envelope):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ujn.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - <soapenv:Body> - <q0:login> <arg0>{"username":"shq","password":"shq"}</arg0> </q0:login> </soapenv:Body> </soapenv:Envelope>
以下為SOAP1.1協(xié)議調用Web服務
/** * 通過SOAP1.1協(xié)議調用Web服務 * * text/xml 這是基于soap1.1協(xié)議 * * @param wsdl WSDL路徑 * @param method方法名 * @param namespace命名空間 * @param headerParameters 頭參數(shù) * @param bodyParameters 體參數(shù) * @param isBodyParametersNS 體參數(shù)是否有命名空間 * @return String * @throws Exception */ public static String invokeBySoap11(String wsdl, String method, String namespace, Map<String, String> headerParameters, Map<String, String> bodyParameters, boolean isBodyParametersNS) throws Exception { StringBuffer soapOfResult = null; // 去除 ?wsdl,獲取方法列表 int length = wsdl.length(); wsdl = wsdl.substring(0, length - 5); //以字符串為參數(shù)創(chuàng)建URL實例 URL url = new URL(wsdl); //創(chuàng)建連接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //設置請求方式 conn.setRequestMethod("POST"); //如果打算使用 URL連接進行輸入,則將 DoInput 標志設置為 true conn.setDoInput(true); //如果打算使用 URL連接進行輸出,則將 DoInput 標志設置為 true conn.setDoOutput(true); //主要是設置HttpURLConnection請求頭里面的屬性(K-V) conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); //獲取輸入流(相對于客戶端來說,使用的是OutputStream) OutputStream out = conn.getOutputStream(); // 獲取soap1.1版本消息 StringBuilder sb = new StringBuilder(); sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "); sb.append("xmlns:ns0=\"" + namespace + "\""); sb.append(">"); //拼裝消息頭 if (headerParameters != null) { sb.append("<soap:Header>"); for (Entry<String, String> headerParameter : headerParameters .entrySet()) { sb.append("<ns0:"); sb.append(headerParameter.getKey()); sb.append(">"); sb.append(headerParameter.getValue()); sb.append("</ns0:"); sb.append(headerParameter.getKey()); sb.append(">"); } sb.append("</soap:Header>"); } //拼裝消息體 sb.append("<soap:Body><ns0:"); sb.append(method); sb.append(">"); // 輸入?yún)?shù) if (bodyParameters != null) { for (Entry<String, String> inputParameter : bodyParameters .entrySet()) { if (isBodyParametersNS) { sb.append("<ns0:"); sb.append(inputParameter.getKey()); sb.append(">"); sb.append(inputParameter.getValue()); sb.append("</ns0:"); sb.append(inputParameter.getKey()); sb.append(">"); } else { sb.append("<"); sb.append(inputParameter.getKey()); sb.append(">"); sb.append(inputParameter.getValue()); sb.append("</"); sb.append(inputParameter.getKey()); sb.append(">"); } } } sb.append("</ns0:"); sb.append(method); sb.append("></soap:Body></soap:Envelope>"); //測試用 System.out.println(sb.toString()); //寫入SOAP消息(相對于客戶端來說,使用的是out.write()) out.write(sb.toString().getBytes()); //獲取服務器端的相應 int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; soapOfResult = new StringBuffer(); //從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)數(shù)組 b 中。以整數(shù)形式返回實際讀取的字節(jié)數(shù) //如果因為流位于文件末尾而沒有可用的字節(jié),則返回值 -1; while ((len = is.read(b)) != -1) { //Converts the byte array to a string using the named charset. String s = new String(b, 0, len, "UTF-8"); soapOfResult.append(s); } } conn.disconnect(); return soapOfResult == null ? null : soapOfResult.toString(); }
注:在客戶端發(fā)送SOAP請求消息后便處于阻塞狀態(tài)。直至服務端返回狀態(tài)碼。
以下為服務端進行響應(SOAP Response Envelope):
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> -<S:Body> -<ns2:loginResponse xmlns:ns2="http://ujn.cn/"> <return>1</return> </ns2:loginResponse> </S:Body> </S:Envelope>
客戶端接收到服務端發(fā)來的Json數(shù)據(jù)后會進行相應的解析操作。如下:
// 將Soap協(xié)議進行解析(DOM解析只能用于解析XML文檔類型,而SOAP消息就是采用XML數(shù)據(jù)格式) Document doc = XmlUtil.string2Doc(result); Element ele = (Element) doc.getElementsByTagName("return").item(0); 方法中使用到的string2Doc()方法體如下: public static Document string2Doc(String str) { //將XML文檔解析成DOM樹 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = null; DocumentBuilder build; if (str == null || str.equals("")) { return null; } try { InputStream bais = new ByteArrayInputStream(str.getBytes("UTF-8")); build = factory.newDocumentBuilder(); //Parse the content of the given InputStream as an XML document and return a new DOM Document object. document = build.parse(bais); } catch (Exception e) { e.printStackTrace(); } return document; }
根據(jù)返回結果,客戶端再進行相應的處理。
以上是web服務的基本工作原理。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Java中注解@JsonFormat與@DateTimeFormat的使用
從數(shù)據(jù)庫獲取時間傳到前端進行展示的時候,我們有時候可能無法得到一個滿意的時間格式的時間日期,本文主要介紹了Java中注解@JsonFormat與@DateTimeFormat的使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08SpringBoot參數(shù)校驗的最佳實戰(zhàn)教程
開發(fā)過程中,后臺的參數(shù)校驗是必不可少的,下面這篇文章主要給大家介紹了關于SpringBoot參數(shù)校驗的最佳實戰(zhàn),文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-08-08SpringBoot教程_創(chuàng)建第一個SpringBoot項目
這篇文章主要介紹了SpringBoot教程_創(chuàng)建第一個SpringBoot項目,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06SpringBoot自動裝配Condition的實現(xiàn)方式
這篇文章主要介紹了SpringBoot自動裝配Condition的實現(xiàn)方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08解決JPA?save()方法null值覆蓋掉mysql預設的默認值問題
這篇文章主要介紹了解決JPA?save()方法null值覆蓋掉mysql預設的默認值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11