package com.gxx.record.webservice; import javax.xml.namespace.QName; import org.apache.axiom.om.OMElement; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; /** * 测试WebService * @author Gxx */ public class TestWebservice { /** * 测试 * @throws Exception */ public void test() throws Exception { String url = "http://localhost:8080/record/services/AxisService?wsdl"; try { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); Options options = serviceClient.getOptions(); // 确定目标服务地址 options.setTo(targetEPR); // 确定调用方法 options.setAction("urn:jiafa"); options.setTimeOutInMilliSeconds(60000); // 上图中的targetNamespace QName qname = new QName("http://impl.service.record.gxx.com","jiafa"); // 指定jiafa方法的参数值 String requestContent = ""; try { } catch (Exception e) { throw new Exception(e.getMessage(), e.getCause()); } Object[] parameters = new Object[] {20,30}; // 指定getPrice方法返回值的数据类型的Class对象 Class[] returnTypes = new Class[] { String.class }; // 调用方法一 传递参数,调用服务,获取服务返回结果集 OMElement element = serviceClient.invokeBlocking(qname, parameters); // 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。 String result = element.getFirstElement().getText(); System.out.println(result); } catch (AxisFault e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { new TestWebservice().test(); } }