JAX-WS でさくっと作る Web サービス

JAX-WS でさくっと Web サービスを作ってみました。さくっと取り出せるように自分用にメモを残しておきます。

「サーバ側」

サーバ側からいきます。まずは、Webサービスの用のクラスを作成する。

・Test.java

package com.example.jaxws;

import javax.jws.*;

@WebService
public class Test {

  @WebMethod
  public String hello(String name) {
    return "hello " + name + " !!";
  }
}


コンパイルして、wegen を実行して Web サービスに必要なクラスを生成しておきます。

# javac com/example/jaxws/Test.java 
# wsgen -cp . com.example.jaxws.Test


次に、エンドポイントを公開するクラスを作成します。

・Server.java

package com.example.jaxws;

import javax.xml.ws.Endpoint;

public class Server {
  public static void main(String[] args) {
    Endpoint.publish("http://***.***.***.***:8888/test", new Test());
  }
}


上記クラスをコンパイルして実行します。

# javac -cp . com/example/jaxws/Server.java
# java -cp . com.example.jaxws.Server


すると http://***.***.***.***:8888/test?wsdl にアクセスすると、上記のコード例だと以下のような WSDL が表示されるはずです。

<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jaxws.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.example.com/" name="TestService">
<types>
<xsd:schema>
<xsd:import namespace="http://jaxws.example.com/" schemaLocation="http://***.***.***.***:8888/test?xsd=1"/>
</xsd:schema>
</types>
<message name="hello">
<part name="parameters" element="tns:hello"/>
</message>
<message name="helloResponse">
<part name="parameters" element="tns:helloResponse"/>
</message>
<portType name="Test">
<operation name="hello">
<input wsam:Action="http://jaxws.example.com/Test/helloRequest" message="tns:hello"/>
<output wsam:Action="http://jaxws.example.com/Test/helloResponse" message="tns:helloResponse"/>
</operation>
</portType>
<binding name="TestPortBinding" type="tns:Test">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="hello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="TestService">
<port name="TestPort" binding="tns:TestPortBinding">
<soap:address location="http://***.***.***.***:8888/test"/>
</port>
</service>
</definitions>


サーバ側は以上です。


「クライアント側」

次にクライアント側になります。まず、wsimport を使って WSDL から必要なクラスを生成します。

> C:\work\jaxws_client
> wsimport http://***.***.***.***:8888/test?wsdl


以下のようなクラスが生成されるはずです。

> >dir com\example\jaxws
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は 0659-09F6 です

 C:\work\jaxws_client\com\example\jaxws のディレクトリ

2016/06/19  21:19    <DIR>          .
2016/06/19  21:19    <DIR>          ..
2016/06/19  21:16               641 Hello.class
2016/06/19  21:16               739 HelloResponse.class
2016/06/19  21:16             1,585 ObjectFactory.class
2016/06/19  21:16               248 package-info.class
2016/06/19  21:16               907 Test.class
2016/06/19  21:16             2,245 TestService.class


Webサービスを呼び出すクライアント側コードを作成します。

・Client.java

package com.example.jaxws;

public class Client {
  public static void main(String[] args) {
    TestService ts = new TestService();
    Test t = ts.getTestPort();
	
    System.out.println(t.hello("hoge"));
  }
}


コンパイルして実行すると、無事「hoge hello !!」と表示されるはずです。

>javac -cp . com\example\jaxws\Client.java
>java -cp . com.example.jaxws.Client
hello hoge !!


簡単ですが、以上になります。

[環境情報]
・サーバ側
 CentOS7 / Java SE 8 Update 92
 Windows 10 / Java SE 8 Update 92