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
さて、ここまでで、クライアントのひな形が出来たので実際に呼び出すコードを記述する。
import sample.client.HelloService; import sample.client.HelloServiceService; public class HelloClient { public static void main(String args[]) { if (args.length != 1) { System.err.println("usages: HelloClient <message>"); System.exit(1); } HelloServiceService service = new HelloServiceService(); HelloService hello = service.getHelloServicePort(); System.out.println("Server said: " + hello.hello(args[0])); } }
Webサービス名が「Service」で終わっていると、ServiceServiceという名前のクラスが出来てしまう。ちょっと不細工だ。
Webサービスを公開する時は、Serviceというのは付けない方がいいだろう。
この例では、mainメソッドにしているが、当然何でも良い。
サーブレットでリクエストを受けて、外部のWebサービスを呼び出すのでもいい。
また、実際に呼び出すWebサービスのURLは、HelloServiceServiceクラスに記述されている。
ローカル環境・開発環境・商用環境などでURLが変わる場合は外だしするように書き換えると良い。
/** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.3-b02- * Generated source version: 2.0 * */ @WebServiceClient(name = "HelloServiceService", targetNamespace = "http://webservice.sample/", wsdlLocation = "http://localhost:8080/WebSample/services/HelloService?WSDL") public class HelloServiceService extends Service { private final static URL HELLOSERVICESERVICE_WSDL_LOCATION; private final static Logger logger = Logger.getLogger(sample.client.HelloServiceService.class.getName()); static { URL url = null; try { URL baseUrl; baseUrl = sample.client.HelloServiceService.class.getResource("."); url = new URL(baseUrl, "http://localhost:8080/WebSample/services/HelloService?WSDL"); } catch (MalformedURLException e) { logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8080/WebSample/services/HelloService?WSDL', retrying as a local file"); logger.warning(e.getMessage()); } HELLOSERVICESERVICE_WSDL_LOCATION = url; }
JAX-WSの機能を利用すると、Webサービスの開発が簡単に出来て便利である。