Axis2支配WebService-简单应用
Axis2部署WebService-----简单应用
首先介绍一下,这个应用的业务逻辑,很简单,就是客户端向服武器端发送一个用户名,然后服务器端根据用户名返回,该人的详细信息.
1):首先写个 服武器端的service
该类的代码如下:
2):用上边的服务端的service 生成 AxisTest.wsdl文件 用eclipse的插件生成代码的过程我就部说了.
这个文件的代码如下:
3):写个客户端的代码用来和服务器端通信:代码如下:
4):在联合上述客户端和服务器端通信的代码中还有两个类,他们用来携带客户端向服务器端发送的消息和服务器端根据客户端发送来的消息返回的消息.代码如下:
5):还有一个比较重要的文件 就是services.xml
代码如下:
到现在代码都已经完成了用eclipse的打包工具,把它打成包就可以发布了,运行客户端程序就可以在控制台和tomcat控制台看到结果了,注意服务器端的信息只打印在tomcat控制台上.在eclipse的控制台看到的信息是客户端和服务器发回的信息.
特别注意的是,如果在编译中出现问题,很可能是jar包版本不同导致的.
首先介绍一下,这个应用的业务逻辑,很简单,就是客户端向服武器端发送一个用户名,然后服务器端根据用户名返回,该人的详细信息.
1):首先写个 服武器端的service
该类的代码如下:
package com.neusoft.axis.service; import com.neusoft.axis.common.AxisClientRequestInfo; import com.neusoft.axis.common.AxisServiceResponseInfo; public class AxisServiceResponse { public AxisServiceResponseInfo axisServiceReturnPersonInfo(AxisClientRequestInfo clientrequest) { AxisServiceResponseInfo axisserviceresponseinfo = new AxisServiceResponseInfo(); String name = clientrequest.getName(); if(name.equals("许兆光")) { axisserviceresponseinfo.setAge("25"); axisserviceresponseinfo.setDegree("本科"); axisserviceresponseinfo.setSex("男"); } else if(name.equals("葛祥平")) { axisserviceresponseinfo.setAge("27"); axisserviceresponseinfo.setDegree("研究生"); axisserviceresponseinfo.setSex("男"); } else { axisserviceresponseinfo.setAge(""); axisserviceresponseinfo.setDegree(""); axisserviceresponseinfo.setSex(""); System.out.println("客户端没有发送姓名到服务端!"); } if(name.equals("")) { return axisserviceresponseinfo; } System.out.println("客户端发送来的姓名是:"+name); System.out.println("下边是服武器端根据客户端发送来姓名得到的该人的详细信息:"); System.out.println("姓名=========="+name); System.out.println("年龄=========="+axisserviceresponseinfo.getAge()); System.out.println("学位=========="+axisserviceresponseinfo.getDegree()); System.out.println("性别=========="+axisserviceresponseinfo.getSex()); return axisserviceresponseinfo; } }
2):用上边的服务端的service 生成 AxisTest.wsdl文件 用eclipse的插件生成代码的过程我就部说了.
这个文件的代码如下:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:ns1="http://AxisServiceResponse.service.axis.neusoft.com/types" xmlns:ns="http://AxisServiceResponse.service.axis.neusoft.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://AxisServiceResponse.service.axis.neusoft.com"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:stn_1="http://common.axis.neusoft.com/xsd" targetNamespace="http://common.axis.neusoft.com/xsd" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <xs:element type="stn_1:AxisClientRequestInfo" name="AxisClientRequestInfo" /> <xs:complexType name="AxisClientRequestInfo"> <xs:sequence> <xs:element type="xs:string" name="name" /> </xs:sequence> </xs:complexType> <xs:element type="stn_1:AxisServiceResponseInfo" name="AxisServiceResponseInfo" /> <xs:complexType name="AxisServiceResponseInfo"> <xs:sequence> <xs:element type="xs:string" name="age" /> <xs:element type="xs:string" name="degree" /> <xs:element type="xs:string" name="sex" /> </xs:sequence> </xs:complexType> <xs:complexType name="AxisClientRequestInfo"> <xs:sequence> <xs:element type="xs:string" name="name" /> </xs:sequence> </xs:complexType> <xs:complexType name="AxisServiceResponseInfo"> <xs:sequence> <xs:element type="xs:string" name="age" /> <xs:element type="xs:string" name="degree" /> <xs:element type="xs:string" name="sex" /> </xs:sequence> </xs:complexType> </xs:schema> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:types="http://AxisServiceResponse.service.axis.neusoft.com/types" xmlns:stn_3="http://common.axis.neusoft.com/xsd" targetNamespace="http://AxisServiceResponse.service.axis.neusoft.com/types" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <xs:import namespace="http://common.axis.neusoft.com/xsd" /> <xs:element name="axisServiceReturnPersonInfo"> <xs:complexType> <xs:sequence> <xs:element type="stn_3:AxisClientRequestInfo" name="clientrequest" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="axisServiceReturnPersonInfoResponse"> <xs:complexType> <xs:sequence> <xs:element type="stn_3:AxisServiceResponseInfo" name="return" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="axisServiceReturnPersonInfoMessage"> <wsdl:part element="ns1:axisServiceReturnPersonInfo" name="part1" /> </wsdl:message> <wsdl:message name="axisServiceReturnPersonInfoResponseMessage"> <wsdl:part element="ns1:axisServiceReturnPersonInfoResponse" name="part1" /> </wsdl:message> <wsdl:portType name="AxisServiceResponsePortType"> <wsdl:operation name="axisServiceReturnPersonInfo"> <wsdl:input message="ns:axisServiceReturnPersonInfoMessage" /> <wsdl:output message="ns:axisServiceReturnPersonInfoResponseMessage" /> </wsdl:operation> </wsdl:portType> <wsdl:binding type="ns:AxisServiceResponsePortType" name="AxisServiceResponseSOAP11Binding"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="axisServiceReturnPersonInfo"> <soap:operation style="document" soapAction="urn:axisServiceReturnPersonInfo" /> <wsdl:input> <soap:body namespace="http://AxisServiceResponse.service.axis.neusoft.com" use="literal" /> </wsdl:input> <wsdl:output> <soap:body namespace="http://AxisServiceResponse.service.axis.neusoft.com" use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding type="ns:AxisServiceResponsePortType" name="AxisServiceResponseSOAP12Binding"> <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="axisServiceReturnPersonInfo"> <soap12:operation style="document" soapAction="urn:axisServiceReturnPersonInfo" /> <wsdl:input> <soap12:body namespace="http://AxisServiceResponse.service.axis.neusoft.com" use="literal" /> </wsdl:input> <wsdl:output> <soap12:body namespace="http://AxisServiceResponse.service.axis.neusoft.com" use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="AxisServiceResponse"> <wsdl:port binding="ns:AxisServiceResponseSOAP11Binding" name="AxisServiceResponseSOAP11port"> <soap:address location="http://localhost:8080/axis2/services/AxisServiceResponse" /> </wsdl:port> <wsdl:port binding="ns:AxisServiceResponseSOAP12Binding" name="AxisServiceResponseSOAP12port"> <soap12:address location="http://localhost:8080/axis2/services/AxisServiceResponse" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
3):写个客户端的代码用来和服务器端通信:代码如下:
package com.neusoft.axis.client; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import com.neusoft.axis.common.AxisClientRequestInfo; import com.neusoft.axis.common.AxisServiceResponseInfo; public class AxisClientRequest { public static void main(String[] args) throws AxisFault { System.out.println("==========客户端发送一个用户名,下边是服武器端根据用户名返回的该人的详细信息"); System.out.println("==========开始向服武器端发送数据"); RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference("http://localhost:8888/axis2/services/AxisTest"); options.setTo(targetEPR); QName sendPersonInfo = new QName("http://AxisServiceResponse.service.axis.neusoft.com/types", "axisServiceReturnPersonInfo"); AxisClientRequestInfo axisclientrequestinfo = new AxisClientRequestInfo(); axisclientrequestinfo.setName("许兆光"); System.out.println("==========客户端发送的姓名是:"+axisclientrequestinfo.getName()); Class[] returnTypes = new Class[] { AxisServiceResponseInfo.class }; Object[] opSendInfo = new Object[] { axisclientrequestinfo}; System.out.println("==========开始请求服武器端方法:"+"axisServiceReturnPersonInfo"); Object[] response = serviceClient.invokeBlocking(sendPersonInfo, opSendInfo, returnTypes); System.out.println("==========客户端请求服务端方法成功!"); AxisServiceResponseInfo result = (AxisServiceResponseInfo) response[0]; System.out.println("==========下边是从服武器返回"+axisclientrequestinfo.getName()+"的个人信息是:"); System.out.println("==========年龄"+result.getAge()); System.out.println("==========学历"+result.getDegree()); System.out.println("==========性别"+result.getSex()); } }
4):在联合上述客户端和服务器端通信的代码中还有两个类,他们用来携带客户端向服务器端发送的消息和服务器端根据客户端发送来的消息返回的消息.代码如下:
package com.neusoft.axis.common; public class AxisClientRequestInfo { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } package com.neusoft.axis.common; public class AxisServiceResponseInfo { private String sex; private String age; private String degree; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
5):还有一个比较重要的文件 就是services.xml
代码如下:
<service name="AxisTest" scope="application" targetNamespace="http://AxisServiceResponse.service.axis.neusoft.com/types"> <description>Security Joint Defence Service</description> <parameter name="ServiceClass" locked="false"> com.neusoft.axis.service.AxisServiceResponse </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </messageReceivers> <schema schemaNamespace="http://AxisServiceResponse.service.axis.neusoft.com/types" /> </service>
到现在代码都已经完成了用eclipse的打包工具,把它打成包就可以发布了,运行客户端程序就可以在控制台和tomcat控制台看到结果了,注意服务器端的信息只打印在tomcat控制台上.在eclipse的控制台看到的信息是客户端和服务器发回的信息.
特别注意的是,如果在编译中出现问题,很可能是jar包版本不同导致的.