[原创]webservice 学习笔记,axis2传递简单对象,非stub方式
首先在MyEclipse中创建一个web project,创建方式可以参见我博客中的《webservice 笔记(小结) axis2 集成spring <一>发布服务》一文,
project取名 Ws2Pojo
服务器端
1、创建pojo类:
package sample.ws.pojo;public class Person {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}
package sample.ws.service;import sample.ws.pojo.Person;public class ServiceTest {/** * 很简单我们只是将发过来的person返回去 * @param person * @return */public Person getPerson(Person person){return person;} }
<?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" name="code">package sample.ws.pojo;public class Person {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}
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:8080/Ws2Pojo/services/TestService");option.setTo(erf);//指定命名空间,指定要调用的方法QName name = new QName("http://service.ws.sample","getPerson");//创建Person对象Person person = new Person();person.setAge("20");person.setName("张三");//创建要传送的object数组Object[] object = new Object[]{person};//创建返回的参数类型Class[] returnTypes = new Class[]{Person.class};//调用远程服务,得到返回的object数组Object[] response = client.invokeBlocking(name, object, returnTypes);//强制转换成Person对象Person p = (Person)response[0];System.out.println(p.getAge());System.out.println(p.getName());}}