axis 之 传递复杂类型
从客户端除了传递字符串以外还可以传递复杂对象(对象必须序列化了),List,Map,数组和文件。
(1)定义一个对象实现了serializable 接口
package cn.com.chenlly.ssh.webservice.axis;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* @Class Address.java
* @Description
* @Copyright (c) 2010, 正辰科技有限公司 All rights reserved.
* @Author Chenlly
* @Version 1.0
* @Date Apr 19, 2010 5:03:23 PM
*/
public class Address implements Serializable {
private Integer identifier;
private String address;
private String city;
private String province;
private String country;
private String []array;
private List<Integer> list;
private boolean isExst;
//constructor
public Address(){
list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
}
public Integer getIdentifier() {
return identifier;
}
public void setIdentifier(Integer identifier) {
this.identifier = identifier;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public boolean isExst() {
return isExst;
}
public void setExst(boolean isExst) {
this.isExst = isExst;
}
}
(2)定义server-config.wsdd文件
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="wsTestHandler"
type="java:cn.com.chenlly.ssh.webservice.axis.WSTestServiceHandle">
<parameter name="status" value="success" />
</handler>
<!-- 自定义服务 -->
<service name="ws" provider="java:RPC">
<parameter name="className"
value="cn.com.chenlly.ssh.webservice.axis.WSTestServiceImpl" />
<parameter name="allowedMethods" value="*" />
<parameter name="scope" value="request" />
<responseFlow>
<handler type="wsTestHandler" />
</responseFlow>
<requestFlow>
<handler type="wsTestHandler" />
</requestFlow>
<beanMapping qname="myNSD:Address"
xmlns:myNSD="urn:AddressManager"
languageSpecificType="java:cn.com.chenlly.ssh.webservice.axis.Address">
</beanMapping>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
</deployment>
//主要是<beanMapping>标签中名字空间和qname写法
第一个你自定义的命名空间和第二个节点本地部分会组成一个新的QName。
在IE 中打开链接
http://192.168.1.98:8082/SSHProject/services/ws?wsdl
生成的WSDL有如下一段
- <schema targetNamespace="urn:AddressManager" xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://192.168.1.98:8082/SSHProject/services/ws" />
<import namespace="http://xml.apache.org/xml-soap" />
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
- <complexType name="Address">
- <sequence>
<element name="address" nillable="true" type="soapenc:string" />
<element name="array" nillable="true" type="impl:ArrayOf_soapenc_string" />
<element name="city" nillable="true" type="soapenc:string" />
<element name="country" nillable="true" type="soapenc:string" />
<element name="exst" type="xsd:boolean" />
<element name="identifier" nillable="true" type="soapenc:int" />
<element name="list" nillable="true" type="impl:ArrayOf_xsd_anyType" />
<element name="province" nillable="true" type="soapenc:string" />
</sequence>
</complexType>
</schema>
这个新的schema就是把对象序列化以后生成了xml流文件。
(3) webService 服务方法
public Address dealAddress(Address address) {
System.out.println("service exst:"+address.isExst());
//客户端对象传递过来设置标志为true
address.setExst(true);
return address;
}
(4) 客户端
package cn.com.chenlly.ssh.webservice.axis;
import java.util.ArrayList;
import java.util.List;
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;
/**
* @Class WSTestService.java
* @Description 客户端调用远程的service
* @Copyright (c) 2010, 正辰科技有限公司 All rights reserved.
* @Author Chenlly
* @Version 1.0
* @Date Apr 9, 2010 3:57:26 PM
*/
public class WSTestServiceClient {
public static void main(String[] args) {
Service service = new Service();
try {
Call call = (Call) service.createCall();
String url = "http://192.168.1.98:8082/SSHProject/services/ws?wsdl";
call.setTargetEndpointAddress(new java.net.URL(url));
//定义对象
Address address = new Address();
address.setIdentifier(1);
address.setProvince("湖南");
address.setCity("长沙");
address.setExst(false);
QName qn = new QName("urn:AddressManager", "Address");//第一个参数名字空间URI,第二个参数本地部分,注意这两部分在server-config.wsdd文件中标签beanMapping配置
call.registerTypeMapping(Address.class, qn,
new BeanSerializerFactory(Address.class, qn),//序列化
new BeanDeserializerFactory(Address.class, qn));
call.setOperationName(new QName(url,"dealAddress"));
call.addParameter("arg0", qn, ParameterMode.IN);//定义一个参数类型,如果是String类型的参数可以不需要这句话
call.setReturnClass(Address.class);//指定返回类型
Address result = (Address) call.invoke(new Object[]{address});//这里传递给service的是一个对象
System.out.println(result.isExst());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//在服务器端打印的isExst()方法是false;然后设置address的exst为true;最后又传送到客户端打印的result结果为true