必要なもの
(1) WSDLファイル : ここでは「HelloWorld.wsdl」 (2) サーバ側のプロジェクト : ここでは「SampleService」 (3) クライアント側のプロジェクト : ここでは「SampleJavaCodes」
サンプル
(1) WSDLファイル : HelloWorld.wsdl
* 「MissingNameException」を定義<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://helloworld.webservice.moodykettle.com" xmlns:intf="http://helloworld.webservice.moodykettle.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="">http://helloworld.webservice.moodykettle.com"> <wsdl:types> <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="">http://helloworld.webservice.moodykettle.com"> <element name="sayHello"> <complexType> <sequence> <element nillable="true" name="name" type="xsd:string" /> </sequence> </complexType> </element> <element name="sayHelloResponse"> <complexType> <sequence> <element name="sayHelloReturn" type="xsd:string" /> </sequence> </complexType> </element> <!-- ★例外★ --> <element name="MissingNameException"> <complexType> <sequence> <element name="MissingNameException" type="xsd:string" /> </sequence> </complexType> </element> </schema> </wsdl:types> <wsdl:message name="sayHelloResponse"> <wsdl:part element="impl:sayHelloResponse" name="parameters" /> </wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part element="impl:sayHello" name="parameters" /> </wsdl:message> <!-- ★例外★ --> <wsdl:message name="MissingNameException"> <wsdl:part element="impl:MissingNameException" name="fault" /> </wsdl:message> <wsdl:portType name="HelloWorld"> <wsdl:operation name="sayHello"> <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" /> <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" /> <!-- ★例外★ --> <wsdl:fault message="impl:MissingNameException" name="MissingNameException" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction="" /> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body use="literal" /> </wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> <!-- ★例外★ --> <wsdl:fault name="MissingNameException"> <wsdlsoap:fault name="MissingNameException" use="literal" /> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldService"> <wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld"> <wsdlsoap:address location="http://localhost:8080/WebService/services/HelloWorld" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
(2) サーバ側のプロジェクト : SampleService
http://blogs.yahoo.co.jp/dk521123/34448685.htmlで、上記の「HelloWorld.wsdl」からサーバ側のソースを自動生成する。
(2-1) services.xml
* 「resources」の中できるので以下に置く「/SampleService/WebContent/WEB-INF/services/HelloWorldServiceMessageReceiverInOut/META-INF」
<?xml version="1.0" encoding="UTF-8"?> <!-- This file was auto-generated from WSDL --> <!-- by the Apache Axis2 version: 1.6.2 Built on : Apr 17, 2012 (05:33:49 IST) --> <serviceGroup> <service name="HelloWorldService"> <messageReceivers> <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="com.sample.service.HelloWorldServiceMessageReceiverInOut"/> </messageReceivers> <parameter name="ServiceClass">com.sample.service.HelloWorldServiceSkeleton</parameter> <parameter name="useOriginalwsdl">true</parameter> <parameter name="modifyUserWSDLPortAddress">true</parameter> <operation name="sayHello" mep="http://www.w3.org/ns/wsdl/in-out" namespace="">http://helloworld.webservice.moodykettle.com"> <actionMapping>http://helloworld.webservice.moodykettle.com/HelloWorld/sayHelloRequest <outputActionMapping>http://helloworld.webservice.moodykettle.com/HelloWorld/sayHelloResponse <faultActionMapping faultName="MissingNameException">http://helloworld.webservice.moodykettle.com/HelloWorld/sayHello/Fault/MissingNameException <faultActionMapping faultName="MissingNameException_Exception">http://helloworld.webservice.moodykettle.com/HelloWorld/sayHello/Fault/MissingNameException </operation> </service> </serviceGroup>
(2-2) HelloWorldServiceSkeleton.java
/** * HelloWorldServiceSkeleton.java. * This file was auto-generated from WSDL * by the Apache Axis2 version: 1.6.2 Built on : Apr 17, 2012 (05:33:49 IST) */ package com.sample.service; /** * HelloWorldServiceSkeleton java skeleton for the axisService. */ public class HelloWorldServiceSkeleton implements HelloWorldServiceSkeletonInterface { /** * Auto generated method signature. * * @param sayHello0 * @return sayHelloResponse1 * @throws MissingNameExceptionException */ public com.sample.service.SayHelloResponse sayHello( com.sample.service.SayHello sayHello0) throws MissingNameExceptionException { // ★自分で実装。ここから★ String name = sayHello0.getName(); if (name == null || name == "") { // 例外作成 MissingNameExceptionException fault = new MissingNameExceptionException("No name"); MissingNameException ex = new MissingNameException(); ex.setMissingNameException("Missing name"); fault.setFaultMessage(ex); throw fault; } SayHelloResponse response = new SayHelloResponse(); response.setSayHelloReturn("Hello, " + name + " !!"); return response; // ★自分で実装。ここまで★ } }
(3) クライアント側のプロジェクト : SampleJavaCodes
http://blogs.yahoo.co.jp/dk521123/34445295.htmlで、上記の「HelloWorld.wsdl」からクライアント側のソースを自動生成する。
(3-1) スタブを呼び出すクラス : Main.java
* 自分で作成package com.sample.client; public class Main { public static void main(String[] args) { try { HelloWorldServiceStub stub = new HelloWorldServiceStub("http://localhost:8080/SampleService/services/HelloWorldService"); SayHello request = new SayHello(); // 空の名前を設定 request.setName(""); SayHelloResponse response = stub.sayHello(request); System.out.println("Response : " + response.getSayHelloReturn()); } catch (MissingNameExceptionException ex) { System.out.println("★Catch MissingNameExceptionException!★"); System.out.println(" "); ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } }
出力結果
★Catch MissingNameExceptionException!★ com.sample.client.MissingNameExceptionException: No name at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at com.sample.client.HelloWorldServiceStub.sayHello(HelloWorldServiceStub.java:214) at com.sample.client.Main.main(Main.java:13)