1> 编写自定义参数类型和返回值类型类
package com.webservice.object; import java.io.Serializable; public class Person implements Serializable { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2> 编写服务(方法)
package com.webservice.wsdd; import com.webservice.object.Person; public class PersonService { public Person changeName(Person person){ Person newPerson = new Person(); if(person!=null){ System.out.println("person id = " + person.getId()); if(person.getId()==1){ newPerson.setName("zhangsan"); }else{ newPerson.setName("lisi"); } } return newPerson; } }
3> 发布(和之前发布方法相同, 配置server-config.wsdd)
请参照http://blog.csdn.net/zhngjan/article/details/9186603 发布方法相同
具体配置参考如下:
<service name="PersonService" provider="java:RPC"> <parameter name="allowedMethods" value="changeName"/> <parameter name="className" value="com.webservice.wsdd.PersonService"/> <beanMapping languageSpecificType="java:com.webservice.object.Person" qname="ns1:Person" xmlns:ns1="BeanService"/> </service>
4> 编写客户端访问方法
package com.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; import org.apache.axis.wsdl.symbolTable.Parameter; import com.webservice.object.Person; public class TestWSDDObject { /** * @param args */ public static void main(String[] args) { try{ Person personIn = new Person(); personIn.setId(1); String url = "http://localhost:8888/axis/services/PersonService"; Service service = new Service(); Call call = (Call)service.createCall(); // 设置调用参数类型 QName qn = new QName("urn:BeanService", "Person"); call.registerTypeMapping(Person.class, qn, new BeanSerializerFactory(Person.class, qn), new BeanDeserializerFactory(Person.class, qn)); // 设置调用服务的地址 call.setTargetEndpointAddress(new URL(url)); // 设置调用方法名 call.setOperationName(new QName("", "changeName")); // 设置参数值 call.addParameter("arg1", qn, ParameterMode.IN); // 设置返回值类型 call.setReturnClass(Person.class); // 调用 Person personOut = (Person)call.invoke(new Object[]{personIn}); String name = "no !"; if(personOut !=null) { name = personOut.getName(); } System.out.println("name = " + name); }catch(Exception e){ e.printStackTrace(); } } }