需要jar包:
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
mail.jar
wsdl4j.jar
测试:
package axis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.axis.encoding.XMLType;
public class TestAxis {
public static void main(String[] args) {
String webServiceURL = "http://127.0.0.1:8087//services/TEST";
String namespaceURI = "webservices.cwf.weaver.com.cn";
String method = "doCreateWorkflowRequest";
List<Map<String, Object>> params=new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("paramName", "in0");
map.put("paramType", XMLType.XSD_STRING);
map.put("paramValue", "testdata");
params.add(map);
String result=(String) AxisClient.getResult(webServiceURL, namespaceURI, method, null, params, XMLType.XSD_STRING);
System.out.println(result);
}
}
代码:
package axis;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Map;
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;
public class AxisClient {
/**
* Axis方式访问WebService接口
* @param webServiceURL webService地址,末尾为?wsdl时不需要写
* @param namespaceURI 命名空间
* @param method 方法名称
* @param soapAction soapAction
* @param params 参数
* @param returnType 返回值类型
* @return
*/
public static Object getResult(String webServiceURL, String namespaceURI,
String method, String soapAction, List<Map<String, Object>> params,
QName returnType) {
if(null==params||params.size()==0)return null;
// 创建客户端
Service service = new Service();
Call call = null;
try {
call = (Call) service.createCall();
// 设置访问地址
call.setTargetEndpointAddress(new URL(webServiceURL));
// 是否使用SOAPAction
if(null==soapAction||"".equals(soapAction)){
call.setUseSOAPAction(false);
}else{
call.setUseSOAPAction(true);
// 设置SOAPAction地址
call.setSOAPActionURI(soapAction);
}
// 设置访问方法名称
call.setOperationName(new QName(namespaceURI, method));
// 添加解析类型的地址
call.setEncodingStyle(namespaceURI);
// 设置返回值类型
call.setReturnType(returnType);
// 添加参数名称
Object[] obj = new Object[params.size()];
for (int i=0;i<params.size();i++) {
Map<String, Object> map = params.get(i);
call.addParameter(new QName((String) map.get("paramName")),
(QName) map.get("paramType"), ParameterMode.IN);
obj[i]=map.get("paramValue");
}
return call.invoke(obj);
} catch (ServiceException e) {
e.printStackTrace();
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
}
}