Qingming Festival Holidayを使用して、Webサービスの関連コンテンツをレビューし、その作業原則を簡単に要約しました。困っている友人や将来の自分自身による参考のために。不適切な記事がある場合は、お互いを励ますために貴重な提案をするように友達に依頼してください。
Webサービスでは、まず関連用語の意味を理解する必要があります。WSDL、UDDI ....関連する用語の導入はここでは繰り返されませんが、焦点は原則に焦点を当てます。
Webサービスには、サービスプロバイダー、サービスリクエスター、サービス仲介者の3つの役割があります。 3つの関係を図1-1に示します
完全なWebサービスの実装には、次の手順が含まれます。
◆Webサービスプロバイダーは、Webサービスを設計および実装し、Webサービスの仲介を介してWebサービスの正しいデバッグを公開し、UDDI登録センターに登録します。 (公開)
◆Webサービスリクエスターは、Webサービス仲介者から特定のサービスを要求し、仲介者はリクエストを満たすサービスを見つけるリクエストに基づいてUDDI登録センターをクエリします。 (発見)
◆Webサービスの仲介者は、Webサービスリクエスターの条件を満たすWebサービスの説明情報を返します。説明情報はWSDLで記述されており、Webサービスをサポートするさまざまなマシンで読むことができます。 (発見)
◆Webサービス仲介者から返された説明情報(WSDL)を使用して、対応するSOAPメッセージを生成し、Webサービスプロバイダーに送信してWebサービスの呼び出しを実現します。 (バインディング)
◆Webサービスプロバイダーは、SOAPメッセージに従って対応するWebサービスを実行し、サービスの結果をWebサービスリクエスターに返します。 (バインディング)
図1-1 Webサービスアーキテクチャ
注:WSDLの機能はWebサービスマニュアルです。サービスリクエスターは、このWSDLに基づいて対応するSOAPメッセージを生成します。 SOAPリクエストメッセージを受信した後、サービスプロバイダーはサービスをバインドします。
次のコードは、web.xmlのサーブレット構成です
<! - サーブレットまたはJSPページに初期化パラメーターまたはカスタムURLを策定する場合、サーブレットまたはJSPページに最初に名前を付ける必要があります。サーブレット要素は、このタスクを完了するために使用されます。 --> <servlet> <servlet-name>UserService</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <!-- Mark whether the container loads this servlet when it is started (instand and call its init() method; the smaller the value of the positive number, the higher the priority of the servlet, the more it loads the最初にアプリケーション - > <load-on-startup> 1 </load-on-startup> </servlet> <! - http:// host/webappprefix/servlet/servletnameを提供します<サーブレットマッピング> servlet-name> userservice </servlet-name> <! - urlパターン要素の値は、スラッシュで開始する必要があります。緑のセクションは、サービスの外部インターフェイスです。対応するjax-ws.xmlファイル(以下を参照)<endpoint name = "userport"実装= "cn.ujn.service.userservice" url-pattern = "/user"> </endpoint>
次に、関連する対応する実装クラスcn.ujn.service.userserviceにバインドされます。クライアントが送信したSOAP要求メッセージボディには、クライアントが要求したメソッド名とパラメーター情報が含まれています。
以下は、クライアントによってカプセル化された石鹸メッセージ本文(JSONモードのサーバーを使用したデータ送信)(SOAP Rerquest Envelope):
<soapenv:envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0 = "http://ujn.cn/" xmlns:xsd = "http://www.w.w.w.w.w.w.w.w.w. xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance"> - <soapenv:body> - <q0:login> <arg0> {"username": "shq"、 "passwords": "shq"} </arg0> </q0:login> soapenv: </soapenv:envelope>以下は、Webサービスを呼び出すSOAP1.1プロトコルです
/*** SOAP1.1プロトコルを介してWebサービスの呼び出しボディパラメーターには名前空間があります * @return String * @throws Exception */ public static string invokebysoap11(string wsdl、string method、string namespace、map <string、string> headerparameters、map <string、string> bodyparameters、bolean isparametersns) //削除?wsdl、メソッドリストint length = wsdl.length(); wsdl = wsdl.substring(0、length -5); //パラメーターとして文字列を使用してURLインスタンスを作成しますurl = new URL(wsdl); //接続の作成//要求方法を設定しますconn.setRequestMethod( "post"); //入力にURL接続を使用する予定がある場合は、doinputフラグをtrue conn.setDoinput(true)に設定します。 //出力にURL接続を使用する予定がある場合は、doinputフラグをtrue conn.setdooutput(true)に設定します。 //主にhttpurlconnection request header conn.setRequestProperty( "content-type"、 "text/xml; charset = utf-8")に属性(kv)を設定します。 //入力ストリームを取得します(クライアントと比較して、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>"); } //メッセージbody sb.append( "<soap:body> <ns0:"); sb.append(メソッド); sb.append( ">"); //入力パラメーター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(メソッド); sb.append( "> </soap:body> </soap:envelope>"); // test system.out.println(sb.toString()); // SOAPメッセージを書き込み(クライアントに関連して、out.write()が使用されます)out.write(sb.tostring()。getBytes()); //サーバー側で対応するintコードを取得= conn.getResponseCode(); if(code == 200){inputstream is = conn.getInputStream(); byte [] b = new byte [1024]; int len = 0; soapofresult = new StringBuffer(); //入力ストリームから一定数のバイトを読み取り、バッファアレイbに保存します。ストリームがファイルの最後にあるために使用可能なバイトがない場合、整数として読み取られる実際のバイト数を返します//値は-1です。 while((len = is.read(b))!= -1){//バイト配列を名前付きのcharsetを使用して文字列に変換します。 string s = new String(b、0、len、 "utf-8"); soapofresult.append(s); }} conn.disconnect(); soapofresult == nullを返しますか? null:soapofresult.toString(); }注:クライアントは、SOAPリクエストメッセージを送信した後にブロックされます。サーバーがステータスコードを返すまで。
以下は、サーバーの応答(SOAP応答エンベロープ)です。
<s:envelope xmlns:s = "http://schemas.xmlsoap.org/soap/envelope/"> - <s:body> - <ns2:loginResponse xmlns:ns2 = "http://ujn.cn/" </s:body> </s:envelope>
サーバーから送信されたJSONデータを受信した後、クライアントは対応する解析操作を実行します。次のように:
// SOAPプロトコルを解析します(DOM ParsingはXMLドキュメントタイプを解析するためにのみ使用できますが、SOAPメッセージはXMLデータ形式です)ドキュメントdoc = xmlutil.string2doc(result);要素ELE =(要素)doc.getElementsByTagname( "return")。アイテム(0);メソッドで使用されるstring2doc()メソッドは次のとおりです。publicstatic document string2doc(string str){// xml documentをDOM Treeに解析します。ドキュメントdocument = null; documentBuilderビルド。 if(str == null || str.equals( "")){return null; } try {inputstream bais = new bytearrayinputStream(str.getBytes( "utf-8")); build = factory.newdocumentbuilder(); //指定された入力ストリームのコンテンツをXMLドキュメントとして解析し、新しいDOMドキュメントオブジェクトを返します。 document = build.parse(bais); } catch(Exception e){e.printstacktrace(); }ドキュメントを返します。 }返品結果によると、クライアントは対応する処理を実行します。
上記は、Webサービスの基本的な機能原則です。
読んでくれてありがとう、私はそれがあなたを助けることができることを願っています。このサイトへのご支援ありがとうございます!