通过wsdl生成服务和客户端代码
通过wsdl生成服务和客户端代码
1. wsdl文件
D:\axis151\mysamples\sample1
axis2userguide.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://apache.org/axis2/Axis2UserGuide"
xmlns:intf="http://apache.org/axis2/Axis2UserGuide"
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://apache.org/axis2/Axis2UserGuide">
<wsdl:types>
<schema
elementFormDefault="qualified"
targetNamespace="http://apache.org/axis2/Axis2UserGuide"
xmlns="http://www.w3.org/2001/XMLSchema">
<!-- ELEMENTS -->
<element name="DoInOnlyRequest">
<complexType>
<sequence>
<element name="messageString" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="TwoWayOneParameterEchoRequest">
<complexType>
<sequence>
<element name="echoString" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="TwoWayOneParameterEchoResponse">
<complexType>
<sequence>
<element name="echoString" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="NoParametersRequest">
<complexType/>
</element>
<element name="NoParametersResponse">
<complexType/>
</element>
<element name="MultipleParametersAddItemRequest">
<complexType>
<sequence>
<element name="itemId" type="xsd:int"/>
<element name="itemName" type="xsd:string"/>
<element name="price" type="xsd:float"/>
<element name="description" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="MultipleParametersAddItemResponse">
<complexType>
<sequence>
<element name="itemId" type="xsd:int"/>
<element name="successfulAdd" type="xsd:boolean"/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<!-- MESSAGES -->
<wsdl:message name="DoInOnlyRequestMessage">
<wsdl:part name="input" element="impl:DoInOnlyRequest"/>
</wsdl:message>
<wsdl:message name="TwoWayOneParameterEchoRequestMessage">
<wsdl:part name="input" element="impl:TwoWayOneParameterEchoRequest"/>
</wsdl:message>
<wsdl:message name="TwoWayOneParameterEchoResponseMessage">
<wsdl:part name="output" element="impl:TwoWayOneParameterEchoResponse"/>
</wsdl:message>
<wsdl:message name="NoParametersRequestMessage">
<wsdl:part name="input" element="impl:NoParametersRequest"/>
</wsdl:message>
<wsdl:message name="NoParametersResponseMessage">
<wsdl:part name="output" element="impl:NoParametersResponse"/>
</wsdl:message>
<wsdl:message name="MultipleParametersAddItemRequestMessage">
<wsdl:part name="input" element="impl:MultipleParametersAddItemRequest"/>
</wsdl:message>
<wsdl:message name="MultipleParametersAddItemResponseMessage">
<wsdl:part name="output" element="impl:MultipleParametersAddItemResponse"/>
</wsdl:message>
<!-- Port type (operations) -->
<wsdl:portType name="Axis2UserGuidePortType">
<wsdl:operation name="DoInOnly" parameterOrder="input">
<wsdl:input name="DoInOnlyRequestMessage"
message="impl:DoInOnlyRequestMessage"/>
</wsdl:operation>
<wsdl:operation name="TwoWayOneParameterEcho" parameterOrder="input">
<wsdl:input name="TwoWayOneParameterEchoRequestMessage"
message="impl:TwoWayOneParameterEchoRequestMessage"/>
<wsdl:output name="TwoWayOneParameterEchoResponseMessage"
message="impl:TwoWayOneParameterEchoResponseMessage"/>
</wsdl:operation>
<wsdl:operation name="NoParameters" parameterOrder="input">
<wsdl:input name="NoParametersRequestMessage"
message="impl:NoParametersRequestMessage"/>
<wsdl:output name="NoParametersResponseMessage"
message="impl:NoParametersResponseMessage"/>
</wsdl:operation>
<wsdl:operation name="MultipleParametersAddItem" parameterOrder="input">
<wsdl:input name="MultipleParametersAddItemRequestMessage"
message="impl:MultipleParametersAddItemRequestMessage"/>
<wsdl:output name="MultipleParametersAddItemResponseMessage"
message="impl:MultipleParametersAddItemResponseMessage"/>
</wsdl:operation>
</wsdl:portType>
<!-- BINDING (bind operations) -->
<wsdl:binding
name="Axis2UserGuideSoapBinding"
type="impl:Axis2UserGuidePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="DoInOnly">
<wsdlsoap:operation soapAction="DoInOnly"/>
<wsdl:input>
<wsdlsoap:body use="literal"/>
</wsdl:input>
</wsdl:operation>
<wsdl:operation name="TwoWayOneParameterEcho">
<wsdlsoap:operation soapAction="TwoWayOneParameterEcho"/>
<wsdl:input>
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NoParameters">
<wsdlsoap:operation soapAction="NoParameters"/>
<wsdl:input>
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="MultipleParametersAddItem">
<wsdlsoap:operation soapAction="MultipleParametersAddItem"/>
<wsdl:input>
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<!-- SERVICE -->
<wsdl:service name="Axis2UserGuideService">
<wsdl:port binding="impl:Axis2UserGuideSoapBinding"
name="Axis2UserGuide">
<wsdlsoap:address location="http://localhost/axis2/services/Axis2UserGuide"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
2. 生成service代码
step1: generating service source code by wsdl
command line:
D:\axis151\mysamples\sample1\wsdl2java.bat -uri file:///D:/axis151/mysamples/sample1/Axis2UserGuide.wsdl
-p org.apache.axis2.axis2userguide -o target_directory_name -d adb -s -wv 1.5.1 -ss -sd
result:
D:\axis151\mysamples\sample1\src\org\apache\axis2\axis2userguide\*.java(服务端代码)
D:\axis151\mysamples\sample1\resource\services.xml, Axis2UserGuideService.wsdl
(可以用远程地址实现,在下文中有介绍)
修改 Axis2UserGuideServiceSkeleton.java,在每个方法中填写入需要运行的代码(粗体字部分为添加的代码示例)
package org.apache.axis2.axis2userguide;
public class Axis2UserGuideServiceSkeleton{
public org.apache.axis2.axis2userguide.NoParametersResponse noParameters
(
org.apache.axis2.axis2userguide.NoParametersRequest noParametersRequest
)
{
System.out.println(noParametersRequest);
NoParametersResponse res = new NoParametersResponse();
return res;
//TODO : fill this with the necessary business logic
//throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#noParameters");
}
public void doInOnly
(
org.apache.axis2.axis2userguide.DoInOnlyRequest doInOnlyRequest
)
{
//TODO : fill this with the necessary business logic
System.out.println(doInOnlyRequest.getMessageString());
}
public org.apache.axis2.axis2userguide.MultipleParametersAddItemResponse multipleParametersAddItem
(
org.apache.axis2.axis2userguide.MultipleParametersAddItemRequest multipleParametersAddItemRequest
)
{
//TODO : fill this with the necessary business logic
System.out.println(multipleParametersAddItemRequest.getPrice());
System.out.println(multipleParametersAddItemRequest.getItemId());
System.out.println(multipleParametersAddItemRequest.getDescription());
System.out.println(multipleParametersAddItemRequest.getItemName());
MultipleParametersAddItemResponse res =
new MultipleParametersAddItemResponse();
res.setSuccessfulAdd(true);
res.setItemId(multipleParametersAddItemRequest.getItemId());
return res;
//throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#multipleParametersAddItem");
}
public org.apache.axis2.axis2userguide.TwoWayOneParameterEchoResponse twoWayOneParameterEcho
(
org.apache.axis2.axis2userguide.TwoWayOneParameterEchoRequest twoWayOneParameterEchoRequest
)
{
//TODO : fill this with the necessary business logic
System.out.println(twoWayOneParameterEchoRequest.getEchoString());
TwoWayOneParameterEchoResponse res =
new TwoWayOneParameterEchoResponse();
res.setEchoString(twoWayOneParameterEchoRequest.getEchoString());
return res;
//throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#twoWayOneParameterEcho");
}
}
3. 生成client代码
步骤1: command line:
D:\axis151\mysamples\sample1\WSDL2Java -uri Axis2UserGuide.wsdl -p org.apache.axis2.axis2userguide -d adb -s
result:
D:\axis151\mysamples\sample1\src\org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.java
D:\axis151\mysamples\sample1\build.xml
步骤2:写一个使用Axis2UserGuideServiceStub.java的客户端服务代码
Client.java
package org.apache.axis2.axis2userguide;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.DoInOnlyRequest;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.TwoWayOneParameterEchoRequest;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.NoParametersRequest;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.MultipleParametersAddItemRequest;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.TwoWayOneParameterEchoResponse;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.NoParametersResponse;
import org.apache.axis2.axis2userguide.Axis2UserGuideServiceStub.MultipleParametersAddItemResponse;
public class Client{
public static void main(java.lang.String args[]){
try{
Axis2UserGuideServiceStub stub =
new Axis2UserGuideServiceStub
("http://localhost/axis2/services/Axis2UserGuideService");
doInOnly(stub);
twoWayOneParameterEcho(stub);
noParameters(stub);
multipleParameters(stub);
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void doInOnly(Axis2UserGuideServiceStub stub){
try{
DoInOnlyRequest req = new DoInOnlyRequest();
req.setMessageString("An in only request");
stub.doInOnly(req);
System.out.println("done");
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void twoWayOneParameterEcho(Axis2UserGuideServiceStub stub){
try{
TwoWayOneParameterEchoRequest req = new TwoWayOneParameterEchoRequest();
req.setEchoString("echo! ... echo!");
TwoWayOneParameterEchoResponse res =
stub.twoWayOneParameterEcho(req);
System.out.println(res.getEchoString());
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void noParameters(Axis2UserGuideServiceStub stub){
try{
NoParametersRequest req = new NoParametersRequest();
System.out.println(stub.noParameters(req));
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void multipleParameters(Axis2UserGuideServiceStub stub){
try{
MultipleParametersAddItemRequest req =
new MultipleParametersAddItemRequest();
req.setPrice((float)1.99);
req.setItemId((int)23872983);
req.setDescription("Must have for cooking");
req.setItemName("flour");
MultipleParametersAddItemResponse res =
stub.multipleParametersAddItem(req);
System.out.println(res.getSuccessfulAdd());
System.out.println(res.getItemId());
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
}
4. 部署及测试
1) 生成service
D:\axis151\mysamples\sample1\ant jar.server
复制以上生成的 .aar file to %tomcat_home%/webapps/axis2/web-inf/services/
2) 生成client
D:\axis151\mysamples\sample1\ant jar.client
3) 部署及测试
在自动生成的build.xml中增加一段测试代码
<target if="jars.ok" name="run.client">
<java classname="org.apache.axis2.axis2userguide.Client">
<classpath refid="client.class.path" />
</java>
<path id="client.class.path">
<pathelement location="${lib}/${name}-test-client.jar"/>
<path refid="axis2.class.path"/>
<pathelement location="${classes}"/>
</path>
</target>
D:\axis151\mysamples\sample1\ant run.client