このチュートリアルでは、JAX-WSを使用してSOAPベースのWebサービス(ドキュメントスタイル)エンドポイントを作成する方法を説明します。 RPCスタイル と比較すると、動作させるには多少の努力が必要です。
この例のディレクトリ構造
JAX-WS Webサービスのエンドポイント
JAX-WSでドキュメントスタイルのWebサービスを作成する手順は次のとおりです。
1. Webサービスエンドポイントインターフェイスを作成する
実際には、デフォルトのスタイルはdocumentであるため、 `@ SOAPBinding`の注釈はオプションです。
File:HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL)//optional public interface HelloWorld{ @WebMethod String getHelloWorldAsString(String name); }
2. Webサービスエンドポイント実装を作成する
File:HelloWorldImpl.java
package com.mkyong.ws; import javax.jws.WebService; //Service Implementation @WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString(String name) { return "Hello World JAX-WS " + name; } }
3.エンドポイントパブリッシャーを作成します.
File:HelloWorldPublisher.java
package com.mkyong.endpoint; import javax.xml.ws.Endpoint; import com.mkyong.ws.HelloWorldImpl; //Endpoint publisher public class HelloWorldPublisher{ public static void main(String[]args) { Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl()); } }
エンドポイントパブリッシャを実行すると、次のエラーメッセージが表示されます。
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found. Have you run APT to generate them?
このリンク://webservices/jax-ws/wrapper-class-package-jaxws-methodname-not-found-you-you-run-apt-to-generate-them/[記事]を参照してください。
必要なJAX-WSポータブルアーティファクトを生成するには、 " wsgen "ツールを使用する必要があります。次のステップに移りましょう。
4. wsgenコマンド
ドキュメントスタイルを実行するには余分なクラスが必要です。 " wsgen "を使用して、必要なすべてのJava成果物(マッピングクラス、wsdlまたはxsdスキーマ)を生成できます。 " wsgen "コマンドは、サービスエンドポイント実装クラスを読み取るために必要です。
wsgen -keep -cp . com.mkyong.ws.HelloWorldImpl
2つのクラスを生成し、それを " package.jaxws "フォルダにコピーします。
File:GetHelloWorldAsString.java
package com.mkyong.ws.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/") public class GetHelloWorldAsString { @XmlElement(name = "arg0", namespace = "") private String arg0; /** ** ** ** @return ** returns String ** / public String getArg0() { return this.arg0; } /** ** ** ** @param arg0 ** the value for the arg0 property ** / public void setArg0(String arg0) { this.arg0 = arg0; } }
File:GetHelloWorldAsStringResponse.java
package com.mkyong.ws.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/") public class GetHelloWorldAsStringResponse { @XmlElement(name = "return", namespace = "") private String __return; /** ** ** ** @return ** returns String ** / public String getReturn() { return this.__return; } /** ** ** ** @param __return ** the value for the __return property ** / public void setReturn(String __return) { this.__return = __return; } }
-
注意** "wsgen"ツールは "JDK__Path \ bin \"フォルダにあります。詳細については、//webservices/jax-ws/jax-ws-wsgen-tool-example/[JAX-WS:wsgenツール例]を参照してください。
Webサービスクライアント
公開されたサービスにアクセスするためのWebサービスクライアントを作成します。
File:HelloWorldClient.java
package com.mkyong.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.mkyong.ws.HelloWorld; public class HelloWorldClient{ public static void main(String[]args) throws Exception { URL url = new URL("http://localhost:9999/ws/hello?wsdl"); QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.getHelloWorldAsString("mkyong")); } }
出力
Hello World JAX-WS mkyong
SOAPトラフィックのトレース
このドキュメントスタイルのWebサービスで、クライアントとサーバーの間のSOAPエンベロープの流れを上から下に示します。
1. WSDLファイルをリクエストする
まず、クライアントがサービスエンドポイントにwsdl要求を送信します。
-
クライアント送信要求:**
GET/ws/hello?wsdl HTTP/1.1 User-Agent: Java/1.6.0__13 Host: localhost:9999 Accept: text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2 Connection: keep-alive
-
サーバーは応答を送信します:**
HTTP/1.1 200 OK Transfer-encoding: chunked Content-type: text/xml;charset=utf-8 <?xml version="1.0" encoding="UTF-8"?> <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.mkyong.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.mkyong.com/" name="HelloWorldImplService"> <types> <xsd:schema> <xsd:import namespace="http://ws.mkyong.com/" schemaLocation="http://localhost:9999/ws/hello?xsd=1"></xsd:import> </xsd:schema> </types> <message name="getHelloWorldAsString"> <part name="parameters" element="tns:getHelloWorldAsString"></part> </message> <message name="getHelloWorldAsStringResponse"> <part name="parameters" element="tns:getHelloWorldAsStringResponse"></part> </message> <portType name="HelloWorld"> <operation name="getHelloWorldAsString"> <input message="tns:getHelloWorldAsString"></input> <output message="tns:getHelloWorldAsStringResponse"></output> </operation> </portType> <binding name="HelloWorldImplPortBinding" type="tns:HelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"> </soap:binding> <operation name="getHelloWorldAsString"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal"></soap:body> </input> <output> <soap:body use="literal"></soap:body> </output> </operation> </binding> <service name="HelloWorldImplService"> <port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding"> <soap:address location="http://localhost:9999/ws/hello"></soap:address> </port> </service> </definitions>
2. getHelloWorldAsString(文字列名)
2番目の呼び出しでは、クライアントはメソッドを呼び出してSOAPエンベロープ内の要求を呼び出し、それをサービスエンドポイントに送信します。サービスエンドポイントでは、要求されたメソッドを呼び出して、結果をSOAPエンベロープに入れてクライアントに返します。
-
クライアント送信要求:**
POST/ws/hello HTTP/1.1 SOAPAction: "" Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2 Content-Type: text/xml; charset=utf-8 User-Agent: Java/1.6.0__13 Host: localhost:9999 Connection: keep-alive Content-Length: 224 <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/"> <arg0>mkyong</arg0> </ns2:getHelloWorldAsString> </S:Body> </S:Envelope>
-
サーバーは応答を送信します:**
HTTP/1.1 200 OK Transfer-encoding: chunked Content-type: text/xml; charset=utf-8 <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/"> <return>Hello World JAX-WS mkyong</return> </ns2:getHelloWorldAsStringResponse> </S:Body> </S:Envelope>
ソースコードをダウンロードする
ダウンロード - リンク://wp-content/uploads/2010/11/JAX-WS-HelloWorld-Document-Example.zip[JAX-WS-HelloWorld-Document-Example.zip](10KB)