Java調用.Net寫的Webservice

我們已經知道現在普遍存在兩種用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--    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章