この章では、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サービスとクライアントを開発するためのロードマップ」を参照してください。 |
注意: WebLogic Serverで実行されている(WebLogic Serverクラスパスにアクセスする)任意のJava SEまたはJava EEアプリケーションからWebサービスを呼び出すことができます。WebLogic Serverライブラリが使用できない環境で実行されているスタンドアロンJavaアプリケーションからのWebサービスの呼出しは、JAX-WS Webサービスのこのリリースではサポートされていません。 |
クライアント・アプリケーションからデプロイ済のWebサービスの操作を呼び出す場合、WebサービスはWebLogic Serverにデプロイされている場合と、.NETなどの他のアプリケーション・サーバーにデプロイされている場合があります。知る必要があるのは、パブリック・コントラクト・ファイル、つまりWSDLのURLのみです。
Javaクライアント・アプリケーションを記述する他に、clientgen
WebLogic WebサービスAntタスクを実行して、クライアント・アプリケーションがWebサービス操作を呼び出すために必要なアーティファクトも生成する必要があります。次のようなアーティファクトがあります。
呼出しの対象となる特定のWebサービスに対するService
インタフェース実装のJavaクラス。
JAXBデータ・バインディング・アーティファクト。
WSDLファイルで指定されたユーザー定義XMLスキーマ・データ型のJavaクラス。
次の例では、「ユーザー定義のデータ型を使用するWebサービスの作成」
で説明している、ComplexService
WebLogic WebサービスのechoComplexType操作を呼び出すJavaクライアント・アプリケーションを作成する方法を示します。echoComplexType
操作は、ユーザー定義のデータ型であるBasicStruct
をパラメータおよび戻り型として使用します。
注意: この手順では、ComplexService 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/simple_client
プロジェクト・ディレクトリの下に、src
ディレクトリと、Javaクライアント・アプリケーション(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。
prompt> cd /myExamples/simple_client prompt> mkdir src/examples/webservices/simple_client
プロジェクト・ディレクトリ内に標準のAnt build.xml
ファイルを作成し、taskdef
Antタスクを追加して、clientgen
タスクの完全修飾Javaクラス名を指定します。
<project name="webservices-simple_client" default="all"> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> </project>
完全なサンプルbuild.xmlファイルについては、「Javaクライアント・アプリケーションをビルドするためのサンプルAntビルド・ファイル」
を参照してください。完全なbuild.xml
ファイルでは、クライアント・クラスの出力ディレクトリの名前を常にハード・コードして使用するのではなく、${clientclass-dir}
などのプロパティを使用します。
次のようなclientgen
およびjavac
Antタスクの呼出しを、build-client
ターゲット内にラップして、build.xml
ファイルに追加します。
<target name="build-client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" destDir="output/clientclass" packageName="examples.webservices.simple_client" type="JAXWS"/> <javac srcdir="output/clientclass" destdir="output/clientclass" includes="**/*.java"/> <javac srcdir="src" destdir="output/clientclass" includes="examples/webservices/simple_client/*.java"/> </target>
clientgen
Antタスクでは、デプロイされたComplexService
WebサービスのWSDLを使用して必要なアーティファクトを生成し、指定されたパッケージ名を使用してそれらをoutput/clientclass
ディレクトリに格納します。変数は、WebサービスをホストしているWebLogic Serverインスタンスの実際のホスト名とポートに置き換えてください。
この例では、パッケージ名はクライアント・アプリケーションと同じパッケージ名examples.webservices.simple_client
に設定されます。パッケージ名をクライアント・アプリケーションとは異なる名前に設定した場合は、適切なクラス・ファイルをインポートする必要があります。たとえば、examples.webservices.complex
というパッケージ名にした場合、クライアント・アプリケーションの以下のクラス・ファイルをインポートする必要があります。
import examples.webservices.complex.BasicStruct; import examples.webservices.complex.ComplexPortType; import examples.webservices.complex.ComplexService;
clientgen
Antタスクはexamples.webservices.simple_client.BasicStruct
JavaBeanクラスも自動的に生成します。このクラスは、WSDLで指定されたユーザー定義のデータ型のJava表現です。
build-client
ターゲットでは、clientgen
の他に標準のjavac
Antタスクも指定して、次の手順で説明するJavaプログラムを含む、すべてのJavaコードをクラス・ファイルにコンパイルします。
clientgen
AntタスクのdestFile
属性を使用すると、生成されたJavaファイルを自動的にコンパイルして、すべてのアーティファクトをJARファイルにパッケージ化できます。詳細と例については、『Oracle WebLogic Server WebLogic Webサービス・リファレンス』のclientgenに関する項を参照してください。
echoComplexType
操作を呼び出すJavaクライアント・アプリケーション・ファイルを作成します。
任意のJava IDEまたはテキスト・エディタを開いて、「サンプルJavaクライアント・アプリケーション」
で指定されたコードを使用してMain.javaというJavaファイルを作成します。
アプリケーションは標準JAX-WSのガイドラインに従って、clientgen
によって生成されたService
インタフェースのWebサービス固有の実装を使用してWebサービスの操作を呼び出します。詳細は、第8章「Webサービス・クライアントの開発」を参照してください。
Main.java
ファイルをメイン・プロジェクト・ディレクトリのsrc/examples/webservices/simple_client
サブディレクトリに保存します。
コマンド・ラインでbuild-client
ターゲットを指定して、clientgen
およびjavac
Antタスクを実行します。
prompt> ant build-client
output/clientclass
ディレクトリを見て、clientgen
Antタスクによって生成されたファイルとアーティファクトを確認します。
Main
アプリケーションの実行に使用される以下のターゲットをbuild.xml
ファイルに追加します。
<path id="client.class.path"> <pathelement path="output/clientclass"/> <pathelement path="${java.class.path}"/> </path> <target name="run" > <java fork="true" classname="examples.webservices.simple_client.Main" failonerror="true" > <classpath refid="client.class.path"/> </target>
run
ターゲットはMain
アプリケーションを呼び出し、デプロイされたWebサービスのWSDL URLを1つの引数として渡します。classpath
要素では、<path>
タスクで作成される参照を使用して、CLASSPATHにclientclass
ディレクトリが追加されます。
run
ターゲットを実行してechoComplexType
操作を呼び出します。
prompt> ant run
呼出しに成功すると、次のような最終出力が表示されます。
run: [java] echoComplexType called. Result: 999, Hello Struct
build.xml
ファイルのbuild-client
およびrun
ターゲットを使用して、開発プロセスの一環としてJavaクライアント・アプリケーションを繰返し更新、再ビルド、および実行できます。
以下に、echoComplexType
操作を呼び出す簡単なJavaクライアント・アプリケーションを示します。<clientgen>
packageName
属性はクライアント・アプリケーションと同じパッケージ名に設定されているため、<clientgen>
生成ファイルをインポートする必要はありません。
package examples.webservices.simple_client; /** * This is a simple Java application that invokes the * echoComplexType operation of the ComplexService web service. */ public class Main { public static void main(String[] args) { ComplexService test = new ComplexService(); ComplexPortType port = test.getComplexPortTypePort(); BasicStruct in = new BasicStruct(); in.setIntValue(999); in.setStringValue("Hello Struct"); BasicStruct result = port.echoComplexType(in); System.out.println("echoComplexType called. Result: " + result.getIntValue() + ", " + result.getStringValue()); } }
次のbuild.xml
ファイルでは、Javaクライアント・アプリケーションをビルドするタスクが定義されています。この例では、プロパティを使用してファイルを簡素化しています。
<project name="webservices-simple_client" default="all"> <!-- set global properties for this build --> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="example-output" value="output" /> <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="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <target name="clean" > <delete dir="${clientclass-dir}"/> </target> <target name="all" depends="clean,build-client,run" /> <target name="build-client"> <clientgen type="JAXWS" wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.simple_client"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/simple_client/*.java"/> </target> <target name="run" > <java fork="true" classname="examples.webservices.simple_client.Main" failonerror="true" > <classpath refid="client.class.path"/> </java> </target> </project>
デプロイされたWebLogic Webサービス内からWebサービス(WebLogic、Microsoft .NETなど)を呼び出すことができます。
その手順は、「JAX-WS Java SEクライアントの開発」で説明した手順と似ていますが、この場合はclientgen
Antタスクを実行してクライアント・スタブを生成するのではなく、jwsc
Antタスク内で<jws>
の子要素<clientgen>
を使用します。jwsc
Antタスクでは、生成されたクライアント・スタブが自動的に呼出し側のWebサービスのWARファイルにパッケージ化されるため、即座にWebサービスからアクセスできるようになります。その後で、標準JAX-WSのプログラミング・ガイドラインに従って、他のWebサービスを呼び出すWebサービスを実装するJWSファイルを作成します。
次の例では、「ユーザー定義のデータ型を使用するWebサービスの作成」
で説明したComplexService
WebサービスのechoComplexType操作を呼び出すJWSファイルを記述する方法を示します。
注意: ComplexService 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/service_to_service
プロジェクト・ディレクトリの下に、src
ディレクトリと、JWSおよびクライアント・アプリケーション・ファイル(この手順で後述)のパッケージ名に相当するサブディレクトリを作成します。
prompt> cd /myExamples/service_to_service prompt> mkdir src/examples/webservices/service_to_service
ComplexService
Webサービスを呼び出すWebサービスを実装するJWSファイルを作成します。
任意のJava IDEまたはテキスト・エディタを開いて、「サンプルClientServiceImpl.java JWSファイル」
で指定されたJavaコードを使用してClientServiceImpl.javaというJavaファイルを作成します。
サンプルJWSファイルでは、1つのパブリック・メソッドcallComplexService()
を含むClientServiceImpl
というJavaクラスが示されています。このJavaクラスでは、後でjwsc
Antタスクで生成されるJAX-WSスタブと、BasicStruct
JavaBean(同じくclientgen
で生成される)をインポートしますが、BasicStruct JavaBeanは、ComplexService
WebサービスのechoComplexType
操作のパラメータと戻り値のデータ型です。
ClientServiceImpl
Javaクラスでは1つのメソッドcallComplexService()
を定義しています。このメソッドは、1つのパラメータBasicStruct
を取り、このパラメータがComplexService
WebサービスのechoComplexType
操作に渡されます。このメソッドは標準のJAX-WS APIを使用し、jwsc
によって生成されたスタブを使用して、ComplexService
のService
とPortType
を取得します。その後で、echoComplexType
操作を呼び出します。
ClientServiceImpl.java
ファイルをsrc/examples/webservices/service_to_service
ディレクトリに保存します。
プロジェクト・ディレクトリ内に標準のAnt build.xml
ファイルを作成して、次のタスクを追加します。
<project name="webservices-service_to_service" default="all"> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> </project>
taskdef
タスクでは、jwsc
Antタスクの完全なクラス名を定義しています。
この手順で説明される追加のターゲット(clean、deploy
、undeploy
、client
、run
など)が含まれている、完全なサンプルbuild.xml
ファイルについては、「ClientServiceをビルドするためのサンプルAntビルド・ファイル」
を参照してください。完全なbuild.xml
ファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}
などのプロパティも使用しています。
次のようなjwsc
Antタスクの呼出しを、build-service
ターゲット内にラップして、build.xml
ファイルに追加します。
<target name="build-service"> <jwsc srcdir="src" destdir="output/ClientServiceEar" > <jws file="examples/webservices/service_to_service/ClientServiceImpl.java" type="JAXWS"> <WLHttpTransport contextPath="ClientService" serviceUri="ClientService" portName="ClientServicePort"/> <clientgen type="JAXWS" wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" packageName="examples.webservices.complex" /> </jws> </jwsc> </target>
この例では、jwsc
Antタスクの<jws>
要素の<clientgen>
子要素で、jwsc
がJWSファイルをコンパイルするだけでなく、WSDLファイルに記述されているWebサービスを呼び出すために必要なクライアント・アーティファクトも生成およびコンパイルすることを指定しています。
この例では、パッケージ名はexamples.webservices.complex
に設定され、これはクライアント・アプリケーションのパッケージ名examples.webservices.simple_client
とは異なります。そのため、クライアント・アプリケーションの適切なクラス・ファイルをインポートする必要があります。
import examples.webservices.complex.BasicStruct; import examples.webservices.complex.ComplexPortType; import examples.webservices.complex.ComplexService;
パッケージ名がクライアント・アプリケーションと同じパッケージ名に設定されている場合、インポートの呼出しは省略できます。
コマンド・ラインでbuild-service
ターゲットを指定して、jwsc
Antタスクを実行します。
prompt> ant build-service
WebサービスをデプロイするWebLogic Serverインスタンスを起動します。
WebLogic Server管理コンソールまたはwldeploy
Antタスクを使用して、エンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。いずれの場合でも、output
ディレクトリにあるClientServiceEar
エンタープライズ・アプリケーションをデプロイします。
wldeploy
Antタスクを使用するには、以下のターゲットをbuild.xml
ファイルに追加します。
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="deploy"> <wldeploy action="deploy" name="ClientServiceEar" source="ClientServiceEar" 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/ClientService/ClientService?WSDL
Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「JAX-WS Java SEクライアントの開発」を参照してください。
echoComplexType
操作を呼び出す簡単なWebサービス・クライアント・アプリケーションを次に示します。
package examples.webservices.service_to_service; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.WebServiceRef; // Import the BasicStruct data type, generated by clientgen and used // by the ComplexService Web Service import examples.webservices.complex.BasicStruct; // Import the JAX-WS stubs generated by clientgen for invoking // the ComplexService web service. import examples.webservices.complex.ComplexPortType; import examples.webservices.complex.ComplexService; @WebService(name="ClientPortType", serviceName="ClientService", targetNamespace="http://examples.org") public class ClientServiceImpl { // Use the @WebServiceRef annotation to define a reference to the // ComplexService web service. @WebServiceRef() ComplexService test; @WebMethod() public String callComplexService(BasicStruct input, String serviceUrl) { // Create a port stub to invoke ComplexService ComplexPortType port = test.getComplexPortTypePort(); // Invoke the echoComplexType operation of ComplexService BasicStruct result = port.echoComplexType(input); System.out.println("Invoked ComplexPortType.echoComplexType." ); return "Invoke went okay! Here's the result: '" + result.getIntValue() + ", " + result.getStringValue() + "'"; } }
次のbuild.xml
ファイルでは、クライアント・アプリケーションをビルドするタスクが定義されています。この例では、プロパティを使用してファイルを簡素化しています。
以下のbuild.xml
ファイルでは、プロパティを使用してファイルを簡素化しています。
<project name="webservices-service_to_service" 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="ClientServiceEar" /> <property name="example-output" value="output" /> <property name="ear-dir" value="${example-output}/ClientServiceEar" /> <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/service_to_service/ClientServiceImpl.java" type="JAXWS"> <WLHttpTransport contextPath="ClientService" serviceUri="ClientService" portName="ClientServicePort"/> <clientgen type="JAXWS" wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" packageName="examples.webservices.complex" /> </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}/ClientService/ClientService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.service_to_service.client" type="JAXWS"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/service_to_service/client/**/*.java"/> </target> <target name="run"> <java classname="examples.webservices.service_to_service.client.Main" fork="true" failonerror="true" > <classpath refid="client.class.path"/> </java> </target> </project>