根據第三方提供的wsdl報文(axis2開發),進行的webservice應用的開發實例

接口應用名稱:NgCallService

入參和出參信息java

入參和出參報文信息

入參:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ROOT>
 <REQUEST_METHOD type="string">getVIPCardVIPGrade</REQUEST_METHOD>
 <REQUEST_INFO>
  <CONTENTID type="string">14</CONTENTID>
  <ID_NO type="string">13*203***66</ID_NO>
 </REQUEST_INFO>
</ROOT>
出參:
<ROOT>
 <RETURN_CODE type="long">0</RETURN_CODE>
 <RETURN_MSG type="string">成功!</RETURN_MSG>
 <DETAIL_MSG type="string">該用戶的使用次數查找成功!</DETAIL_MSG>
 <OUT_DATA>
  <USAVLE_NUMBER type="string">0</USAVLE_NUMBER>
  <VIP_NO type="string">0**301******3939</VIP_NO>
  <VIP_GRADE type="string">03</VIP_GRADE>
  <SERVICE_ACC>0</SERVICE_ACC>
 </OUT_DATA>
</ROOT>
View Code

封裝入參報文爲request對象: NgCallServiceRequestlinux

package com.sinovatech.service.NgCallService;
public class NgCallServiceRequest {
    private String requestMethod;// 請求服務方法
    private String contentId;// 服務類型(0:火車站服務 14:機場服務 2:SIM卡免費換卡服務)
    private String idNo;
. . . . . . . 
. . . . . . . 這裏沒有加上getter和setter方法
}
View Code

封裝出參報文爲response對象: NgCallService

package com.sinovatech.service.NgCallService;
public class NgCallServiceResponse {
    private String returnCode;// 執行結果信息編碼(0:成功,其餘編碼標識有錯誤,具體錯誤信息參照RETURN_MSG節點內容)
    private String returnMsg;// 執行結果信息內容
    private String detailMsg;// 執行結果詳細信息內容
    private String usavleNumber;// 剩餘次數
    private String vipNo;// vip用戶卡號
    private String vipGrade;// vip用戶等級
    private String serviceAcc;// 總次數
. . . . . . . 
. . . . . . . 這裏沒有加上getter和setter方法
}
View Code

對象和報文的轉換關係web

package com.si***ch.service;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.si***ch.service.NgCallService.NgCallServiceRequest;
import com.si***ch.service.NgCallService.NgCallServiceResponse;
import com.si***ch.service.s2299Query.BusiInfo;
import com.si***ch.service.s2299Query.S2299QueryRequest;
import com.si***ch.service.s2299Query.S2299QueryResponse;
import com.si***ch.service.sQUserBase.sQUserBaseRequest;
import com.si***ch.service.sQUserBase.sQUserBaseResponse;

public class MyParseXML {
    private static final Logger log = Logger.getLogger(MyParseXML.class);
    private static StringBuffer xmlHead =null;
    static{
        xmlHead = new StringBuffer();
        xmlHead.append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?>").append("\n");
        xmlHead.append("<ROOT>").append("\n");
    }
        
    public static String parseNgCallServiceRequest2XML(NgCallServiceRequest request){
        log.info("parseNgCallServiceRequest2XML=================start====================");
        StringBuffer xmlRequest = new StringBuffer();
        xmlRequest.append(xmlHead.toString());
        if(request.getRequestMethod()!=null){
            xmlRequest.append("<REQUEST_METHOD type=\"string\">"+request.getRequestMethod()+"</REQUEST_METHOD>").append("\n");
        }else{
            xmlRequest.append("<REQUEST_METHOD type=\"string\"></REQUEST_METHOD>").append("\n");
        }
        xmlRequest.append("<REQUEST_INFO>").append("\n");
        
        if(request.getContentId()!=null){
            xmlRequest.append("<CONTENTID type=\"string\">"+request.getContentId()+"</CONTENTID>").append("\n");
        }else{
            xmlRequest.append("<CONTENTID type=\"string\"></CONTENTID>").append("\n");
        }
        
        if(request.getIdNo()!=null){
            xmlRequest.append("<ID_NO type=\"string\">"+request.getIdNo()+"</ID_NO>").append("\n");
        }else{
            xmlRequest.append("<ID_NO type=\"string\"></ID_NO>").append("\n");
        }
        xmlRequest.append("</REQUEST_INFO>").append("\n").append("</ROOT>").append("\n");
        log.info("parseNgCallServiceRequest2XML=================end====================");
        return xmlRequest.toString();
    }
    
    /**
     * @throws DocumentException 
     * 
     */
    public static NgCallServiceResponse parseNgCallServiceResponse(String xml) throws DocumentException{
        log.info("parseNgCallServiceResponse=================start====================");
        Document doc = DocumentHelper.parseText(xml);
        NgCallServiceResponse response = new NgCallServiceResponse();
        Element root = doc.getRootElement();
        String returnCode = root.elementText("RETURN_CODE");
        String returnMsg = root.elementText("RETURN_MSG");
        String detailMsg = root.elementText("DETAIL_MSG");
        if(returnCode!=null&&(returnCode.equals("0")||returnCode.equals("0"))){
            Element outData = root.element("OUT_DATA");
            String usavleNumber = outData.elementText("USAVLE_NUMBER");
            String vipNo = outData.elementText("VIP_NO");
            String vipGrade = outData.elementText("VIP_GRADE");
            String serviceAcc = outData.elementText("SERVICE_ACC");
            response.setUsavleNumber(usavleNumber);
            response.setVipNo(vipNo);
            response.setVipGrade(vipGrade);
            response.setServiceAcc(serviceAcc);
        }
        response.setReturnCode(returnCode);
        response.setReturnMsg(returnMsg);
        response.setDetailMsg(detailMsg);
        log.info("parseNgCallServiceResponse=================end====================");
        return response;
    }
    
    public static void main(String[] args) throws DocumentException {
        
        //測試request轉爲xml
                
        /*NgCallServiceRequest request = new NgCallServiceRequest();
        request.setRequestMethod("getCustFreeAirNumVIPCardVIPGrade");
        request.setContentId("14");
        request.setIdNo("13120******");
        System.out.println(parseNgCallServiceRequest2XML(request));*/        
                
        StringBuffer response = new StringBuffer();
        response.append(xmlHead);
        response.append("<RETURN_CODE type=\"long\">0</RETURN_CODE>").append("<RETURN_MSG type=\"string\">成功!</RETURN_MSG>")
        .append("<DETAIL_MSG type=\"string\">該用戶的使用次數查找成功!</DETAIL_MSG>")
        .append("<OUT_DATA><USAVLE_NUMBER type=\"string\">0</USAVLE_NUMBER>").append("<VIP_NO type=\"string\">0413013******939</VIP_NO>")
        .append("<VIP_GRADE type=\"string\">03</VIP_GRADE>").append("<SERVICE_ACC type=\"string\">0</SERVICE_ACC>")
        .append("</OUT_DATA></ROOT>");
        NgCallServiceResponse res = parseNgCallServiceResponse(response.toString());
        StringBuffer out = new StringBuffer();
        out.append(res.getReturnCode()+"||"+res.getReturnMsg()+"||"+res.getDetailMsg()+"||"+res.getUsavleNumber()+"||"+res.getVipNo()+"||"+res.getVipGrade()+"||"+res.getServiceAcc());
        System.out.println(out.toString());
    }
}
View Code
根據wsdl的地址生成axis2的客戶端
進入到axis2安裝目錄的bin目錄

cd /app/weblogic/axis2-1.4.1/bin

執行命令生成axis2客戶端(linux環境)

./wsdl2java.sh

-uri http://10.*.*.*:5*00/esbWS/services/ NgCallService?wsdl -o /app/weblogic/ws-java/ NgCallService /

生成的客戶端以下:

NgCallServiceCallbackHandler.java  和  NgCallServiceStub.java
View Code

在jsp頁面中經過iframe調用struts1的actionapache

<iframe src="${pageContext.request.contextPath}/myservice/NgCallServiceInfo.do" name="iframe" frameborder="0" width="730" height="290" class="">
</iframe>
View Code

Jsp頁面中要訪問的Struts1中的起始Action方法app

public ActionForward NgCallServiceInfo(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    log.info("NgCallServiceAction  查看查詢參數開始..................");
    // 得到登陸用戶信息,沒有登陸提示用戶須要從新登陸
    UserBean user = (UserBean) request.getSession().getAttribute("USER_INFO");
    //這裏是作測試,寫一個固定的號碼:13682672166 
    NgCallServiceResponse ngCallServiceResponse1 = NgCallService("13682672166");
    /*if (user != null) {
        NgCallServiceResponse ngCallServiceResponse = NgCallService(user.getMsisdn());
        if(ngCallServiceResponse!=null){
        request.setAttribute("trainServiceAcc", ngCallServiceResponse.getServiceAcc());
        request.setAttribute("trainUsavleNumber", ngCallServiceResponse.getUsavleNumber());
    }
    }else {
    request.setAttribute("usernologin", "nologin");
    }
    log.info("NgCallServiceAction  查看查詢參數結束..................");*/
    if(ngCallServiceResponse1!=null){
    log.info("ngCallServiceResponse1.getDetailMsg()     "+ngCallServiceResponse1.getDetailMsg());
    log.info("ngCallServiceResponse1.getReturnCode()    "+ngCallServiceResponse1.getReturnCode());
    request.setAttribute("msg",ngCallServiceResponse1.getDetailMsg());
}
    return mapping.findForward("myservice");
}






public NgCallServiceResponse NgCallService(String idNo){
        NgCallServiceRequest request=new NgCallServiceRequest();
        request.setRequestMethod("getVIPCardVIPGrade");
        request.setContentId("14");   //0:火車站服務 14:機場服務  2:SIM卡免費換卡服務
        request.setIdNo(idNo);
        NgCallServiceResponse response = NgCallServiceInfo(request);
        //0時表示成功
        if(response != null && response.getReturnCode().equals("0")){
            return response;
        }
        return null;
    }

public  static NgCallServiceResponse NgCallServiceInfo(NgCallServiceRequest request){
        log.info("======查詢機場火車站sim卡剩餘次數的服務  NgCallServiceInfo start=========");
        String xml="";
        NgCallServiceResponse response = null;
//下面的語句是讀取配置文件中的axis2接口的url
        String requestUrl=GlobalConfig.getProperty("sys.boss","NgCallServiceURL");
        try {
        xml = MyParseXML.parseNgCallServiceRequest2XML(request);
        log.info("查詢機場火車站sim卡剩餘次數的服務(傳入參數):"+xml);
        log.info("requestUrl:"+requestUrl);
        NgCallServiceStub stub = new NgCallServiceStub(requestUrl);
        NgCallServiceStub.CallService call=new NgCallServiceStub.CallService();
        call.setPin(xml);
        
        com.sitech.ws.NgCallServiceStub.CallServiceResponse response2;
        response2 = stub.callService(call);
        xml=response2.get_return();
        log.info("查詢機場火車站sim卡剩餘次數的服務 接口(輸出結果):"+xml);
        response = MyParseXML.parseNgCallServiceResponse(xml);
        } catch (Exception e) {
            log.info(e);
        }
        log.info("======查詢機場火車站sim卡剩餘次數的服務 接口  NgCallServiceInfo end=========");
        return response;
    }
View Code

調用接口輸出的報文信息dom

<?xml version="1.0" encoding="GBK"?>
<ROOT>
<RETURN_CODE type="long">0</RETURN_CODE>
<RETURN_MSG type="string">成功!</RETURN_MSG>
<DETAIL_MSG type="string">該用戶的機場服務使用次數查找成功!</DETAIL_MSG>
<OUT_DATA>
    <USAVLE_NUMBER type="string">0</USAVLE_NUMBER>
    <VIP_NO type="string">0*130********688</VIP_NO>
    <VIP_GRADE type="string">02</VIP_GRADE>
</OUT_DATA>
</ROOT>
View Code
相關文章
相關標籤/搜索