検索

13.4. JAX-WS Web サービスクライアント

download PDF

13.4.1. JAX-WS Web サービスの使用とアクセス

手動または JAX-WS アノテーションを使用して Web サービスエンドポイントを作成した後には、 WSDL にアクセスして、Web サービスと通信を行う基本的なクライアントアプリケーションを作成することができます。公開されている WSDL からの Java コード生成プロセスは、Web サービスの消費と呼ばれます。これ は 2 段階で実行されます。
  1. クライアントアーティファクトの作成
  2. サービススタブの構築
  3. エンドポイントへのアクセス
クライアントアーティファクトの作成

クライアントアーティファクトを作成する前に、WSDL コントラクトを作成しておく必要があります。以下の WSDL コントラクトは、以降、本トピックで例として使用します。

例13.16 WSDL コントラクトの例


<definitions
    name='ProfileMgmtService'
    targetNamespace='http://org.jboss.ws/samples/retail/profile'
    xmlns='http://schemas.xmlsoap.org/wsdl/'
    xmlns:ns1='http://org.jboss.ws/samples/retail'
    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
    xmlns:tns='http://org.jboss.ws/samples/retail/profile'
    xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
 
   <types>
 
      <xs:schema targetNamespace='http://org.jboss.ws/samples/retail'
                 version='1.0' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
         <xs:complexType name='customer'>
            <xs:sequence>
               <xs:element minOccurs='0' name='creditCardDetails' type='xs:string'/>
               <xs:element minOccurs='0' name='firstName' type='xs:string'/>
               <xs:element minOccurs='0' name='lastName' type='xs:string'/>
            </xs:sequence>
         </xs:complexType>
      </xs:schema>
 
      <xs:schema
          targetNamespace='http://org.jboss.ws/samples/retail/profile'
          version='1.0'
          xmlns:ns1='http://org.jboss.ws/samples/retail'
          xmlns:tns='http://org.jboss.ws/samples/retail/profile'
          xmlns:xs='http://www.w3.org/2001/XMLSchema'>
 
         <xs:import namespace='http://org.jboss.ws/samples/retail'/>
         <xs:element name='getCustomerDiscount'
                     nillable='true' type='tns:discountRequest'/>
         <xs:element name='getCustomerDiscountResponse'
                     nillable='true' type='tns:discountResponse'/>
         <xs:complexType name='discountRequest'>
            <xs:sequence>
               <xs:element minOccurs='0' name='customer' type='ns1:customer'/>
 
            </xs:sequence>
         </xs:complexType>
         <xs:complexType name='discountResponse'>
            <xs:sequence>
               <xs:element minOccurs='0' name='customer' type='ns1:customer'/>
               <xs:element name='discount' type='xs:double'/>
            </xs:sequence>
         </xs:complexType>
      </xs:schema>
 
   </types>
 
   <message name='ProfileMgmt_getCustomerDiscount'>
      <part element='tns:getCustomerDiscount' name='getCustomerDiscount'/>
   </message>
   <message name='ProfileMgmt_getCustomerDiscountResponse'>
      <part element='tns:getCustomerDiscountResponse'
            name='getCustomerDiscountResponse'/>
   </message>
   <portType name='ProfileMgmt'>
      <operation name='getCustomerDiscount'
                 parameterOrder='getCustomerDiscount'>
 
         <input message='tns:ProfileMgmt_getCustomerDiscount'/>
         <output message='tns:ProfileMgmt_getCustomerDiscountResponse'/>
      </operation>
   </portType>
   <binding name='ProfileMgmtBinding' type='tns:ProfileMgmt'>
      <soap:binding style='document'
                    transport='http://schemas.xmlsoap.org/soap/http'/>
      <operation name='getCustomerDiscount'>
         <soap:operation soapAction=''/>
         <input>
 
            <soap:body use='literal'/>
         </input>
         <output>
            <soap:body use='literal'/>
         </output>
      </operation>
   </binding>
   <service name='ProfileMgmtService'>
      <port binding='tns:ProfileMgmtBinding' name='ProfileMgmtPort'>
 
         <soap:address
             location='SERVER:PORT/jaxws-samples-retail/ProfileMgmtBean'/>
      </port>
   </service>
</definitions>	
	

注記

JAX-WS アノテーションを使用して Web サービスエンドポイントを作成した場合には、WSDL コントラクトは自動的に生成されるので、その URL のみが必要となります。この URL は、エンドポイントがデプロイされた後に、Web ベース管理コンソールの Runtime セクションの Web セクションから取得することができます。
wsconsume.sh または wsconsume.bat ツールを使用して抽象コントラクト (WSDL) を消費し、アノテーションが付いた Java クラスとそれを定義するオプションのソースを作成します。コマンドは、JBoss Enterprise Application Platform インストールの EAP_HOME/bin/ ディレクトリにあります。

例13.17 wsconsume.sh コマンドの構文

[user@host bin]$ ./wsconsume.sh --help
WSConsumeTask is a cmd line tool that generates portable JAX-WS artifacts from a WSDL file.

usage: org.jboss.ws.tools.cmd.WSConsume [options] <wsdl-url>

options: 
    -h, --help                  Show this help message
    -b, --binding=<file>        One or more JAX-WS or JAXB binding files 
    -k, --keep                  Keep/Generate Java source
    -c  --catalog=<file>        Oasis XML Catalog file for entity resolution
    -p  --package=<name>        The target package for generated source
    -w  --wsdlLocation=<loc>    Value to use for @WebService.wsdlLocation
    -o, --output=<directory>    The directory to put generated artifacts
    -s, --source=<directory>    The directory to put Java source
    -t, --target=<2.0|2.1|2.2>  The JAX-WS specification target
    -q, --quiet                 Be somewhat more quiet
    -v, --verbose               Show full exception stack traces
    -l, --load-consumer         Load the consumer and exit (debug utility)
    -e, --extension             Enable SOAP 1.2 binding extension
    -a, --additionalHeaders     Enable processing of implicit SOAP headers
    -n, --nocompile             Do not compile generated sources
   

以下のコマンドは、ProfileMgmtService.wsdl ファイルからソース .java ファイルを生成して出力に一覧表示します。ソースには、パッケージのディレクトリ構造が使用されます。これは、-p スイッチで指定します。
[user@host bin]$  wsconsume.sh -k -p org.jboss.test.ws.jaxws.samples.retail.profile ProfileMgmtService.wsdl
output/org/jboss/test/ws/jaxws/samples/retail/profile/Customer.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/DiscountRequest.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/DiscountResponse.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/ObjectFactory.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/ProfileMgmt.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/ProfileMgmtService.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/package-info.java
output/org/jboss/test/ws/jaxws/samples/retail/profile/Customer.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/DiscountRequest.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/DiscountResponse.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/ObjectFactory.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/ProfileMgmt.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/ProfileMgmtService.class
output/org/jboss/test/ws/jaxws/samples/retail/profile/package-info.class
.java ソースファイルとコンパイル済み .class ファイルの両方は、コマンドを実行するディレクトリー内の output/ ディレクトリーに生成されます。
表13.2 wsconsume.sh によって作成されたアーティファクトの説明
ファイル 説明
ProfileMgmt.java
サービスエンドポイントインターフェース。
Customer.java
カスタムデータ型。
Discount*.java
カスタムデータ型。
ObjectFactory.java
JAXB XML レジストリー。
package-info.java
JAXB パッケージアノテーション。
ProfileMgmtService.java
サービスファクトリー。
wsconsume.sh コマンドは、全カスタムデータタイプ (JAXB アノテーション付きクラス)、サービスエンドポイントインターフェース、およびサービスファクトリクラスを生成します。これらのアーティファクトは、Web サービスクライアント実装の構築に使用されます。
サービススタブの構築

Web サービスクライアントは、サービススタブを使用してリモート Web サービス呼び出しの詳細情報を抽象化します。クライアントアプリケーション側には、WS 呼び出しはその他のビジネスコンポーネントと同じように見えます。この場合、サービスエンドポイントインターフェースはビジネスインターフェースとして機能し、サービススタブとして構築する際にサービスファクトリクラスは使用されません。

例13.18 サービススタブの構築とエンドポイントへのアクセス

以下の例では、まず最初に WSDL ロケーションとサービス名を使用してサービスファクトリを作成し、次に wsconsume.sh コマンドで作成されたサービスエンドポイントインターフェースを使用して サービススタブを構築します。最終的にこのスタブはその他のビジネスインターフェースと同じように使用することができます。
エンドポイントの WSDL URL は、Web ベースの管理コンソールで確認することができます。画面の左上にある Runtime メニュー項目を選択し、画面左下の Deployments メニュー項目を選びます。Webservices をクリックして対象のデプロイメントを選択すると詳細が表示されます。

import javax.xml.ws.Service;
[...]
Service service = Service.create(                                 
new URL("http://example.org/service?wsdl"),
new QName("MyService")
);
ProfileMgmt profileMgmt = service.getPort(ProfileMgmt.class);     
 
// Use the service stub in your application
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.