Java调用.Net写的Webservice
来源:互联网 发布:如何登陆淘宝联盟 编辑:程序博客网 时间:2024/05/16 14:27
我们已经知道现在普遍存在两种用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--
- Java调用.Net写的Webservice
- NET调用Java写的WebService[转]
- asp.net调用java写的webservice
- .net写的WebService被Java调用
- .NET调用Java写的WebService[原]
- 主题:java 调用.net 写的WebService 问题求解!!!
- Delphi7调用vs.net写的WebService
- Java调用.net的WebService
- Java调用.net的WebService
- Java调用.NET的WebService
- java调用.net的 WebService
- java调用.net的webservice
- .Net调用Java的WebService
- java调用.net的webservice
- Java调用.net的WebService
- java调用.net的webservice
- java调用.net的webservice
- java调用c#写的webservice
- 论中国钢铁企业如何分步实施信息化一
- 如何中国的西联代理网点
- 我的技术博客--潇湘博客开通了~`````
- 中文参考手册2(MySQL的一般信息)
- 中文参考手册7(MySQL存取权限系统)grant设置密码password
- Java调用.Net写的Webservice
- 当SQLServer崩溃时如何恢复?
- 亚洲货币绞杀战
- SQLserver中用convertSQLserver中用convert函数转换日期格式函数转换日期格式
- SQL语句参考及记录集对象详解
- 网络编程
- 钢铁企业如何分步实施信息化
- 查询结果的动态排序(5)
- 计算机专业毕业了,还要不要参加培训班?