ヘッダーをスキップ
Oracle® Fusion Middleware Oracle WebLogic Server JAX-RPC Webサービス・スタート・ガイド
11g リリース1(10.3.6)
B61647-04
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

2 ユース・ケースとサンプル

この章では、Java API for XML-based RPC (JAX-RPC)を使用したWebLogic Webサービスの一般的なユース・ケースとサンプルについて説明します。

この章では、以下のトピックについて説明します。

各ユース・ケースでは、簡単なWebLogic Webサービスを作成する手順や、デプロイ済のWebサービスから操作を呼び出す手順を示します。これらの例には基本的なJavaコードとAnt build.xmlファイルが含まれており、それらをユーザーの開発環境で使用してサンプルを再現したり、現在の開発環境とは別の環境で指示に従ってサンプルを作成して実行したりできます。

ユース・ケースではサンプルで使用されるプロセスやツールについては詳しく説明していません。詳細は、以降の章を参照してください。

簡単なHelloWorld Webサービスの作成

この項では、1つの操作を含む非常に簡単なWebサービスの作成方法について説明します。Webサービスを実装するJava Webサービス(JWS)・ファイルでは必須のJWSアノテーション@WebServiceを1つのみ使用しています。JWSファイルは、JWSメタデータ・アノテーションを使用してWebサービスの形式を指定した標準のJavaファイルです。メタデータ・アノテーションはJDK 5.0で導入されたもので、Webサービス・ファイルにアノテーションを付けるために使用される一連のアノテーションをJWSアノテーションと呼びます。WebLogic Webサービスは標準のJWSアノテーションを使用します。サポートされるJWSアノテーションの完全なリストについては、『Oracle Fusion Middleware Oracle WebLogic Server WebLogic Webサービス・リファレンス』のWebサービスのアノテーションのサポートに関する項を参照してください。

次の例では、1つの操作sayHelloWorldを含むHelloWorldServiceというWebサービスを作成する方法を示します。簡単にするために、操作は入力された文字列値を戻すものとします。

  1. WebLogic Server環境を設定します。

    コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbinサブディレクトリにあるsetDomainEnv.cmd (Windows)またはsetDomainEnv.sh (UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、MW_HOME/user_projects/domains/domainNameです。MW_HOMEはOracle製品の最上位のインストール・ディレクトリ、domainNameはドメインの名前です。

  2. 次のようにプロジェクト・ディレクトリを作成します。

       prompt> mkdir /myExamples/hello_world
    
  3. プロジェクト・ディレクトリの下に、srcディレクトリと、JWSファイル(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。

       prompt> cd /myExamples/hello_world
        prompt> mkdir src/examples/webservices/hello_world
    
  4. Webサービスを実装するJWSファイルを作成します。

    任意のJava IDEまたはテキスト・エディタを開いて、「サンプルHelloWorldImpl.java JWSファイル」で指定されたJavaコードを使用してHelloWorldImpl.javaというJavaファイルを作成します。

    サンプルJWSファイルでは、1つのパブリック・メソッドsayHelloWorld(String)を含むHelloWorldImplというJavaクラスが示されています。@WebServiceアノテーションは、JavaクラスがHelloWorldServiceというWebサービスを実装することを指定しています。デフォルトでは、すべてのパブリック・メソッドが操作として公開されます。

  5. src/examples/webservices/hello_worldディレクトリにHelloWorldImpl.javaファイルを保存します。

  6. プロジェクト・ディレクトリ(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>
    

    この手順で説明される追加のターゲット(cleanundeployclient、およびrunなど)が含まれている、完全なサンプルbuild.xmlファイルについては、「HelloWorldImpl.javaのサンプルAntビルド・ファイル」を参照してください。完全なbuild.xmlファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}などのプロパティも使用しています。

  7. 次のような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="JAXRPC"/>
        </jwsc>
      </target>
    

    jwsc WebLogic WebサービスAntタスクは、補助的なアーティファクト(デプロイメント記述子、ユーザー定義のデータ型のシリアライゼーション・クラス、WSDLファイルなど)を生成し、ユーザーが作成および生成したJavaコードをコンパイルして、すべてのアーティファクトを、後でWebLogic Serverにデプロイされるエンタープライズ・アプリケーションEARにアーカイブ化します。

  8. コマンド・ラインでbuild-serviceターゲットを指定して、jwsc Antタスクを実行します。

    prompt> ant build-service
    

    output/helloWorldEarディレクトリを見て、jwsc Antタスクによって生成されたファイルとアーティファクトを確認します。

  9. WebサービスをデプロイするWebLogic Serverインスタンスを起動します。

  10. 管理コンソールまたは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.usernamewls.passwordwls.hostnamewls.port、およびwls.server.nameの値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。

    deployターゲットを実行して、WARファイルをデプロイします。

      prompt> ant deploy
    
  11. ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。

    http://host:port/HelloWorldImpl/HelloWorldImpl?WSDL
    

    WLHttpTransport JWSアノテーションのcontextPathおよびserviceUri属性の値を使用してURLを構築します。ただし、このユース・ケースのJWSファイルにはWLHttpTransportアノテーションが含まれていないので、contextPathおよびserviceUri属性のデフォルト値(JWSファイル内のJavaクラスの名前)を使用します。これらの属性は、次の「ユーザー定義のデータ型を使用するWebサービスの作成」の例で明示的に設定されます。WebLogic Serverインスタンスのホスト名とポートを使用します。

build.xmlファイルでcleanbuild-serviceundeploydeployの各ターゲットを使用すると、開発プロセスの一環としてWebサービスの更新、再ビルド、アンデプロイおよび再デプロイを繰り返し実行できます。

Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「Java SEクライアントからのWebサービスの呼出し」を参照してください。

サンプルHelloWorldImpl.java JWSファイル

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 + "'";
   }
 }

HelloWorldImpl.javaのサンプルAntビルド・ファイル

以下の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="JAXRPC"/>
    </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/HelloWorldImpl?WSDL"
       destDir="${clientclass-dir}"
       packageName="examples.webservices.hello_world.client"
       type="JAXRPC"/>
    <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/HelloWorldImpl"  />
     </java> </target>
</project>

ユーザー定義のデータ型を使用するWebサービスの作成

前のユース・ケースでは、Webサービス操作のパラメータと戻り値として、単純データ型Stringのみを使用していました。次の例では、ユーザー定義のデータ型(特にBasicStructというJavaBean)を操作のパラメータおよび戻り値として使用するWebサービスの作成方法を示します。

Webサービスでユーザー定義のデータ型を使用する場合、データ型のJavaソースを作成して、それをJWSファイル内で適切に使用する以外に、プログラマが実際にすべきことはほとんどありません。jwsc Antタスクは、JWSファイルにユーザー定義のデータ型がある場合、XML表現(SOAPメッセージで使用)とJava表現(WebLogic Serverで使用)との間でデータを変換するのに必要な、すべてのデータ・バインディング・アーティファクトを自動的に生成します。データ・バインディング・アーティファクトには、Javaユーザー定義データ型に相当するXMLスキーマ、JAX-RPCの型マッピング・ファイルなどがあります。

以下の手順は、「簡単なHelloWorld Webサービスの作成」の手順とよく似ています。そのため、説明では必要な手順をすべて示していますが、簡単なHelloWorldサンプルと異なる手順についてのみ、詳細を述べています。

  1. WebLogic Server環境を設定します。

    コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbinサブディレクトリにあるsetDomainEnv.cmd (Windows)またはsetDomainEnv.sh (UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、MW_HOME/user_projects/domains/domainNameです。MW_HOMEはOracle製品の最上位のインストール・ディレクトリ、domainNameはドメインの名前です。

  2. プロジェクト・ディレクトリを作成します。

       prompt> mkdir /myExamples/complex
    
  3. プロジェクト・ディレクトリの下に、srcディレクトリと、JWSファイル(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。

       prompt> cd /myExamples/complex
       prompt> mkdir src/examples/webservices/complex
    
  4. BasicStruct JavaBeanのソースを作成します。

    任意のJava IDEまたはテキスト・エディタを開いて、「サンプルBasicStruct JavaBean」で指定されたJavaコードを使用して BasicStruct.javaというJavaファイルをプロジェクト・ディレクトリに作成します。

  5. BasicStruct.javaファイルをプロジェクト・ディレクトリのsrc/examples/webservices/complexサブディレクトリに保存します。

  6. 「サンプルComplexImpl.java JWSファイル」で指定されたJavaコードを使用して、Webサービスを実装するJWSファイルを作成します。

    このサンプルJWSファイルでは、複数のJWSアノテーションを使用しています。@WebMethodでは、メソッドがWebサービス操作として公開されることを明示的に指定し、操作名をデフォルトのメソッド名echoStructからechoComplexTypeに変更します。@WebParam@WebResultでは、パラメータと戻り値を構成します。@SOAPBindingでは、Webサービスのタイプを指定します。@WLHttpTransportでは、Webサービスの呼出しに使用されるURIを指定します。また、ComplexImpl.java JWSファイルではexamples.webservice.complex.BasicStructクラスをインポートして、echoStruct()メソッドのパラメータと戻り値としてユーザー定義データ型のBasicStructを使用します。

    JWSファイルの作成に関するより詳細な情報については、第4章「JWSファイルのプログラミング」を参照してください。

  7. ComplexImpl.javaファイルをプロジェクト・ディレクトリのsrc/examples/webservices/complexサブディレクトリに保存します。

  8. プロジェクト・ディレクトリ内に標準の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ビルド・ファイル」を参照してください。

  9. 次のようなjwsc Antタスクの呼出しを、build-serviceターゲット内にラップして、build.xmlファイルに追加します。

    <target name="build-service">
      <jwsc
         srcdir="src"
         destdir="output/ComplexServiceEar" >
         <jws file="examples/webservices/complex/ComplexImpl.java" 
            type="JAXRPC">
           <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のポートの名前も指定されています。この値は、@WLHttpTransport属性を使用してJWSファイルで指定されている値をオーバーライドします。コンテキスト・パスの定義の詳細は、『Oracle Fusion Middleware Oracle WebLogic Server WebLogic Webサービス・リファレンス』のWebLogic Webサービスのコンテキスト・パスの定義に関する項を参照してください。

  10. jwsc Antタスクを使用します。

    prompt> ant build-service
    

    output/ComplexServiceEarディレクトリを見て、jwsc Antタスクによって生成されたファイルとアーティファクトを確認します。

  11. WebサービスをデプロイするWebLogic Serverインスタンスを起動します。

  12. 管理コンソールまたはwldeploy Antタスクを使用して、ComplexServiceEarエンタープライズ・アプリケーションにパッケージ化されたWebサービスをWebLogic Serverにデプロイします。例:

      prompt> ant deploy
    
  13. 管理コンソールまたは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.usernamewls.passwordwls.hostnamewls.port、およびwls.server.nameの値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。

    deployターゲットを実行して、WARファイルをデプロイします。

      prompt> ant deploy
    
  14. ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。

    http://host:port/complex/ComplexService?WSDL
    

Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「Java SEクライアントからのWebサービスの呼出し」を参照してください。

サンプルBasicStruct JavaBean

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;
   }
 }

サンプルComplexImpl.java JWSファイル

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 WebLogic-specific JWS annotation interface
import weblogic.jws.WLHttpTransport;
// 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)
// WebLogic-specific JWS annotation that specifies the context path and service
 // URI used to build the URI of the Web Service is "complex/ComplexService"
@WLHttpTransport(contextPath="complex", serviceUri="ComplexService",
                  portName="ComplexServicePort")
/**
  * 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;
   }
 }

ComplexImpl.java JWSファイルのサンプルAntビルド・ファイル

以下の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="JAXRPC">
         <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="JAXRPC"/>
    <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>

WSDLファイルからのWebサービスの作成

Webサービスの作成に関するもう1つの一般的なユース・ケースは、既存のWSDLファイル(黄金のWSDLとも呼ばれます)から開始することです。WSDLファイルはWebサービスがどのようなものかを指定するパブリック・コントラクトであり、サポートされる操作のリスト、各操作のシグネチャと形式、操作を呼び出すときに使用されるプロトコルとトランスポート、データをトランスポートするときに使用されるXMLスキーマ・データ型などを指定します。WSDLファイルに基づいて、WebLogic Serverにデプロイできるように、Webサービスを実装するアーティファクトを生成します。wsdlc Antタスクを使用して次のアーティファクトを生成します。

一般に、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つあります。

  1. WebLogic Server環境を設定します。

    コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbinサブディレクトリにあるsetDomainEnv.cmd (Windows)またはsetDomainEnv.sh (UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、MW_HOME/user_projects/domains/domainNameです。MW_HOMEはOracle製品の最上位のインストール・ディレクトリ、domainNameはドメインの名前です。

  2. 作業ディレクトリを作成します。

      prompt> mkdir /myExamples/wsdlc
    
  3. WSDLファイルをコンピュータ上のアクセス可能なディレクトリに置きます。

    この例では、WSDLファイルはTemperatureService.wsdlという名前で/myExamples/wsdlc/wsdl_filesディレクトリにあると仮定しています。ファイルの完全なリストについては、「サンプルWSDLファイル」を参照してください。

  4. プロジェクト・ディレクトリ内に標準のAnt build.xmlファイルを作成し、taskdef Antタスクを追加して、wsdlcタスクの完全修飾Javaクラス名を指定します。

    <project name="webservices-wsdlc" default="all">
      <taskdef name="wsdlc"
                classname="weblogic.wsee.tools.anttasks.WsdlcTask"/>
    </project>
    

    この手順で説明される追加のターゲット(cleanundeployclient、およびrunなど)が含まれている、完全なサンプルbuild.xmlファイルについては、「TemperatureServiceのサンプルAntビルド・ファイル」を参照してください。完全なbuild.xmlファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}などのプロパティも使用しています。

  5. 次のような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" />
      </target>
    

    このサンプルwsdlcタスクでは、JWS SEIとデータ・バインディング・アーティファクトを含むJARファイルを、カレント・ディレクトリ下のoutput/compiledWsdlディレクトリに生成します。また、JWS SEIの部分的な実装ファイル(TemperaturePortTypeImpl.java)をoutput/impl/examples/webservices/wsdlcディレクトリに生成します(このディレクトリは、destImplDirで指定されたoutputディレクトリと、パッケージ名で指定されたディレクトリ階層を組み合せたものです)。生成されたすべてのJWSファイルはexamples.webservices.wsdlcパッケージにパッケージ化されます。

  6. コマンド・ラインでgenerate-from-wsdlターゲットを指定して、wsdlc Antタスクを実行します。

    prompt> ant generate-from-wsdl
    

    wsdlc Antタスクによってアーティファクトとファイルが生成されたことを調べるには、outputディレクトリを確認します。

  7. 任意のJava IDEまたはテキスト・エディタを使用して、生成されたoutput/impl/examples/webservices/wsdlc/TemperaturePortTypeImpl.java JWS実装ファイルを更新し、必要な処理を行うようにメソッドにJavaコードを追加します。

    例については「サンプルTemperaturePortType Java実装ファイル」を参照してください。追加されたJavaコードが太字になっています。生成されたJWS実装ファイルには@WebServiceおよび@WLHttpTransportアノテーションの値が自動的に含まれており、その値は元のWSDLファイルの値に対応しています。


    注意:

    「WSDLから開始する」ユース・ケースのJWS実装ファイルに追加できるJWSアノテーションには制限があります。詳細は、『Oracle Fusion Middleware Oracle WebLogic Server Webサービス・リファレンス』の「wsdlc」を参照してください。

    簡単にするために、サンプルのTemperaturePortTypeImpl.javaにあるgetTemp()メソッドは、ハード・コード化された数字を返します。実際のファイルの場合、このメソッドの実装では、特定の郵便番号で現在の気温をルックアップします。

  8. 更新したTemperaturePortTypeImpl.javaファイルを永続的なディレクトリ(プロジェクト・ディレクトリの下のsrcディレクトリなど。パッケージ名に対応する子ディレクトリも作成します)にコピーします。

    prompt> cd /examples/wsdlc
     prompt> mkdir src/examples/webservices/wsdlc
     prompt> cp output/impl/examples/webservices/wsdlc/TemperaturePortTypeImpl.java \
                src/examples/webservices/wsdlc/TemperaturePortTypeImpl.java
    
  9. 更新されたJWS実装クラスに対してjwsc Antタスクを実行するbuild-serviceターゲットを、build.xmlファイルに追加します。jwsccompiledWsdl属性を使用して、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/TemperaturePortTypeImpl.java"
                compiledWsdl="${compiledWsdl-dir}/TemperatureService_wsdl.jar"
                type="JAXRPC">
              <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のポートの名前も指定されています。この値は、@WLHttpTransport属性を使用してJWSファイルで指定されている値をオーバーライドします。

  10. build-serviceターゲットを実行してデプロイ可能なWebサービスを生成します。

    prompt> ant build-service
    

    JWSファイルを更新して再ビルドする場合、このターゲットを再実行します。

  11. WebサービスをデプロイするWebLogic Serverインスタンスを起動します。

  12. 管理コンソールまたは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.usernamewls.passwordwls.hostnamewls.port、およびwls.server.nameの値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。

    deployターゲットを実行して、WARファイルをデプロイします。

      prompt> ant deploy
    
  13. ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。

    http://host:port/temp/TemperatureService?WSDL
    

    上記のURLのコンテキスト・パスとサービスURIの部分は元の黄金のWSDLで指定されています。WebLogic Serverインスタンスのホスト名とポートを使用します。デプロイされたWSDLファイルと元のWSDLファイルは、エンドポイント・アドレスのホストとポート以外は同じであることに注目してください。

build.xmlファイルでcleanbuild-serviceundeploydeployの各ターゲットを使用すると、開発プロセスの一環としてWebサービスの更新、再ビルド、アンデプロイおよび再デプロイを繰り返し実行できます。

Webサービスを実行するには、そのWebサービスを呼び出すクライアントを作成する必要があります。Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「Java SEクライアントからのWebサービスの呼出し」を参照してください。

サンプルWSDLファイル

<?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/" >
        <message name="getTempRequest">
                 <part name="zip" type="xsd:string"/>
         </message>
        <message name="getTempResponse">
                 <part name="return" type="xsd:float"/>
         </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"
                                 namespace="urn:xmethods-Temperature" />
                         </input>
                         <output>
                                 <soap:body use="literal"
                                 namespace="urn:xmethods-Temperature" />
                         </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>

サンプルTemperaturePortType Java実装ファイル

package examples.webservices.wsdlc;
import javax.jws.WebService;
 import weblogic.jws.*;
/**
  * TemperaturePortTypeImpl class implements web service endpoint 
  * interface TemperaturePortType */
@WebService(
   serviceName="TemperatureService",
   targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl"
   endpointInterface="examples.webservices.wsdlc.TemperaturePortType)
@WLHttpTransport(
   contextPath="temp",
   serviceUri="TemperatureService",
   portName="TemperaturePort")
 public class TemperaturePortTypeImpl implements examples.webservices.wsdlc.TemperaturePortType {
  public TemperaturePortTypeImpl() { }
  public float getTemp(java.lang.String zip) {
      return 1.234f;
   }
 }

TemperatureServiceのサンプルAntビルド・ファイル

以下の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" />
  </target>
  <target name="build-service">
    <jwsc
       srcdir="src"
       destdir="${ear-dir}">
      <jws file="examples/webservices/wsdlc/TemperaturePortTypeImpl.java"
            compiledWsdl="${compiledWsdl-dir}/TemperatureService_wsdl.jar"
            type="JAXRPC">
          <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="JAXRPC">
     <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>

Java SEクライアントからのWebサービスの呼出し


注意:

この項で説明するように、WebLogic Serverで実行されている(WebLogic Serverクラスパスにアクセスする)任意のJava SEまたはJava EEアプリケーションからWebサービスを呼び出すことができます。WebLogic Serverのライブラリにアクセスできない環境で実行されているスタンドアロンJavaアプリケーションのサポートについての詳細は、「Webサービス呼出し時のスタンドアロン・クライアントJARファイルの使用」を参照してください。

クライアント・アプリケーションからデプロイ済のWebサービスの操作を呼び出す場合、WebサービスはWebLogic Serverにデプロイされている場合と、.NETなどの他のアプリケーション・サーバーにデプロイされている場合があります。知る必要があるのは、パブリック・コントラクト・ファイル、つまりWSDLのURLのみです。

Javaクライアント・アプリケーションを記述する他に、clientgen WebLogic WebサービスAntタスクを実行して、クライアント・アプリケーションがWebサービス操作を呼び出すために必要なアーティファクトも生成する必要があります。アーティファクトは次を含みます:

次の例では、「ユーザー定義のデータ型を使用するWebサービスの作成」で説明したComplexService WebLogic WebサービスのechoComplexType操作を呼び出すJavaクライアント・アプリケーションを作成する方法を示します。echoComplexType操作は、パラメータおよび戻り値の型としてBasicStructユーザー定義データ型を取ります。


注意:

この手順では、ComplexService Webサービスを作成してデプロイ済であると仮定しています。

  1. WebLogic Server環境を設定します。

    コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbinサブディレクトリにあるsetDomainEnv.cmd (Windows)またはsetDomainEnv.sh (UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、MW_HOME/user_projects/domains/domainNameです。MW_HOMEはOracle製品の最上位のインストール・ディレクトリ、domainNameはドメインの名前です。

  2. プロジェクト・ディレクトリを作成します。

       prompt> mkdir /myExamples/simple_client
    
  3. プロジェクト・ディレクトリの下に、srcディレクトリと、Javaクライアント・アプリケーション(この手順で後述)のパッケージ名に対応するサブディレクトリを作成します。

       prompt> cd /myExamples/simple_client
        prompt> mkdir src/examples/webservices/simple_client
    
  4. プロジェクト・ディレクトリ内に標準の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}などのプロパティを使用します。

  5. 次のような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="JAXRPC"/>
        <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インスタンスの実際のホスト名とポートに置き換えてください。

    
    

    clientgen Antタスクはexamples.webservices.complex.BasicStruct JavaBeanクラスも自動的に生成します。このクラスは、WSDLで指定されたユーザー定義のデータ型のJava表現です。

    build-clientターゲットでは、clientgenの他に標準のjavac Antタスクも指定して、次の手順で説明する簡単なJavaプログラムを含む、すべてのJavaコードをクラス・ファイルにコンパイルします。

    clientgen AntタスクのdestFile属性を使用すると、生成されたJavaファイルを自動的にコンパイルして、すべてのアーティファクトをJARファイルにパッケージ化できます。コンパイルの詳細は、『Oracle Fusion Middleware Oracle WebLogic Server Webサービス・リファレンス』の「clientgen」を参照してください。

  6. echoComplexType操作を呼び出すJavaクライアント・アプリケーション・ファイルを作成します。

    任意のJava IDEまたはテキスト・エディタを開いて、「サンプルJavaクライアント・アプリケーション」で指定されたコードを使用してMain.javaというJavaファイルを作成します。

    Mainクライアント・アプリケーションは、1つの引数(WebサービスのWSDL URL)を取ります。アプリケーションは標準JAX-RPCのガイドラインに従って、clientgenによって生成されたServiceインタフェースのWebサービス固有の実装を使用してWebサービスの操作を呼び出します。また、アプリケーションは、clientgen Antタスクで生成されたBasicStructユーザー定義データ型をインポートして使用します(このデータ型はechoStruct操作のパラメータおよび戻り値として使用されます)。詳細は、第6章「Webサービスの呼出し」を参照してください。

  7. Main.javaファイルをメイン・プロジェクト・ディレクトリのsrc/examples/webservices/simple_clientサブディレクトリに保存します。

  8. コマンド・ラインでbuild-clientターゲットを指定して、clientgenおよびjavac Antタスクを実行します。

    prompt> ant build-client
    

    output/clientclassディレクトリを見て、clientgen Antタスクによって生成されたファイルとアーティファクトを確認します。

  9. 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"/>
           <arg line="http://${wls.hostname}:${wls.port}/complex/ComplexService" />
         </java>
      </target>
    

    runターゲットはMainアプリケーションを呼び出し、デプロイされたWebサービスのWSDL URLを1つの引数として渡します。classpath要素では、<path>タスクで作成される参照を使用して、CLASSPATHにclientclassディレクトリが追加されます。

  10. runターゲットを実行してechoComplexType操作を呼び出します。

        prompt> ant run
    

    呼出しに成功すると、次のような最終出力が表示されます。

    run:
          [java] echoComplexType called. Result: 999, Hello Struct
    

build.xmlファイルのbuild-clientおよびrunターゲットを使用して、開発プロセスの一環としてJavaクライアント・アプリケーションを繰返し更新、再ビルド、および実行できます。

サンプルJavaクライアント・アプリケーション

以下に、echoComplexType操作を呼び出す簡単なJavaクライアント・アプリケーションを示します。

package examples.webservices.simple_client;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
// import the BasicStruct class, used as a param and return value of the
 // echoComplexType operation. The class is generated automatically by
 // the clientgen Ant task.
import examples.webservices.complex.BasicStruct;
/**
  * This is a simple Java client application that invokes the
  * echoComplexType operation of the ComplexService Web service.
  */
public class Main {
  public static void main(String[] args)
       throws ServiceException, RemoteException { 
    ComplexService service = new ComplexService_Impl (args[0] + "?WSDL" );
     ComplexPortType port = service.getComplexServicePort();
    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="JAXRPC"
      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"/>
    <arg line="http://${wls.hostname}:${wls.port}/complex/ComplexService" />
     </java>
   </target>
</project>

WebLogic WebサービスからのWebサービスの呼出し

デプロイ済のWebLogic Webサービス内からWebサービス(WebLogic、.NETなど)を呼び出すこともできます。

WebサービスをWebLogic Web サービスから呼び出すための手順は、「Java SEクライアントからのWebサービスの呼出し」で説明した手順と似ていますが、この場合はclientgen Antタスクを実行してクライアント・スタブを生成するのではなく、jwsc Antタスク内で<jws>の子要素<clientgen>を使用します。jwsc Antタスクでは、生成されたクライアント・スタブが自動的に呼出し側のWebサービスのWARファイルにパッケージ化されるため、即座にWebサービスからアクセスできるようになります。その後で、標準JAX-RPCのプログラミング・ガイドラインに従って、他のWebサービスを呼び出すWebサービスを実装するJWSファイルを作成します。

次の例では、「ユーザー定義のデータ型を使用するWebサービスの作成」で説明したComplexService WebサービスのechoComplexType操作を呼び出すJWSファイルを記述する方法を示します。


注意:

ComplexService Webサービスは正常にデプロイ済であると想定しています。

  1. WebLogic Server環境を設定します。

    コマンド・ウィンドウを開いて、ドメイン・ディレクトリのbinサブディレクトリにあるsetDomainEnv.cmd (Windows)またはsetDomainEnv.sh (UNIX)スクリプトを実行します。WebLogic Serverドメインのデフォルトの場所は、MW_HOME/user_projects/domains/domainNameです。MW_HOMEはOracle製品の最上位のインストール・ディレクトリ、domainNameはドメインの名前です。

  2. プロジェクト・ディレクトリを作成します。

       prompt> mkdir /myExamples/service_to_service
    
  3. プロジェクト・ディレクトリの下に、srcディレクトリと、JWSおよびクライアント・アプリケーション・ファイル(この手順で後述)のパッケージ名に相当するサブディレクトリを作成します。

       prompt> cd /myExamples/service_to_service
        prompt> mkdir src/examples/webservices/service_to_service
    
  4. ComplexService Webサービスを呼び出すWebサービスを実装するJWSファイルを作成します。

    任意のJava IDEまたはテキスト・エディタを開いて、「サンプルClientServiceImpl.java JWSファイル」で指定されたJavaコードを使用してClientServiceImpl.javaというJavaファイルを作成します。

    サンプルJWSファイルでは、1つのパブリック・メソッドcallComplexService()を含むClientServiceImplというJavaクラスが示されています。このJavaクラスでは、後でjwsc Antタスクで生成されるJAX-RPCスタブと、BasicStruct Java Bean(同じくclientgenで生成されるもので、ComplexService WebサービスのechoComplexType操作のパラメータと戻り値のデータ型)をインポートします。

    ClientServiceImpl Javaクラスでは1つのメソッドcallComplexService()を定義しており、このメソッドはComplexService WebサービスのechoComplexType操作に渡されるBasicStructと、ComplexService WebサービスのURLという2つのパラメータを取ります。メソッドは標準のJAX-RPC APIを使用し、jwscによって生成されたスタブを使用してComplexServiceServicePortTypeを取得した後、echoComplexType操作を呼び出します。

  5. ClientServiceImpl.javaファイルをsrc/examples/webservices/service_to_serviceディレクトリに保存します。

  6. プロジェクト・ディレクトリ内に標準のAnt build.xmlファイルを作成して、次のタスクを追加します。

    <project name="webservices-service_to_service" default="all">
      <taskdef name="jwsc"
         classname="weblogic.wsee.tools.anttasks.JwscTask" />
    </project>
    

    taskdefタスクでは、jwsc Antタスクの完全なクラス名を定義しています。

    この手順で説明される追加のターゲット(cleandeployundeployclientrunなど)が含まれている、完全なサンプルbuild.xmlファイルについては、「ClientServiceをビルドするためのサンプルAntビルド・ファイル」を参照してください。完全なbuild.xmlファイルでは、EARディレクトリの名前を常にハード・コード化して使用するのではなく、${ear-dir}などのプロパティも使用しています。

  7. 次のような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="JAXRPC">
                <WLHttpTransport
                  contextPath="ClientService" serviceUri="ClientService"
                  portName="ClientServicePort"/>
         <clientgen
            type="JAXRPC"
     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;
    

    パッケージ名がクライアント・アプリケーションと同じパッケージ名に設定されている場合、インポートの呼出しは省略できます。

  8. コマンド・ラインでbuild-serviceターゲットを指定して、jwsc Antタスクを実行します。

    prompt> ant build-service
    
  9. WebサービスをデプロイするWebLogic Serverインスタンスを起動します。

  10. 管理コンソールまたは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.usernamewls.passwordwls.hostnamewls.port、およびwls.server.nameの値は、ご使用のWebLogic Serverインスタンスの対応する値で置き換えます。

    deployターゲットを実行して、WARファイルをデプロイします。

      prompt> ant deploy
    
  11. ブラウザでWSDLを呼び出して、Webサービスが正しくデプロイされていることをテストします。

    http://host:port/ClientService/ClientService?WSDL
    

Webサービスを呼び出すJavaクライアント・アプリケーションの作成例については、「Java SEクライアントからのWebサービスの呼出し」を参照してください。

サンプルClientServiceImpl.java JWSファイル

echoComplexType操作を呼び出す簡単なWebサービス・クライアント・アプリケーションを次に示します。

package examples.webservices.service_to_service;
import java.rmi.RemoteException;
 import javax.xml.rpc.ServiceException;
import javax.jws.WebService;
 import javax.jws.WebMethod;
import weblogic.jws.WLHttpTransport;
// Import the BasicStruct data type, generated by clientgen and used
 // by the ComplexService Web Service
import examples.webservices.complex.BasicStruct;
// Import the JAX-RPC Stubs for invoking the ComplexService Web Service.
 // Stubs generated by clientgen
import examples.webservices.service_to_service.ComplexPortType;
 import examples.webservices.service_to_service.ComplexService_Impl;
 import examples.webservices.service_to_service.ComplexService;
@WebService(name="ClientPortType", serviceName="ClientService",
             targetNamespace="http://examples.org")
@WLHttpTransport(contextPath="ClientService", serviceUri="ClientService",
                  portName="ClientServicePort")
public class ClientServiceImpl {
  @WebMethod()
   public String callComplexService(BasicStruct input, String serviceUrl) 
       throws ServiceException, RemoteException
   {
    // Create service and port stubs to invoke ComplexService
     ComplexService service = new ComplexService_Impl(serviceUrl + "?WSDL");
     ComplexPortType port = service.getComplexServicePort();
    // 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="JAXRPC">
         <WLHttpTransport
           contextPath="ClientService" serviceUri="ClientService"
           portName="ClientServicePort"/>
         <clientgen
 type="JAXRPC"
            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="JAXRPC"/>
    <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"/>
           <arg
             line="http://${wls.hostname}:${wls.port}/ClientService/ClientService"/>
    </java>
  </target>
</project>