JBoss Web Serviceを使用することで、JAX-WSのWebサービスが簡単に作成出来る。
例として、以下のような簡単なクラスを作成し、helloメソッドをWebサービスとして公開する。
package sample.webservice; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloService { @WebMethod public String hello(String name) { return "Hello " + name + "!"; } }
Webサービスとするクラスに、@WebServiceアノテーション、
公開するメソッドに、@WebMethodアノテーションをつけておく。
web.xmlには通常のサーブレットと同様にマッピングを行う。
<servlet> <servlet-name>HelloService</servlet-name> <servlet-class>sample.webservice.HelloService</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloService</servlet-name> <url-pattern>/services/HelloService</url-pattern> </servlet-mapping>
ここまでで、準備は完了。
JBossにデプロイを行う。
WSDLにアクセスを行なってみる。
http://localhost:8080/WebSample/services/HelloService?WSDL
以下のようなWSDLが返ってくる。
<definitions name='HelloServiceService' targetNamespace='http://webservice.sample/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://webservice.sample/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <types> <xs:schema targetNamespace='http://webservice.sample/' version='1.0' xmlns:tns='http://webservice.sample/' xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:element name='hello' type='tns:hello'/> <xs:element name='helloResponse' type='tns:helloResponse'/> <xs:complexType name='hello'> <xs:sequence> <xs:element minOccurs='0' name='arg0' type='xs:string'/> </xs:sequence> </xs:complexType> <xs:complexType name='helloResponse'> <xs:sequence> <xs:element minOccurs='0' name='return' type='xs:string'/> </xs:sequence> </xs:complexType> </xs:schema> </types> <message name='HelloService_hello'> <part element='tns:hello' name='hello'></part> </message> <message name='HelloService_helloResponse'> <part element='tns:helloResponse' name='helloResponse'></part> </message> <portType name='HelloService'> <operation name='hello' parameterOrder='hello'> <input message='tns:HelloService_hello'></input> <output message='tns:HelloService_helloResponse'></output> </operation> </portType> <binding name='HelloServiceBinding' type='tns:HelloService'> <soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/> <operation name='hello'> <soap:operation soapAction=''/> <input> <soap:body use='literal'/> </input> <output> <soap:body use='literal'/> </output> </operation> </binding> <service name='HelloServiceService'> <port binding='tns:HelloServiceBinding' name='HelloServicePort'> <soap:address location='http://localhost:8080/WebSample/services/HelloService'/> </port> </service> </definitions>
この例では、自分でアノテーションを設定したが、GUIでも可能である。
メニューから「新規」ー「その他」を選択し、
「JBoss ツール」ー「Simple Web Service」を選択する。
- Technology: JAX-WS
- 動的Webプロジェクト: このプロジェクトを選択
- サービス名: 公開するサービス名
- Update web.xml: チェック
- Service implemention: 公開するクラスを選択
この場合、web.xmlまで自動で作成してくれる。
ただし、既存のクラスをそのまま公開されるのではなく
「org.jboss.sample.webservice.HelloService」というクラスが新たに作成される。
メニューで選択した通り、既存のクラスを元にWebサービスを新規に作ったことになる。
Apache Axisを使用したWeb Serviceよりかなりスッキリした感じで作成することが出来た。