JAX-WS クライアントのデプロイメント記述子の作成

デプロイメント記述子 は標準のテキスト・ファイルであり、XML を使用してフォーマットされており、Web サービス・アプリケーション内にパッケージ化されています。オプションで、Web Services for Java™ Platform、Enterprise Edition (Java EE) 仕様 (JSR 109) サービス参照デプロイメント記述子を使用して、Java API for XML-Based Web Services (JAX-WS) Web サービス・クライアント内のアノテーションで指定されるアプリケーション・メタデータの拡張またはオーバーライドを行うことができます。

始める前に

まず、wsimport コマンドを使用して、Web サービス記述言語 (WSDL) ファイルから Web サービス・クライアントの成果物を生成する必要があります。

このタスクについて

service-ref エントリーを application-client.xmlweb.xml、または ejb-jar.xml の各 Java EE デプロイメント記述子内に追加することができます。service-ref エントリーは、Web の Java EE コンポーネント、Enterprise JavaBeans (EJB)、またはアプリケーション・クライアント・コンテナーによって使用される Web サービスへの参照を表しています。service-ref エントリーには、サービスの検索に使用される JNDI 名があります。 service-ref エントリーを指定すると、クライアント・アプリケーションは JNDI 検索を使用してサービスを見つけることができるようになります。また、ユーザーは、リソース注入にこれらのサービス参照を使用することもできます。

デプロイメント記述子の 1 つによって検出された各 service-ref エントリーごとに、対応するサービス・オブジェクトが JNDI 名前空間にバインドされ、ポート情報が指定されている場合はポート情報が組み込まれます。 これで、JAX-WS クライアントは JNDI 検索を行って JAX-WS サービスまたはポート・インスタンスを取得できるようになります。

JAX-WS サービスを表す service-ref を定義する場合は、 wsimport ツールで生成される javax.xml.ws.Service サブクラスを service-interface 値として使用します。これは、@WebServiceClient アノテーションを含むクラスです。JAX-WS ポートを表す service-ref を定義する場合は、 service-interface 値は wsimport ツールで生成される javax.xml.ws.Service サブクラスのままです。service-ref-type 値は、ポートで使用されるサービス・エンドポイント・インターフェース (SEI) のクラスを示します。SEI クラスは wsimport によっても 生成され、@WebService アノテーションを使用してアノテーションが付けられます。

手順

  1. service-ref エントリーを、application-client.xmlweb.xml、または ejb-jar.xml の各デプロイメント記述子に定義します。
    例えば、Web archive (WAR) ファイルに、次の service-ref エントリーを含む WEB-INF/web.xml デプロイメント記述子が含まれているとします。
    <service-ref>
      <service-ref-name>service/ExampleService</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
    </service-ref>
    
    <service-ref>
      <service-ref-name>service/ExamplePort</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
      <service-ref-type>com.ibm.sample.ExamplePort</service-ref-type>
    </service-ref>
    
    <service-ref>
      <service-ref-name>service/ExamplePortInjected</service-ref-name>
      <service-interface>com.ibm.sample.ExampleService</service-interface>
      <service-ref-type>com.ibm.sample.ExamplePort</service-ref-type>
    
      <injection-target>
        <injection-target-class>com.ibm.sample.J2EEClient</injection-target-class>
        <injection-target-name>injectedPort</injection-target-name>
      </injection-target>
    </service-ref>
    この例では、com.ibm.sample.ExampleService は生成された JAX-WS サービス・クラスであり、このクラスは javax.xml.ws.Service のサブクラスである必要があります。 さらに、ExampleService.getPort() メソッドは com.ibm.sample.ExamplePort のインスタンスを戻します。
  2. Web サービス・クライアント・アプリケーション内のデプロイメント記述子を使用し、アプリケーションをカスタマイズします。 以下のコード・フラグメントの例は、クライアント・アプリケーションが WAR モジュール内で service-ref エントリーをどのように使用できるかを示しています。
    import javax.xml.ws.Service;
    import com.ibm.sample.ExampleService;
    import com.ibm.sample.ExamplePort;
    
    // Create an InitialContext object for doing JNDI lookups.
    InitialContext ic = new InitialContext();
    
    // Client obtains an instance of the generic service class using JNDI.
    Service genericService =
    (Service) ic.lookup(“java:comp/env/service/ExampleService”);
    
    // Client obtains an instance of the generated service class using JNDI.
    ExampleService exampleService =
    (ExampleService) ic.lookup(“java:comp/env/service/ExampleService”);
    
    // Client obtains an instance of the port using JNDI.
    ExamplePort ExamplePort =
    (ExamplePort) ic.lookup(“java:comp/env/service/ExamplePort”);
    
    // The container injects an instance of ExamplePort based on the client deployment descriptor
    private ExamplePort injectedPort;

タスクの結果

これで、クライアント・アプリケーションのデプロイメント記述子に定義されたサービス参照を使用できるようになりました。 さらに、デプロイメント記述子を使用して、@WebServiceRef または @Resource アノテーションで指定された情報を拡張またはオーバーライドすることもできます。

<lookup-name> デプロイメント記述子エレメントは、Java EE 6 で新しく導入され、定義済みのサービス参照を間接的に参照する際に使用されます。<lookup-name> エレメントを使用する際に一緒に指定できるのは <service-ref-name> エレメントのみで、<service-ref> の他の子エレメントは定義できません。

以下の例は、WEB-INF/web.xml ファイル内の service-ref エントリーを示しています。この例では、最初の service-ref への間接参照を定義する 同じ web.xml ファイル内の service-ref エントリー および JAX-WS サービスへの参照を定義しています。

<service-ref>
  <service-ref-name>service/ExampleService</service-ref-name>
  <service-interface>com.ibm.sample.ExampleService</service-interface>
  <service-ref-type>com.ibm.sample.ExampleServicePortType</service-ref-type>
  <wsdl-file>WEB-INF/wsdl/ExampleService.wsdl</wsdl-file>
</service-ref>

<service-ref>
  <service-ref-name>service/ExampleService2</service-ref>
  <lookup-name>java:comp/env/service/ExampleService</lookup-name>
</service-ref>

上記の service-refWEB-INF/web.xml ファイル内に定義されていると仮定すると、クライアント・アプリケーションは java:comp/env/service/ExampleService2 という名前を使用して JNDI 検索を実行することができ、その結果は、最初の service-ref に定義されているように、WSDL 文書 WEB-INF/wsdl/ExampleService.wsdl に定義されている ExampleService サービスへの参照になります。

次のタスク

Web サービスの呼び出しに使用されるクライアント・アプリケーション・コードを作成して、クライアントの実装を完了します。


トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_jaxwsclientdd
ファイル名:twbs_jaxwsclientdd.html