webservice
的调用,常用的大约有
3
种方式:
1
、使用
axis
调用
2
、使用
xfire
调用
3
、使用
cxf
调用
本文先介绍axis调用
本文先介绍axis调用
先上代码:
package com.java;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
public class WebServiceTest {
public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {
//标识Web Service的具体路径
String endpoint="http://localhost/EMRToMobileService/EMRToMobileService.asmx?WSDL";
//发布的wsdl里的targetNamespace里的url
String nameSpaceUri = "http://tempuri.org/";
Call call;
Object res = "";
// 创建 Service实例
Service service = new Service();
try {
// 通过Service实例创建Call的实例
call = (Call)service.createCall();
String strPatientID="415555";
String strEventNo="1";
//将Web Service的服务路径加入到call实例之中.
call.setTargetEndpointAddress(new java.net.URL(endpoint));
//发布的方法名
call.setOperationName(new QName(nameSpaceUri,"GetEMRList"));
//设置参数,主要要和net接口中的名称一致,类型尽量用String,注意QName中参数的设置
call.addParameter(new QName("http://tempuri.org/","strPatientID"), org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/","strEventNo"), org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
//这里注意有方法名,已经引用的地址。从net接口中找 ,注意地址的写法
call.setSOAPActionURI("http://tempuri.org/GetEMRList");
res =call.invoke(new Object[]{strPatientID,strEventNo});
//返回值String
System.err.println("method:"+res);
} catch (ServiceException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
finally {
}
}
}
然后将XML文件解析出来,获取需要的参数值。
这里的写法模式基本上是固定的,最需要注意的就是传入的地址和参数的写法,否则很容易就报错了。
两种报错:
(1){http://xml.apache.org/axis/}HttpErrorCode:404
(404)Not Found
不是你的webservice没有发布成功,就是你的url输入错了,仔细去检查下
(2) faultDetail:
{http://xml.apache.org/axis/}stackTrace:服务器无法处理请求。 ---> ORA-00936: 缺失表达式
这种情况则是参数没有设置正确,导致传入参数是报错
推荐博客:http://www.tuicool.com/articles/6NjqIn
http://blog.youkuaiyun.com/quwei7515/article/details/17952085
http://zhidao.baidu.com/link?url=35bzoFNmQCM-AGt_m5F8-oC1EW3q-X2HborpRZ1zbwW36bpPbUGnblGQDUeIYA_5xwJE4LlEfWOkjG7VoyTq_Xlvi-lq8VMKjje8jR52K6W