9 JAX-WS Webサービス・クライアントの開発例
この章の内容は次のとおりです:
各例では、簡単なWebLogic Webサービスを作成する手順や、デプロイ済のWebサービスから操作を呼び出す手順を示します。これらの例には基本的なJavaコードとAnt build.xmlファイルが含まれており、それらをユーザーの開発環境で使用してサンプルを再現することや、現在の開発環境とは別の環境で指示に従ってサンプルを作成して実行することができます。
例ではサンプルで使用されるプロセスやツールについては詳しく説明していません。詳細は、以降の章を参照してください。
ノート:
高度なWebサービス機能を示すベスト・プラクティスの例については、JAX-WS Webサービス・クライアントを開発するためのロードマップおよび信頼性のあるWebサービスとクライアントを開発するためのロードマップを参照してください。
JAX-WS Java SEクライアントの開発
ノート:
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サービスを作成してデプロイ済であると仮定しています。
build.xmlファイルのbuild-clientおよびrunターゲットを使用して、開発プロセスの一環としてJavaクライアント・アプリケーションを繰返し更新、再ビルド、および実行できます。
サンプル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());
}
}
Javaクライアント・アプリケーションをビルドするためのサンプルAntビルド・ファイル
次の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 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サービスは正常にデプロイ済であると想定しています。
Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「JAX-WS Java SEクライアントの開発」を参照してください。
サンプルClientServiceImpl.java JWSファイル
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() + "'";
}
}
ClientServiceをビルドするためのサンプルAntビルド・ファイル
次の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>