生成される WSDL2Apex コード
WSDL2Apex ツールを使用して WSDL ドキュメントから Apex クラスを生成できます。WSDL2Apex ツールは、オープンソースのツールで、GitHub から入手できます。
WSDL2Apex のソースコードは、GitHub の WSDL2Apex リポジトリにあります。貢献することもできます。
次の例では、WSDL ドキュメントから Apex クラスがどのように作成されるかを示します。Apex クラスは、WSDL をインポートすると自動生成されます。
次のコードは、サンプル WSDL ドキュメントを示します。
1<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
2xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
3xmlns:s="http://www.w3.org/2001/XMLSchema"
4xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
5xmlns:tns="http://doc.sample.com/docSample"
6targetNamespace="http://doc.sample.com/docSample"
7xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
8
9<!-- Above, the schema targetNamespace maps to the Apex class name. -->
10
11
12<!-- Below, the type definitions for the parameters are listed.
13 Each complexType and simpleType parameteris mapped to an Apex class inside the parent class for the WSDL. Then, each element in the complexType is mapped to a public field inside the class. -->
14
15<wsdl:types>
16<s:schema elementFormDefault="qualified"
17targetNamespace="http://doc.sample.com/docSample">
18<s:element name="EchoString">
19<s:complexType>
20<s:sequence>
21<s:element minOccurs="0" maxOccurs="1" name="input" type="s:string" />
22</s:sequence>
23</s:complexType>
24</s:element>
25<s:element name="EchoStringResponse">
26<s:complexType>
27<s:sequence>
28<s:element minOccurs="0" maxOccurs="1" name="EchoStringResult"
29type="s:string" />
30</s:sequence>
31</s:complexType>
32</s:element>
33</s:schema>
34</wsdl:types>
35
36<!--The stub below defines operations. -->
37
38<wsdl:message name="EchoStringSoapIn">
39<wsdl:part name="parameters" element="tns:EchoString" />
40</wsdl:message>
41<wsdl:message name="EchoStringSoapOut">
42<wsdl:part name="parameters" element="tns:EchoStringResponse" />
43</wsdl:message>
44<wsdl:portType name="DocSamplePortType">
45<wsdl:operation name="EchoString">
46<wsdl:input message="tns:EchoStringSoapIn" />
47<wsdl:output message="tns:EchoStringSoapOut" />
48</wsdl:operation>
49</wsdl:portType>
50
51<!--The code below defines how the types map to SOAP. -->
52
53<wsdl:binding name="DocSampleBinding" type="tns:DocSamplePortType">
54<wsdl:operation name="EchoString">
55<soap:operation soapAction="urn:dotnet.callouttest.soap.sforce.com/EchoString"
56style="document" />
57<wsdl:input>
58<soap:body use="literal" />
59</wsdl:input>
60<wsdl:output>
61<soap:body use="literal" />
62</wsdl:output>
63</wsdl:operation>
64</wsdl:binding>
65
66<!-- Finally, the code below defines the endpoint, which maps to the endpoint in the class -->
67
68<wsdl:service name="DocSample">
69<wsdl:port name="DocSamplePort" binding="tns:DocSampleBinding">
70<soap:address location="http://YourServer/YourService" />
71</wsdl:port>
72</wsdl:service>
73</wsdl:definitions>この WSDL ドキュメントから、次の Apex クラスが自動生成されます。クラス名 docSample は、WSDL をインポートしたときに指定した名前です。
1//Generated by wsdl2apex
2
3public class docSample {
4 public class EchoStringResponse_element {
5 public String EchoStringResult;
6 private String[] EchoStringResult_type_info = new String[]{
7 'EchoStringResult',
8 'http://doc.sample.com/docSample',
9 null,'0','1','false'};
10 private String[] apex_schema_type_info = new String[]{
11 'http://doc.sample.com/docSample',
12 'true','false'};
13 private String[] field_order_type_info = new String[]{
14 'EchoStringResult'};
15 }
16 public class EchoString_element {
17 public String input;
18 private String[] input_type_info = new String[]{
19 'input',
20 'http://doc.sample.com/docSample',
21 null,'0','1','false'};
22 private String[] apex_schema_type_info = new String[]{
23 'http://doc.sample.com/docSample',
24 'true','false'};
25 private String[] field_order_type_info = new String[]{'input'};
26 }
27 public class DocSamplePort {
28 public String endpoint_x = 'http://YourServer/YourService';
29 public Map<String,String> inputHttpHeaders_x;
30 public Map<String,String> outputHttpHeaders_x;
31 public String clientCertName_x;
32 public String clientCert_x;
33 public String clientCertPasswd_x;
34 public Integer timeout_x;
35 private String[] ns_map_type_info = new String[]{
36 'http://doc.sample.com/docSample', 'docSample'};
37 public String EchoString(String input) {
38 docSample.EchoString_element request_x = new
39 docSample.EchoString_element();
40 request_x.input = input;
41 docSample.EchoStringResponse_element response_x;
42 Map<String, docSample.EchoStringResponse_element> response_map_x =
43 new Map<String, docSample.EchoStringResponse_element>();
44 response_map_x.put('response_x', response_x);
45 WebServiceCallout.invoke(
46 this,
47 request_x,
48 response_map_x,
49 new String[]{endpoint_x,
50 'urn:dotnet.callouttest.soap.sforce.com/EchoString',
51 'http://doc.sample.com/docSample',
52 'EchoString',
53 'http://doc.sample.com/docSample',
54 'EchoStringResponse',
55 'docSample.EchoStringResponse_element'}
56 );
57 response_x = response_map_x.get('response_x');
58 return response_x.EchoStringResult;
59 }
60 }
61}元の WSDL ドキュメントからの次の対応付けに注意してください。
- WSDL 対象名前空間は Apex クラス名に対応付けされます。
- 複雑なデータ型はクラスになります。データ型の各要素は、クラスの公開項目です。
- WSDL ポート名はスタブクラスに対応付けます。
- WSDL の各操作は、公開メソッドに対応付けます。
自動生成された docSample クラスを使用して外部 Web サービスを呼び出すことができます。次のコードは、外部サーバ上の echoString メソッドをコールします。
1docSample.DocSamplePort stub = new docSample.DocSamplePort();
2String input = 'This is the input string';
3String output = stub.EchoString(input);