客户端实现WebService服务接口

  首先,要获得搭建好的WebService服务的WSDL,如要实现国内手机号码归属地查询WEB服务,其WSDL为:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL

  拿到WSDL后在浏览器打开看服务是否正常:

   

第一种:

  打开cmd,运行命令:

wsimport -p com -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL

  找到解析位置,复制相关Java文件到客户端:

 

  写测试类,直接调用相关接口:

import mobileWebService.MobileCodeWS;
import mobileWebService.MobileCodeWSSoap;

public class MobileWebServiceTest {

    public static void main(String[] args) {
        //获取webservice服务器对象(这里的类名,其实是从wsdl文件中的servicename标签中进行获取的)
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap m = mobileCodeWS.getMobileCodeWSSoap();
        String str = m.getMobileCodeInfo("手机号码", "");
        System.out.println(str);
    }
}

  运行结果:

 

第二种: 

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


/**
 * 基于cxf客户端调用WebService测试
 */
public class WebServiceClient {

    public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL");
        Object[] result = client.invoke("getMobileCodeInfo", "手机号码", "");
        System.out.println(result[0]);
    }
}

 

  WebService服务遇到问题及解决办法:https://www.cnblogs.com/Big-Boss/p/11912679.html

  

posted @ 2019-11-29 10:50  ヤBig、Bossづ  阅读(222)  评论(0编辑  收藏  举报