この章では、Java API for XML-based Web services (JAX-WS)を使用したWebLogic Webサービスの一般的な開発例を示します。
この章の内容は次のとおりです:
各例では、簡単なWebLogic Webサービスを作成する手順や、デプロイ済のWebサービスから操作を呼び出す手順を示します。これらの例には基本的なJavaコードとAnt build.xml
ファイルが含まれており、それらをユーザーの開発環境で使用してサンプルを再現することや、現在の開発環境とは別の環境で指示に従ってサンプルを作成して実行することができます。
例ではサンプルで使用されるプロセスやツールについては詳しく説明していません。詳細は、以降の章を参照してください。
注意: 高度なWebサービス機能を示すベスト・プラクティスの例については、第7章「JAX-WS Webサービス・クライアントを開発するためのロードマップ」および第13章「信頼性のあるWebサービスとクライアントを開発するためのロードマップ」を参照してください。 |
この項では、操作が1つの非常に簡単なWebサービスの作成方法について説明します。Webサービスを実装するJava Webサービス(JWS)・ファイルでは必須のJWSアノテーション@WebService
を1つのみ使用しています。JWSファイルは、JWSメタデータ・アノテーションを使用してWebサービスの形式を指定した標準のJavaファイルです。メタデータ・アノテーションはJDK 5.0で導入され、Webサービス・ファイルにアノテーションを付けるために使用される一連のアノテーションはJWSアノテーションと呼ばれます。WebLogic Webサービスは標準のJWSアノテーションを使用します。サポートされるJWSアノテーションの完全なリストは、『Oracle WebLogic Server WebLogic Webサービス・リファレンス』のWebサービスのアノテーション・サポートに関する項を参照してください。
次の例では、1つの操作sayHelloWorld
を含むHelloWorldService
というWebサービスを作成する方法を示します。簡単にするために、操作は入力された文字列値を戻すものとします。
WebLogic Server環境を設定します。
コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbin
サブディレクトリにあるsetDomainEnv.cmd
(Windows)またはsetDomainEnv.sh
(UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、ORACLE_HOME
/user_projects/domains/
domainName
です。ここで、ORACLE_HOME
は、Oracle WebLogic Serverのインストール時にOracleホームとして指定したディレクトリ、domainName
は、ドメインの名前です。
次のようにプロジェクト・ディレクトリを作成します。
prompt> mkdir /myExamples/hello_world
プロジェクト・ディレクトリの下に、src
ディレクトリと、JWSファイル(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。
prompt> cd /myExamples/hello_world prompt> mkdir src/examples/webservices/hello_world
Webサービスを実装するJWSファイルを作成します。
任意のJava IDEまたはテキスト・エディタを開いて、「サンプルHelloWorldImpl.java JWSファイル」
で指定されたJavaコードを使用してHelloWorldImpl.javaというJavaファイルを作成します。
サンプルJWSファイルでは、1つのパブリック・メソッドsayHelloWorld(String)
を含むHelloWorldImpl
というJavaクラスが示されています。@WebService
アノテーションは、JavaクラスがHelloWorldService
というWebサービスを実装することを指定しています。デフォルトでは、すべてのパブリック・メソッドが操作として公開されます。
src/examples/webservices/hello_world
ディレクトリにHelloWorldImpl.java
ファイルを保存します。
プロジェクト・ディレクトリ(myExamples/hello_world/src
)内に標準のAnt build.xml
ファイルを作成し、taskdef
Antタスクを追加して、jwsc
タスクの完全修飾Javaクラス名を指定します。
<project name="webservices-hello_world" default="all"> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> </project>
この手順で説明される追加のターゲット(clean、undeploy
、client
、およびrun
など)が含まれている、完全なサンプルbuild.xml
ファイルについては、「HelloWorldImpl.javaのサンプルAntビルド・ファイル」
を参照してください。完全なbuild.xml
ファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}
などのプロパティも使用しています。
次のようなjwsc
Antタスクの呼出しを、build-service
ターゲット内にラップして、build.xml
ファイルに追加します。
<target name="build-service"> <jwsc srcdir="src" destdir="output/helloWorldEar"> <jws file="examples/webservices/hello_world/HelloWorldImpl.java" type="JAXWS"/> </jwsc> </target>
jwsc
WebLogic WebサービスAntタスクは、補助的なアーティファクトを生成し、ユーザーが作成および生成したJavaコードをコンパイルして、すべてのアーティファクトを、後でWebLogic Serverにデプロイされるエンタープライズ・アプリケーションEARファイルにアーカイブ化します。type="JAXWS"
を使用して作成するWebサービス(JAX-WS)のタイプを指定します。
コマンド・ラインでbuild-service
ターゲットを指定して、jwsc
Antタスクを実行します。
prompt> ant build-service
output/helloWorldEar
ディレクトリを見て、jwsc
Antタスクによって生成されたファイルとアーティファクトを確認します。
WebサービスをデプロイするWebLogic Serverインスタンスを起動します。
WebLogic Server管理コンソールまたはwldeploy
Antタスクを使用して、エンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。いずれの場合でも、output
ディレクトリにあるhelloWorldEar
エンタープライズ・アプリケーションをデプロイします。
wldeploy
Antタスクを使用するには、以下のターゲットをbuild.xml
ファイルに追加します。
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="deploy"> <wldeploy action="deploy" name="helloWorldEar" source="output/helloWorldEar" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target>
wls.username
、wls.password
、wls.hostname
、wls.port
、およびwls.server.name
の値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。
deploy
ターゲットを実行して、WARファイルをデプロイします。
prompt> ant deploy
ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。
http://host:port/HelloWorldImpl/HelloWorldService?WSDL
URLは、contextPath
およびserviceUri
属性のデフォルト値を使用して構築します。contextPath
のデフォルト値はJWSファイル内のJavaクラスの名前です。serviceURI
属性のデフォルト値は@WebService
アノテーションのserviceName
要素です(指定されている場合)。そうでない場合、拡張子を除いたJWSファイルの名前の後にService
を付けたものになります。たとえば、@WebService
アノテーションのserviceName
要素が指定されておらず、JWSファイルの名前がHelloWorldImpl.java
である場合、そのserviceUri
のデフォルト値はHelloWorldImplService
になります。
これらの属性は、次の「ユーザー定義のデータ型を使用したWebサービスの作成」の例で明示的に設定されます。各自のWebLogic Serverインスタンスに適合したホスト名およびポートを選択してください。
build.xml
ファイルでclean
、build-service
、undeploy
、deploy
の各ターゲットを使用すると、開発プロセスの一環としてWebサービスの更新、再ビルド、アンデプロイおよび再デプロイを繰り返し実行できます。
Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「WebLogic WebサービスからのWebサービスの呼出し」を参照してください。
package examples.webservices.hello_world;
// Import the @WebService annotation
import javax.jws.WebService;
@WebService(name="HelloWorldPortType", serviceName="HelloWorldService")
/**
* This JWS file forms the basis of simple Java-class implemented WebLogic
* Web Service with a single operation: sayHelloWorld
*/
public class HelloWorldImpl {
// By default, all public methods are exposed as Web Services operation
public String sayHelloWorld(String message) {
try {
System.out.println("sayHelloWorld:" + message);
} catch (Exception ex) { ex.printStackTrace(); }
return "Here is the message: '" + message + "'";
}
}
以下のbuild.xml
ファイルでは、プロパティを使用してファイルを簡素化しています。
<project name="webservices-hello_world" default="all"> <!-- set global properties for this build --> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="myserver" /> <property name="ear.deployed.name" value="helloWorldEar" /> <property name="example-output" value="output" /> <property name="ear-dir" value="${example-output}/helloWorldEar" /> <property name="clientclass-dir" value="${example-output}/clientclasses" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="all" depends="clean,build-service,deploy,client" /> <target name="clean" depends="undeploy"> <delete dir="${example-output}"/> </target> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}"> <jws file="examples/webservices/hello_world/HelloWorldImpl.java" type="JAXWS"/> </jwsc> </target> <target name="deploy"> <wldeploy action="deploy" name="${ear.deployed.name}" source="${ear-dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="undeploy"> <wldeploy action="undeploy" name="${ear.deployed.name}" failonerror="false" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.hello_world.client" type="JAXWS"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/hello_world/client/**/*.java"/> </target> <target name="run"> <java classname="examples.webservices.hello_world.client.Main" fork="true" failonerror="true" > <classpath refid="client.class.path"/> <arg line="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService" /> </java> </target> </project>
前の例では、Webサービス操作のパラメータと戻り値として、単純データ型String
のみを使用していました。次の例では、ユーザー定義のデータ型(特にBasicStruct
というJavaBean)を操作のパラメータおよび戻り値として使用するWebサービスの作成方法を示します。
Webサービスでユーザー定義のデータ型を使用する場合、データ型のJavaソースを作成して、それをJWSファイル内で適切に使用する以外に、プログラマが実際にすべきことはほとんどありません。jwsc
Antタスクは、JWSファイルにユーザー定義のデータ型がある場合、XML表現(SOAPメッセージで使用)とJava表現(WebLogic Serverで使用)との間でデータを変換するのに必要な、すべてのデータ・バインディング・アーティファクトを自動的に生成します。データ・バインディング・アーティファクトには、Javaユーザー定義データ型に相当するXMLスキーマがあります。
次の手順は「簡単なHelloWorld Webサービスの作成」に記載されている手順によく似ています。そのため、手順では必要なステップをすべて示していますが、簡単なHelloWorldサンプルとは異なるステップのみの詳細を説明しています。
WebLogic Server環境を設定します。
コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbin
サブディレクトリにあるsetDomainEnv.cmd
(Windows)またはsetDomainEnv.sh
(UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、ORACLE_HOME
/user_projects/domains/
domainName
です。ここで、ORACLE_HOME
は、Oracle WebLogic Serverのインストール時にOracleホームとして指定したディレクトリ、domainName
は、ドメインの名前です。
プロジェクト・ディレクトリを作成します。
prompt> mkdir /myExamples/complex
プロジェクト・ディレクトリの下に、src
ディレクトリと、JWSファイル(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。
prompt> cd /myExamples/complex prompt> mkdir src/examples/webservices/complex
BasicStruct
JavaBeanのソースを作成します。
任意のJava IDEまたはテキスト・エディタを開いて、「サンプルBasicStruct JavaBean」
で指定されたJavaコードを使用して BasicStruct.javaというJavaファイルをプロジェクト・ディレクトリに作成します。
BasicStruct.java
ファイルをプロジェクト・ディレクトリのsrc/examples/webservices/complex
サブディレクトリに保存します。
「サンプルComplexImpl.java JWSファイル」で指定されたJavaコードを使用して、Webサービスを実装するJWSファイルを作成します。
このサンプルJWSファイルでは、複数のJWSアノテーションを使用しています。@WebMethod
では、メソッドがWebサービス操作として公開されることを明示的に指定し、操作名をデフォルトのメソッド名echoStruct
からechoComplexType
に変更します。@WebParam
と@WebResult
では、パラメータと戻り値を構成します。@SOAPBinding
では、Webサービスのタイプを指定します。また、ComplexImpl.java
JWSファイルではexamples.webservice.complex.BasicStruct
クラスをインポートして、echoStruct()
メソッドのパラメータと戻り値としてユーザー定義データ型のBasicStruct
を使用します。
JWSファイルの作成に関するより詳細な情報については、第4章「JWSファイルのプログラミング」を参照してください。
ComplexImpl.java
ファイルをプロジェクト・ディレクトリのsrc/examples/webservices/complex
サブディレクトリに保存します。
プロジェクト・ディレクトリ内に標準のAnt build.xml
ファイルを作成し、taskdef
Antタスクを追加して、jwsc
タスクの完全修飾クラス名を指定します。
<project name="webservices-complex" default="all"> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> </project>
完全なサンプルbuild.xmlファイルについては、「ComplexImpl.java JWSファイルのサンプルAntビルド・ファイル」
を参照してください。
次のようなjwsc
Antタスクの呼出しを、build-service
ターゲット内にラップして、build.xml
ファイルに追加します。
<target name="build-service"> <jwsc srcdir="src" destdir="output/ComplexServiceEar" > <jws file="examples/webservices/complex/ComplexImpl.java" type="JAXWS"> <WLHttpTransport contextPath="complex" serviceUri="ComplexService" portName="ComplexServicePort"/> </jws> </jwsc> </target>
上記の例で注目すべき点は以下のとおりです。
<jws>
要素のtype
属性では、Webサービスのタイプ(JAX-WSまたはJAX-RPC)が指定されています。
jwsc
Antタスクの<jws>
要素の<WLHttpTransport>
子要素では、HTTP/Sトランスポートを介してWebサービスを呼び出す際に使用されるURLのコンテキスト・パスとサービスURIセクションに加え、生成されるWSDLのポートの名前も指定されています。コンテキスト・パスの定義の詳細は、「WebLogic Webサービスのコンテキスト・パスの定義」を参照してください。
jwsc
Antタスクを使用します。
prompt> ant build-service
output/ComplexServiceEar
ディレクトリを見て、jwsc
Antタスクによって生成されたファイルとアーティファクトを確認します。
WebサービスをデプロイするWebLogic Serverインスタンスを起動します。
WebLogic Server管理コンソールまたはwldeploy
Antタスクを使用して、ComplexServiceEar
エンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。例:
prompt> ant deploy
WebLogic Server管理コンソールまたはwldeploy
Antタスクを使用して、エンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。いずれの場合でも、output
ディレクトリにあるComplexServiceEar
エンタープライズ・アプリケーションをデプロイします。
wldeploy
Antタスクを使用するには、以下のターゲットをbuild.xml
ファイルに追加します。
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="deploy"> <wldeploy action="deploy" name="ComplexServiceEar" source="output/ComplexServiceEar" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target>
wls.username
、wls.password
、wls.hostname
、wls.port
、およびwls.server.name
の値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。
deploy
ターゲットを実行して、WARファイルをデプロイします。
prompt> ant deploy
ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。
http://host:port/complex/ComplexService?WSDL
Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「WebLogic WebサービスからのWebサービスの呼出し」を参照してください。
package examples.webservices.complex; /** * Defines a simple JavaBean called BasicStruct that has integer, String, * and String[] properties */ public class BasicStruct { // Properties private int intValue; private String stringValue; private String[] stringArray; // Getter and setter methods public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } public String getStringValue() { return stringValue; } public void setStringValue(String stringValue) { this.stringValue = stringValue; } public String[] getStringArray() { return stringArray; } public void setStringArray(String[] stringArray) { this.stringArray = stringArray; } public String toString() { return "IntValue="+intValue+", StringValue="+stringValue; } }
package examples.webservices.complex; // Import the standard JWS annotation interfaces import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; // Import the BasicStruct JavaBean import examples.webservices.complex.BasicStruct; // Standard JWS annotation that specifies that the portType name of the Web // Service is "ComplexPortType", its public service name is "ComplexService", // and the targetNamespace used in the generated WSDL is "http://example.org" @WebService(serviceName="ComplexService", name="ComplexPortType", targetNamespace="http://example.org") // Standard JWS annotation that specifies this is a document-literal-wrapped // Web Service @SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED) /** * This JWS file forms the basis of a WebLogic Web Service. The Web Services * has two public operations: * * - echoInt(int) * - echoComplexType(BasicStruct) * * The Web Service is defined as a "document-literal" service, which means * that the SOAP messages have a single part referencing an XML Schema element * that defines the entire body. */ public class ComplexImpl { // Standard JWS annotation that specifies that the method should be exposed // as a public operation. Because the annotation does not include the // member-value "operationName", the public name of the operation is the // same as the method name: echoInt. // // The WebResult annotation specifies that the name of the result of the // operation in the generated WSDL is "IntegerOutput", rather than the // default name "return". The WebParam annotation specifies that the input // parameter name in the WSDL file is "IntegerInput" rather than the Java // name of the parameter, "input". @WebMethod() @WebResult(name="IntegerOutput", targetNamespace="http://example.org/complex") public int echoInt( @WebParam(name="IntegerInput", targetNamespace="http://example.org/complex") int input) { System.out.println("echoInt '" + input + "' to you too!"); return input; } // Standard JWS annotation to expose method "echoStruct" as a public operation // called "echoComplexType" // The WebResult annotation specifies that the name of the result of the // operation in the generated WSDL is "EchoStructReturnMessage", // rather than the default name "return". @WebMethod(operationName="echoComplexType") @WebResult(name="EchoStructReturnMessage", targetNamespace="http://example.org/complex") public BasicStruct echoStruct(BasicStruct struct) { System.out.println("echoComplexType called"); return struct; } }
以下のbuild.xml
ファイルでは、プロパティを使用してファイルを簡素化しています。
<project name="webservices-complex" default="all"> <!-- set global properties for this build --> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="myserver" /> <property name="ear.deployed.name" value="complexServiceEAR" /> <property name="example-output" value="output" /> <property name="ear-dir" value="${example-output}/complexServiceEar" /> <property name="clientclass-dir" value="${example-output}/clientclass" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="all" depends="clean,build-service,deploy,client"/> <target name="clean" depends="undeploy"> <delete dir="${example-output}"/> </target> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}" keepGenerated="true" > <jws file="examples/webservices/complex/ComplexImpl.java" type="JAXWS"> <WLHttpTransport contextPath="complex" serviceUri="ComplexService" portName="ComplexServicePort"/> </jws> </jwsc> </target> <target name="deploy"> <wldeploy action="deploy" name="${ear.deployed.name}" source="${ear-dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}"/> </target> <target name="undeploy"> <wldeploy action="undeploy" failonerror="false" name="${ear.deployed.name}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}"/> </target> <target name="client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.complex.client" type="JAXWS"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/complex/client/**/*.java"/> </target> <target name="run" > <java fork="true" classname="examples.webservices.complex.client.Main" failonerror="true" > <classpath refid="client.class.path"/> <arg line="http://${wls.hostname}:${wls.port}/complex/ComplexService" /> </java> </target> </project>
Webサービスの作成のもう1つの一般的な例は、既存のWSDLファイル(黄金のWSDLとも呼ばれます)から開始する方法です。WSDLファイルはWebサービスがどのようなものかを指定するパブリック・コントラクトであり、サポートされる操作のリスト、各操作のシグネチャと形式、操作を呼び出すときに使用されるプロトコルとトランスポート、データをトランスポートするときに使用されるXMLスキーマ・データ型などを指定します。WSDLファイルに基づいて、WebLogic Serverにデプロイできるように、Webサービスを実装するアーティファクトを生成します。wsdlc
Antタスクを使用して次のアーティファクトを生成します。
WSDLファイルで記述されたWebサービスを実装するJWSサービス・エンドポイント・インタフェース(SEI)。
生成されるJWS SEIの不完全な(途中まで作成済みの)実装が含まれるJWS実装ファイル。このファイルは開発者がカスタマイズする必要があります。
JAXBデータ・バインディング・アーティファクト。
(オプション)生成されるJWS SEIのJavadoc。
注意: 更新することになるwsdlc Antタスクによって生成されるファイルは、JWS実装ファイルのみです。JWS SEIおよびデータ・バインディング・アーティファクトが含まれるJARファイルを更新する必要はありません。 |
一般に、wsdlc
Antタスクを1回実行して、生成されるJWS SEIファイルとデータ・バインディング・アーティファクトを含むJARファイルを生成し、その後で、インタフェースを実装する生成済JWSファイルをコーディングして、Webサービスのビジネス・ロジックを追加します。特に、Webサービス操作を実装するメソッドに、操作の動作に必要なJavaコードを追加し、JWSアノテーションを追加します。
JWS実装ファイルをコーディングしたら、前の項で説明した同じ手順に従って、jwsc
Antタスクを実行してデプロイ可能なWebサービスを生成します。相違点は、compiledWsdl
属性を使用して、wsdlc
Antタスクによって生成されるJARファイル(JWS SEIファイルとデータ・バインディング・アーティファクトを含む)を指定することのみです。
次の簡単な例は、「サンプルWSDLファイル」に示したWSDLファイルからWebサービスを作成する方法を示しています。このWebサービスには、郵便番号を渡されると気温を返すgetTemp
という1つの操作が含まれています。
WebLogic Server環境を設定します。
コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbin
サブディレクトリにあるsetDomainEnv.cmd
(Windows)またはsetDomainEnv.sh
(UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、ORACLE_HOME
/user_projects/domains/
domainName
です。ここで、ORACLE_HOME
は、Oracle WebLogic Serverのインストール時にOracleホームとして指定したディレクトリ、domainName
は、ドメインの名前です。
作業ディレクトリを作成します。
prompt> mkdir /myExamples/wsdlc
WSDLファイルをコンピュータ上のアクセス可能なディレクトリに置きます。
この例では、WSDLファイルはTemperatureService.wsdl
という名前で/myExamples/wsdlc/wsdl_files
ディレクトリにあると仮定しています。ファイルの完全なリストについては、「サンプルWSDLファイル」を参照してください。
プロジェクト・ディレクトリ内に標準のAnt build.xml
ファイルを作成し、taskdef
Antタスクを追加して、wsdlc
タスクの完全修飾Javaクラス名を指定します。
<project name="webservices-wsdlc" default="all"> <taskdef name="wsdlc" classname="weblogic.wsee.tools.anttasks.WsdlcTask"/> </project>
この手順で説明される追加のターゲット(clean、undeploy
、client
、およびrun
など)が含まれている、完全なサンプルbuild.xml
ファイルについては、「TemperatureServiceのサンプルAntビルド・ファイル」
を参照してください。完全なbuild.xml
ファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}
などのプロパティも使用しています。
次のようなwsdlc
Antタスクの呼出しを、generate-from-wsdl
ターゲット内にラップして、build.xml
ファイルに追加します。
<target name="generate-from-wsdl"> <wsdlc srcWsdl="wsdl_files/TemperatureService.wsdl" destJwsDir="output/compiledWsdl" destImplDir="output/impl" packageName="examples.webservices.wsdlc" type="JAXWS"/> </target>
このサンプルwsdlc
タスクでは、JWS SEIとデータ・バインディング・アーティファクトを含むJARファイルを、カレント・ディレクトリ下のoutput/compiledWsdl
ディレクトリに生成します。また、JWS SEIの部分的な実装ファイル(TemperatureService_TemperaturePortImpl.java
)を、output/impl/examples/webservices/wsdlc
ディレクトリに生成します(このディレクトリは、destImplDir
で指定された出力ディレクトリと、パッケージ名で指定されたディレクトリ階層を組み合せたものです)。生成されたすべてのJWSファイルはexamples.webservices.wsdlc
パッケージにパッケージ化されます。
コマンド・ラインでgenerate-from-wsdl
ターゲットを指定して、wsdlc
Antタスクを実行します。
prompt> ant generate-from-wsdl
wsdlc
Antタスクによってアーティファクトとファイルが生成されたことを調べるには、output
ディレクトリを確認します。
任意のJava IDEまたはテキスト・エディタを使用して、生成されたoutput/impl/examples/webservices/wsdlc/TemperatureService_TemperaturePortImpl.java
JWS実装ファイルを編集し、目的の処理が行えるようにメソッドにJavaコードを追加します。
例は、「TemperatureService_TemperaturePortImpl Java実装ファイルのサンプル」を参照してください。追加されたJavaコードは太字になっています。生成されたJWS実装ファイルには@WebService
JWSアノテーション値が自動的に含まれており、その値は元のWSDLファイルの値に対応しています。
注意: 「WSDLから開始する」ユース・ケースのJWS実装ファイルに追加できるJWSアノテーションには制限があります。詳細は、『Oracle WebLogic Server WebLogic Webサービス・リファレンス』のwsdlcに関する項を参照してください。 |
処理を簡単にするため、サンプルのTemperatureService_TemperaturePortImpl.java
のgetTemp()
メソッドは、固定の数値を返します。実際のファイルの場合、このメソッドの実装では、特定の郵便番号で現在の気温をルックアップします。
更新したTemperatureService_TemperaturePortImpl.java
ファイルを永続的なディレクトリ(プロジェクト・ディレクトリの下のsrc
ディレクトリなど)にコピーします。パッケージ名に対応する子ディレクトリも必ず作成するようにしてください。
prompt> cd /examples/wsdlc prompt> mkdir src/examples/webservices/wsdlc prompt> cp output/impl/examples/webservices/wsdlc/TemperatureService_TemperaturePortImpl.java.java \src/examples/webservices/wsdlc/TemperatureService_TemperaturePortImpl.java.java
更新されたJWS実装クラスに対してjwsc
Antタスクを実行するbuild-service
ターゲットを、build.xml
ファイルに追加します。jwsc
のcompiledWsdl
属性を使用して、wsdlc
Antタスクによって生成されるJARファイルの名前を指定します。
<taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}"> <jws file="examples/webservices/wsdlc/TemperatureService_TemperaturePortImpl.java" compiledWsdl="${compiledWsdl-dir}/TemperatureService_wsdl.jar" type="JAXWS"> <WLHttpTransport contextPath="temp" serviceUri="TemperatureService" portName="TemperaturePort"> </WLHttpTransport> </jws> </jwsc> </target>
上記の例で注目すべき点は以下のとおりです。
<jws>
要素のtype
属性では、Webサービスのタイプ(JAX-WSまたはJAX-RPC)が指定されています。
jwsc
Antタスクの<jws>
要素の<WLHttpTransport>
子要素では、HTTP/Sトランスポートを介してWebサービスを呼び出す際に使用されるURLのコンテキスト・パスとサービスURIセクションに加え、生成されるWSDLのポートの名前も指定されています。
build-service
ターゲットを実行してデプロイ可能なWebサービスを生成します。
prompt> ant build-service
JWSファイルを更新して再ビルドする場合、このターゲットを再実行します。
WebサービスをデプロイするWebLogic Serverインスタンスを起動します。
WebLogic Server管理コンソールまたはwldeploy
Antタスクを使用して、エンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。いずれの場合でも、output
ディレクトリにあるwsdlcEar
エンタープライズ・アプリケーションをデプロイします。
wldeploy
Antタスクを使用するには、以下のターゲットをbuild.xml
ファイルに追加します。
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="deploy"> <wldeploy action="deploy" name="wsdlcEar" source="output/wsdlcEar" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target>
wls.username
、wls.password
、wls.hostname
、wls.port
、およびwls.server.name
の値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。
deploy
ターゲットを実行して、WARファイルをデプロイします。
prompt> ant deploy
ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。
http://host:port/temp/TemperatureService?WSDL
上記のURLのコンテキスト・パスとサービスURIの部分は元の黄金のWSDLで指定されています。WebLogic Serverインスタンスのホスト名とポートを使用します。デプロイされたWSDLファイルと元のWSDLファイルは、エンドポイント・アドレスのホストとポート以外は同じであることに注目してください。
build.xml
ファイルでclean
、build-service
、undeploy
、deploy
の各ターゲットを使用すると、開発プロセスの一環としてWebサービスの更新、再ビルド、アンデプロイおよび再デプロイを繰り返し実行できます。
Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「WebLogic WebサービスからのWebサービスの呼出し」を参照してください。
<?xml version="1.0"?> <definitions name="TemperatureService" targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:tns="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" > <types> <xsd:schema targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="getTempRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="zip" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getTempResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="return" type="xsd:float"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="getTempRequest"> <part name="parameters" element="tns:getTempRequest"/> </message> <message name="getTempResponse"> <part name="parameters" element="tns:getTempResponse"/> </message> <portType name="TemperaturePortType"> <operation name="getTemp"> <input message="tns:getTempRequest"/> <output message="tns:getTempResponse"/> </operation> </portType> <binding name="TemperatureBinding" type="tns:TemperaturePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="getTemp"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="TemperatureService"> <documentation> Returns current temperature in a given U.S. zipcode </documentation> <port name="TemperaturePort" binding="tns:TemperatureBinding"> <soap:address location="http://localhost:7001/temp/TemperatureService"/> </port> </service> </definitions>
package examples.webservices.wsdlc;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
/**
* Returns current temperature in a given U.S. zipcode
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.8-b13684
* Generated source version: 2.2
*
*/
@WebService(
portName = "TemperaturePort",
serviceName = "TemperatureService",
targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl"
wsdlLocation = "/wsdls/TemperatureService.wsdl",
endpointInterface = "examples.webservices.wsdlc.TemperaturePortType")
@BindingType("http://schemas.xmlsoap.org/wsdl/soap/http")
public class TemperatureService_TemperaturePortImpl implements
TemperaturePortType
{
public TemperatureService_TemperaturePortImpl() { }
/**
*
* @param zip
* @return
* returns float
*/
public float getTemp(String zip) {
return 1.234f;
}
}
以下のbuild.xml
ファイルでは、プロパティを使用してファイルを簡素化しています。
<project default="all"> <!-- set global properties for this build --> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="myserver" /> <property name="ear.deployed.name" value="wsdlcEar" /> <property name="example-output" value="output" /> <property name="compiledWsdl-dir" value="${example-output}/compiledWsdl" /> <property name="impl-dir" value="${example-output}/impl" /> <property name="ear-dir" value="${example-output}/wsdlcEar" /> <property name="clientclass-dir" value="${example-output}/clientclasses" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="wsdlc" classname="weblogic.wsee.tools.anttasks.WsdlcTask"/> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="all" depends="clean,generate-from-wsdl,build-service,deploy,client" /> <target name="clean" depends="undeploy"> <delete dir="${example-output}"/> </target> <target name="generate-from-wsdl"> <wsdlc srcWsdl="wsdl_files/TemperatureService.wsdl" destJwsDir="${compiledWsdl-dir}" destImplDir="${impl-dir}" packageName="examples.webservices.wsdlc" type="JAXWS"/> </target> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}"> <jws file="examples/webservices/wsdlc/TemperatureService_TemperaturePortImpl.java" compiledWsdl="${compiledWsdl-dir}/TemperatureService_wsdl.jar" type="JAXWS"> <WLHttpTransport contextPath="temp" serviceUri="TemperatureService" portName="TemperaturePort"/> </jws> </jwsc> </target> <target name="deploy"> <wldeploy action="deploy" name="${ear.deployed.name}" source="${ear-dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="undeploy"> <wldeploy action="undeploy" name="${ear.deployed.name}" failonerror="false" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/temp/TemperatureService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.wsdlc.client" type="JAXWS"> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/wsdlc/client/**/*.java"/> </target> <target name="run"> <java classname="examples.webservices.wsdlc.client.TemperatureClient" fork="true" failonerror="true" > <classpath refid="client.class.path"/> <arg line="http://${wls.hostname}:${wls.port}/temp/TemperatureService" /> </java> </target> </project>