使用Axis调用WebSerivices非常简单,废话不多说,直接上代码
?
import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; public class ClientCall { public static void main(String[] args) { try { String url = "此处为webServices地址"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(new QName(url, "exec"));//exec为要调用的方法名 call.addParameter(new QName("此处为命名空间,为WSDL文件中的targetNamespace地址", "units"),XMLType.SOAP_STRING,ParameterMode.IN);//这里的units为传入参数的变量名字 call.setReturnType(new QName("此处为命名空间,为WSDL文件中的targetNamespace地址", "response"), Xljg[].class);//Xlgj[]这里是重点,返回数组时主要在些配置,Xljg为返回的对象 // 注册映射关系 QName XljgInfo = new QName("此处为命名空间,为WSDL文件中的targetNamespace地址", "xljg");//此处的xljg为WSDL文件中complexType name的属性值 call.registerTypeMapping(Xljg.class, XljgInfo, new BeanSerializerFactory(Xljg.class, XljgInfo), new BeanDeserializerFactory(Xljg.class, XljgInfo));//Xljg.class同上,这里不需要为数组 Xljg[] xljg = (Xljg[]) call.invoke(new Object[] {"昆明"});//昆明为传入的参数,即上面的units System.out.println(xljg.length);//打印输出数组 for(Xljg xl : xljg){ System.out.println(xl.getQualifications()+": "+xl.getCount()); } } catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }
?
?Xljg类的代码:
public class Xljg { private String qualifications; private long count; public String getQualifications() { return qualifications; } public long getCount() { return count; } }
?