我们已经知道现在普遍存在两种用Java调用Webservice的方法,一种是用Axis,另一种是用Soap.
下面的例子是用的第一种方法:
1. 环境配置:
1.1将以下六个jar包拷至工程下defaultroot/WEB-INF/lib:
axis-1.4.jar;
axis-jaxrpc-1.4.jar;
axis-saaj-1.4.jar;
commons-discovery-0.2.jar;
wsdl4j-1.5.1.jar; xalan-2.7.0.jar
前五个包可以通过以下地址下载axis-1.4包后,从解压后的lib文件夹中获取。 http://apache.mirror.phpchina.com/ws/axis/1_4/。最后一个包可以通过以下地址获取:http://people.apache.org/repo/m1-ibiblio-rsync-repository/xalan/jars/。
2.Java客户端和WebService之间的接口类作成,源代码如下:
/* パッケージのimport */
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.types.Schema;
/** * WebServiceを呼び出すクラス*/
public class AppWebServiceAdapterUtility {
/ * *
* WebServiceを呼び出すクラスの実処理
* @param args ArrayList 呼び出すWebServiceメソッドのパラメータ
* @param parameters ArrayList パラメータ値
* @param methodName String 呼び出すWebServiceメソッド名
* @return Schema 呼び出すWebServiceメソッドの返す结果
* @exception RemoteException 业务EJB例外
* @exception ValidationException
* /
public static Schema linkAdapter(ArrayList args, ArrayList parameters, String methodName) throws Exception {
Service service = new Service();
Call call = null;
try {
call = (Call) service.createCall();
} catch(ServiceException se) {
throw new EcException();
}
// URLを设定する。
call.setTargetEndpointAddress(new java.net.URL(AppProperty.getWsUrl()));
// 名前を设定する。
call.setOperationName(new QName(AppProperty.getSoapUri(),methodName));
// パラメータを设定する
for (int i=0; i < args.size(); i++) {
if ((null != args.get(i)) && (args.get(i) instanceof String[])) {
String[] arrayArgs = (String[]) args.get(i);
call.addParameter(new QName(AppProperty.getSoapUri(),arrayArgs[0]),
new QName(AppProperty.getParaPrefix() + arrayArgs[1]),
javax.xml.rpc.ParameterMode.IN);
}
}
// SOAPを利用するか
call.setUseSOAPAction(true);
// 戻り値の类型を设定する。
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);
// SOAPのURLを设定する。
call.setSOAPActionURI(AppProperty.getSoapUri() + "/" + methodName);
// WebServiceを呼び出す。
Schema result = null;
try {
result = (Schema)call.invoke(parameters.toArray());
} catch(RemoteException re) {
throw new EcException();
}
// 结果を返す
return result;
}
}
3.调用接口的Java客户端作成,示例代码如下所示:
import java.util.ArrayList;
import org.apache.axis.types.Schema;
个别包名省略...
public class WSAdapterTest {
public static void main(String[] args) throws Exception {
// 呼び出すWebServiceメソッドのパラメータ名前とパラメータ型
String[][] arrayArgs = {{"p_LoginID","STRING"},
{"p_PjNo","STRING"},
{"p_SubPjNo","STRING"},
{"p_ManageFlg","INT"},
{"p_NoticeFlg","INT"},
{"p_Reason","STRING"}
}; ① // パラメータ値
Object[] arrayParas = {"ddd","ffff","ddd",new Integer(1),new Integer(1),"dddd"}; ② // インターフェイスメソッドのパラメータを作成する
ArrayList argus = new ArrayList();
ArrayList parameters = new ArrayList();
for (int i=0; i < arrayArgs.length; i++) {
argus.add(arrayArgs[i]);
}
for (int j=0; j < arrayParas.length; j++) {
parameters.add(arrayParas[j]);
}
/* EcExceptionが起こった际の设定 */
AppErrorData error = new AppErrorData();
// インターフェイスメソッドを呼び出す
Schema result = null;
try {
result = (Schema) AppWebServiceAdapterUtility.linkAdapter(argus, parameters, AppConstants.WS_METHOD_NM_SUB_PROJECT_MANAGED_UPDATE); ③
} catch(EcException ee) {
error.setErrorMsg(error.getErrorMsg("RC_WS_EXCEPTION") + ee.getMessage());
throw new ValidationException(error);
}
// 结果が「true」のでの场合
// System.out.println("result:" + result.get_any()[0].getValue());
// if (AppConstants.WS_FALSE.equals(result.get_any()[0].getValue())) {
// error.setErrorMsg(error.getErrorMsg("RC_WS_FAILED"));
// throw new ValidationException(error); // }
// 结果が「true」とメッセージの场合
System.out.println("result:" + result.get_any()[1].getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(0).getFirstChild().getNodeValue());
System.out.println("message:" + result.get_any()[1].getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(1).getFirstChild().getNodeValue());
// WEBサービスを呼び出す処理结果 = False の场合、エラーとする。
if(AppConstants.WS_FALSE.equals(result.get_any()[1].getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(0).getFirstChild().getNodeValue())) {
error.setErrorMsg(error.getErrorMsg("RC_WS_FAILED"));
throw new ValidationException(error);
}
}
}
解释如下:
3.1 调用接口方法需要准备三个相应的参数,分别为:由要调用的WebServiec方法的形参的名和类型对组成的ArrayList(①),由要调用的WebServiec方法的实参组成ArrayList(②)和要调用的WebServiec方法名(③)。
3.2 返回结果只有boolean型的true或者false时,用result.get_any()[0].getValue()来取具体的返回值。
3.3 返回结果既有true或false还有message信息时,用result.get_any()[1].getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(0).getFirstChild().getNodeValue())来获取true或false的boolean值;用result.get_any()[1].getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(1).getFirstChild().getNodeValue())来获取message信息。
3.4 标注黄色的代码是本例作成时测试代码,在实装调用WebService时,不必加入这些代码。
--END--