java-工具-Webservice wsdl解析

原文链接:http://www.cnblogs.com/coshaho/p/5689738.html
wsdl解析

首先必然是理解第三方webservice的接口描述,也就是解析wsdl文件。wsdl文件是webservice服务接口描述文档,一个wsdl文件可以包含多个接口,一个接口可以包含多个方法。

public class WsdlInfo 
{
    private String wsdlName;

    private List<InterfaceInfo> interfaces;

    /**
     * coshaho
     * @param path  wsdl地址
     * @throws Exception
     */
    public WsdlInfo(String path) throws Exception
    {
        WProject project = new WProject();
        WsdlInterface[] wsdlInterfaces = WsdlImporter.importWsdl( project, path );
        this.wsdlName = path;
        if(null != wsdlInterfaces)
        {    
            List<InterfaceInfo> interfaces = new ArrayList<InterfaceInfo>();
            for(WsdlInterface wsdlInterface : wsdlInterfaces)
            {
                InterfaceInfo interfaceInfo = new InterfaceInfo(wsdlInterface);
                interfaces.add(interfaceInfo);
            }
            this.interfaces = interfaces;
        }
    }

    public String getWsdlName() {
        return wsdlName;
    }

    public void setWsdlName(String wsdlName) {
        this.wsdlName = wsdlName;
    }

    public List<InterfaceInfo> getInterfaces() {
        return interfaces;
    }

    public void setInterfaces(List<InterfaceInfo> interfaces) {
        this.interfaces = interfaces;
    }
}
public class InterfaceInfo 
{
    private String interfaceName;

    private List<OperationInfo> operations;

    private String[] adrress;

    public InterfaceInfo(WsdlInterface wsdlInterface)
    {
        this.interfaceName = wsdlInterface.getName();

        this.adrress = wsdlInterface.getEndpoints();

        int operationNum = wsdlInterface.getOperationCount();
        List<OperationInfo> operations = new ArrayList<OperationInfo>();

        for(int i = 0; i < operationNum; i++)
        {
            WsdlOperation operation = ( WsdlOperation )wsdlInterface.getOperationAt( i );
            OperationInfo operationInfo = new OperationInfo(operation);
            operations.add(operationInfo);
        }

        this.operations = operations;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public List<OperationInfo> getOperations() {
        return operations;
    }

    public void setOperations(List<OperationInfo> operations) {
        this.operations = operations;
    }

    public String[] getAdrress() {
        return adrress;
    }

    public void setAdrress(String[] adrress) {
        this.adrress = adrress;
    }
}
public class OperationInfo 
{
    private String operationName;

    private String requestXml;

    private String responseXml;

    public OperationInfo(WsdlOperation operation)
    {
        operationName = operation.getName();
        requestXml = operation.createRequest( true );
        responseXml = operation.createResponse(true);    
    }

    public String getOperationName() {
        return operationName;
    }

    public void setOperationName(String operationName) {
        this.operationName = operationName;
    }

    public String getRequestXml() {
        return requestXml;
    }

    public void setRequestXml(String requestXml) {
        this.requestXml = requestXml;
    }

    public String getResponseXml() {
        return responseXml;
    }

    public void setResponseXml(String responseXml) {
        this.responseXml = responseXml;
    }
}
public class WSDLParseTest 
{
    public static void main(String[] args) throws Exception
    {
        String url = "http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl";
        WsdlInfo wsdlInfo = new WsdlInfo(url);
        System.out.println("WSDL URL is " + wsdlInfo.getWsdlName());

        for(InterfaceInfo interfaceInfo : wsdlInfo.getInterfaces())
        {
            System.out.println("Interface name is " + interfaceInfo.getInterfaceName());
            for(String ads : interfaceInfo.getAdrress())
            {
                System.out.println("Interface address is " + ads);
            }
            for(OperationInfo operation : interfaceInfo.getOperations())
            {
                System.out.println("operation name is " + operation.getOperationName());
                System.out.println("operation request is ");
                System.out.println("operation request is " + operation.getRequestXml());
                System.out.println("operation response is ");
                System.out.println(operation.getResponseXml());
            }
        }
    }
}
WSDL URL is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl
Interface name is ChinaOpenFundWSSoap12
Interface address is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx
operation name is getFundCodeNameDataSet
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameDataSet/>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameDataSetResponse>
         <!--Optional:-->
         <web:getFundCodeNameDataSetResult>
            <xs:schema>
               <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
            </xs:schema>
            <!--You may enter ANY elements at this point-->
         </web:getFundCodeNameDataSetResult>
      </web:getFundCodeNameDataSetResponse>
   </soap:Body>
</soap:Envelope>
operation name is getFundCodeNameString
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameString/>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameStringResponse>
         <!--Optional:-->
         <web:getFundCodeNameStringResult>
            <!--Zero or more repetitions:-->
            <web:string>?</web:string>
         </web:getFundCodeNameStringResult>
      </web:getFundCodeNameStringResponse>
   </soap:Body>
</soap:Envelope>
operation name is getOpenFundDataSet
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getOpenFundDataSet>
         <!--Optional:-->
         <web:userID>?</web:userID>
      </web:getOpenFundDataSet>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soap:Header/>
   <soap:Body>
      <web:getOpenFundDataSetResponse>
         <!--Optional:-->
         <web:getOpenFundDataSetResult>
            <xs:schema>
               <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
            </xs:schema>
            <!--You may enter ANY elements at this point-->
         </web:getOpenFundDataSetResult>
      </web:getOpenFundDataSetResponse>
   </soap:Body>
</soap:Envelope>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值