根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例

接口应用名称:NgCallService

入参和出参信息

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例

入参和出参报文信息

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
入参:

<?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对象: NgCallServiceRequest

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
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

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
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

对象和报文的转换关系

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
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的客户端
根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
进入到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的action

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
<iframe src="${pageContext.request.contextPath}/myservice/NgCallServiceInfo.do" name="iframe" frameborder="0" width="730" height="290" class="">

</iframe>
View Code

Jsp页面中要访问的Struts1中的起始Action方法

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
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

调用接口输出的报文信息

根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
<?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

 

你可能感兴趣的:(webservice)