axis1.4 传递嵌套list的复杂对象
项目需要,发布webservice服务端,向客户端传递复杂对象。对象包含了list<>,反复研究了一下,发现webservice不能够直接传递list对象,必须转换成对象数组才能传递。javaBean:
注意:<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
如果没有该配置,将无法在浏览器中显示wsdl详细信息。
<handler type="java:com.wensi.service.server.BSHeaderHandler" name="BSHeaderHandler"/>
和
<requestFlow>
<handler type="BSHeaderHandler"/>
</requestFlow>
是实现一个相当于拦截器的功能,客户端在调用服务端的接口之前,必须经过这个方法的处理。
在这里,我写了一个权限验证的拦截器,代码如下:
由于客户端是手持机,而这边的客户端是java ssh架构,所以存储用户信息,正能依靠内存来完成。
客户端调用的时候,需要向soapHeader放验证信息。
客户端代码如下:package com.wensi.service.client;import java.net.MalformedURLException;import java.net.URL;import java.rmi.RemoteException;import java.util.Date;import javax.xml.namespace.QName;import javax.xml.rpc.ParameterMode;import javax.xml.rpc.ServiceException;import javax.xml.rpc.encoding.XMLType;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.ser.ArrayDeserializerFactory;import org.apache.axis.encoding.ser.ArraySerializerFactory;import org.apache.axis.encoding.ser.BeanDeserializerFactory;import org.apache.axis.encoding.ser.BeanSerializerFactory;import org.apache.axis.message.SOAPHeaderElement;import com.wensi.common.Util;import com.wensi.service.server.AccountCustomerBean;import com.wensi.service.server.AccountFlag;import com.wensi.service.server.AccountResponse;import com.wensi.service.server.AuthorityBean;import com.wensi.service.server.LoginResponse;import com.wensi.service.server.OperatorBean;import com.wensi.service.server.ServiceConstant;public class ZNWServiceClient {String endpoint = ServiceConstant.endpoint;//webservice的具体路径Service service = new Service();//创建service实例public LoginResponse login(String userName,String password) throws Exception{SOAPHeaderElement cpHeader = new SOAPHeaderElement(endpoint,"MessageHeader");cpHeader.setPrefix("cp");cpHeader.setMustUnderstand(true); SOAPElement ele = null;ele = cpHeader.addChildElement("isLogin");ele.addTextNode("true");//创建call实例Call call = (Call) service.createCall();//将webservice的服务路径加入到call实例中,并为call设置服务的位置URL url = new URL(endpoint);call.setTargetEndpointAddress(url);//调用webservice 的方法call.setOperationName("login");//序列化对象QName qn1 = new QName("urn:ZNWService","OperatorBean");call.registerTypeMapping(OperatorBean.class, qn1, new BeanSerializerFactory(OperatorBean.class,qn1), new BeanDeserializerFactory(OperatorBean.class,qn1));QName qn2 = new QName("urn:ZNWService","AuthorityBean");call.registerTypeMapping(AuthorityBean.class, qn2, new BeanSerializerFactory(AuthorityBean.class,qn2), new BeanDeserializerFactory(AuthorityBean.class,qn2));QName qn3 = new QName("urn:ZNWService","ArrayOfAuthorityBean");call.registerTypeMapping(AuthorityBean[].class, qn3,new ArraySerializerFactory(),new ArrayDeserializerFactory());QName qn4 = new QName("urn:ZNWService","LoginResponse");call.registerTypeMapping(LoginResponse.class, qn4, new BeanSerializerFactory(LoginResponse.class,qn4), new BeanDeserializerFactory(LoginResponse.class,qn4));call.addParameter("userName", org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);call.addParameter("password", org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);call.setReturnType(qn1, LoginResponse.class);call.addHeader(cpHeader);LoginResponse loginRes = (LoginResponse) call.invoke(new Object[]{userName,password});return loginRes;}}