还是用 我博客《webservice 学习笔记,axis2传输简单对象,非stub方式
》的例子:
首先建立ServiceTest.java
package sample.ws.service; import sample.ws.pojo.Person; public class ServiceTest { /** * 很简单我们只是将发过来的person[]返回去 * @param person * @return */ public Person[] getPerson(Person[] person){ // Person person1 = new Person(); // person1.setAge("20"); // person1.setName("张三"); // // Person person2 = new Person(); // person2.setAge("30"); // person2.setName("李四"); // // Person[] person3 = new Person[2]; // person3[0] = person1; // person3[1] = person2; // System.out.println("afd"); return person; } }
pojo Person.java 不变
public class Person{ private String name ; public String getName() { return name; } public void setName(String name) { this.name = name; } }
services.xml 也保持不变;
<?xml version="1.0" encoding="UTF-8"?> <service name="TestService" scope="application"> <description>TestService</description> <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> <parameter name="ServiceClass"> sample.ws.service.ServiceTest </parameter> </service>
下面就是客户端了:
1、 pojo Person.java 保持不变
public class Person{ private String name ; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、请求客户端 Ws2pojoClient.java
package sample.ws.client; import javax.xml.namespace.QName; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import sample.ws.pojo.Person; public class Ws2pojoClient { public static void main(String args[]) throws java.lang.Exception { RPCServiceClient client = new RPCServiceClient(); Options option = client.getOptions(); // 指定客户端访问的webservice服务器端地址 EndpointReference erf = new EndpointReference( "http://localhost:9999/Ws2Pojo/services/TestService"); option.setTo(erf); // 指定命名空间,指定要调用的方法 QName name = new QName("http://service.ws.sample", "getPerson"); // 创建Person对象 Person person1 = new Person(); person1.setAge("20"); person1.setName("张三"); Person person2 = new Person(); person2.setAge("30"); person2.setName("李四"); // 创建Person数组 Person[] person3 = new Person[2]; person3[0] = person1; person3[1] = person2; // 创建要传送的object数组 Object[] object = new Object[] { person3 }; // 创建返回的参数类型 Class[] returnTypes = new Class[] { Person[].class }; // 调用远程服务,得到返回的object数组 Object[] response = client.invokeBlocking(name, object, returnTypes); // 强制转换成Person[]对象 Person[] p = (Person[]) response[0]; // 遍历得到我们刚刚请求过去的值 for (int i = 0; i < p.length; i++) { System.out.println(p[i].getAge()); System.out.println(p[i].getName()); } } }
得到结果:
20
张三
30
李四
如果有问题可以加群 : 84242283(webservice交流学习)