JBossでWebサービスクライアントを作成する

mabushiisign

JBoss Web ServiceでJAX-WSのWebサービスを作ったのでクライアントを今度は作成する。
自身がテストする上でもクライアントの作成は必要である。
クライアントを作成するには、JBossに付属のwsconsumeコマンドを使用する。
このコマンドでクライアントソースをジェネレートしてくれる。

リファレンスは以下の通り。

>wsconsume --help
WSConsumeTask is a cmd line tool that generates portable JAX-WS artifacts from a WSDL file.
 
usage: org.jboss.wsf.spi.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>      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

ここでは、-k, -pを使用する。

>wsconsume -k -p sample.client http://localhost:8080/WebSample/services/HelloService?WSDL
parsing WSDL...
 
 
generating code...
 
sample\client\Hello.java
sample\client\HelloResponse.java
sample\client\HelloService.java
sample\client\HelloServiceService.java
sample\client\ObjectFactory.java
sample\client\package-info.java
 
compiling code...

これで、6つのJavaソースがジェネレートされた。
これをプロジェクトに取り込んで使用する。
実際にWebサービスにアクセス出来る場合は、上記の方法で良いが、WSDLのみを入手し開発する場合は、以下のように、-wでWSDLのURLを指定し、取り込むWSDLはファイルを指定する。

>wsconsume -k -p sample.client -w http://localhost:8080/WebSample/services/HelloService?WSDL HelloService.wsdl

さて、ここまでで、クライアントのひな形が出来たので実際に呼び出すコードを記述する。

1import sample.client.HelloService;
2import sample.client.HelloServiceService;
3 
4public class HelloClient {
5 
6    public static void main(String args[]) {
7 
8        if (args.length != 1)
9        {
10            System.err.println("usages: HelloClient <message>");
11            System.exit(1);
12        }
13 
14        HelloServiceService service = new HelloServiceService();
15        HelloService hello = service.getHelloServicePort();
16        System.out.println("Server said: " + hello.hello(args[0]));
17    }
18}

Webサービス名が「Service」で終わっていると、ServiceServiceという名前のクラスが出来てしまう。ちょっと不細工だ。
Webサービスを公開する時は、Serviceというのは付けない方がいいだろう。
この例では、mainメソッドにしているが、当然何でも良い。
サーブレットでリクエストを受けて、外部のWebサービスを呼び出すのでもいい。
また、実際に呼び出すWebサービスのURLは、HelloServiceServiceクラスに記述されている。
ローカル環境・開発環境・商用環境などでURLが変わる場合は外だしするように書き換えると良い。

1/**
2 * This class was generated by the JAX-WS RI.
3 * JAX-WS RI 2.1.3-b02-
4 * Generated source version: 2.0
5 * 
6 */
7@WebServiceClient(name = "HelloServiceService", targetNamespace = "http://webservice.sample/", wsdlLocation = "http://localhost:8080/WebSample/services/HelloService?WSDL")
8public class HelloServiceService
9    extends Service
10{
11 
12    private final static URL HELLOSERVICESERVICE_WSDL_LOCATION;
13    private final static Logger logger = Logger.getLogger(sample.client.HelloServiceService.class.getName());
14 
15    static {
16        URL url = null;
17        try {
18            URL baseUrl;
19            baseUrl = sample.client.HelloServiceService.class.getResource(".");
20            url = new URL(baseUrl, "http://localhost:8080/WebSample/services/HelloService?WSDL");
21        } catch (MalformedURLException e) {
22            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8080/WebSample/services/HelloService?WSDL', retrying as a local file");
23            logger.warning(e.getMessage());
24        }
25        HELLOSERVICESERVICE_WSDL_LOCATION = url;
26    }

JAX-WSの機能を利用すると、Webサービスの開発が簡単に出来て便利である。

ABOUT ME
mabushiisign
mabushiisign
B'zとdoaとミスチルと登山が好きな40代のプログラマです。 普段は、SIer企業で働いています。
記事URLをコピーしました