调用webService学习小结

  这段时间项目进行到了最后时刻,但是还有很多需求没有搞清楚,眼看deadline越来越近,压力也越来越大。现在我的主要工作是将别人开发好的一个系统给加载到我们系统中,使用的方法是通过webService调用那个系统的登录接口。我对webService这个技术知之甚少,所以,这段时间通过各种方法狂补webService知识,刚开始无头苍蝇似的乱撞,看到什么有关的就开始学,只是凭着脑子在想着学,没有动手,导致越学越糊涂。这周刚开始的时候仔细想了想前段时间学习方式,感觉不太对,于是准备从新开始,从发布一个本地webservice服务,通过调用发布成功的本地webService进行测试,测试好多种调用方式,总算是会调用webService服务了。在这里我想说之前漫无目的的学不是一点用也没有,还算是了解了一些webService的知识,毕竟学了那么长时间,怎么会没有收获。下面将调用webService接口的方法记录下来,便于以后复习。

  在网上查了好多资料,很多人分享的调用webService知识都很不错,我也是根据这些学会的webService,大致上如下:  

  1、通过axis实现发布本地webService并调用。

  发布本地webService服务:首先新建一个web项目(一般都是动态的),在src下新建一个java类,作为webService服务类,我建的类如下:

package com.lsk.ws;

public class PrintService {
	
	public void print(String str){
		
		System.out.println("Hello,this is my first webservice!");
		System.out.println(str);
		
	}
	
	public String lsk(String params1,String params2,String params3){
		
		String lskResult = params1 + "&" + params2 + "&" + params3;
		return lskResult;
		
	}
}

  print方法是为了测试无返回值但有打印的webService发放返回结果,结果什么都不返回。lsk这个方法测试返回正常结果。建好这个文件之后,然后右击选择webService 点击creat webService,然后的操作可以在网上找到,在这里不再赘述。

  调用发布的这个本地webService服务:在任意地方写个测试类,内容如下:

String endpoint = "http://localhost:8080/WebService/services/PrintService";
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);
            
            QName qn = new QName("urn:PrintService", "lsk");
            call.setOperationName(qn);
            String result = (String) call.invoke(new Object[] {"111","222","333"}); // 传参
            System.out.println(result);
            
            /*QName qn = new QName("urn:PrintService", "print");
            call.setOperationName(qn);
            call.invoke(new Object[] {"111"}); // 传参*/            
        } catch (Exception e) {
            e.printStackTrace();
        }

 

  上述测试方法用的是axis1.4,测试之前导入axis1.4的jar包。

  2、通过xFire调用webService,还以本机发布的webService为例。

  新建java project,首先导入xFirejar包,然后新建一个interface,该接口只需要生命需要调用的webService方法,如:

package com.lsk.webClient;

public interface IPrint {
    public void print(String str);
    public String lsk(String x1,String x2,String x3);
}

 

  然后,写个测试类和方法测试调用webService,如下:

Service srModel = new ObjectServiceFactory().create(IPrint.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());//创建工厂实例
        String helloURL = "http://localhost:8080/WebService/services/PrintService";
        try {
            IPrint IPrintService = (IPrint) factory.create(srModel, helloURL);
            String params1 = "mmm";
            String params2 = "nnn";
            String params3 = "ddd";
            IPrintService.print("哈哈哈哈哈哈");
            System.out.print(IPrintService.lsk(params1, params2, params3));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

  这是xFire调用webService的一种方法,我觉得这种方法与wsdl2java生成客户端类似。

  下面说一种调用网络上免费的webService服务的方法:

public static void main(String[] args) throws Exception {
        
        String endpoint = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";          
          
        String operationName = "getWeatherbyCityName";
          
        Service service = new Service();
         
        Call call = (Call) service.createCall();
          
        call.setTargetEndpointAddress(endpoint);
           
        call.setOperationName(new QName("http://WebXml.com.cn/",operationName)); 
           
        call.addParameter( new QName("http://WebXml.com.cn/","theCityName"),
        org.apache.axis.encoding.XMLType.XSD_STRING,
        javax.xml.rpc.ParameterMode.IN);
        
        
        call.setReturnClass(java.lang.String[].class);  
          
        call.setUseSOAPAction(true);   
        call.setSOAPActionURI("http://WebXml.com.cn/"+"getWeatherbyCityName");
        
        String[] res = null;
        res=(String[]) call.invoke(new Object[]{"郑州"});   
        
         
        for(String str:res){
            System.out.println(str);
        }
    }

  至此,对调用webService基本上算是了解了,还不能说精通,还需继续努力,人毕竟需要时刻进步,何况我们伟大的攻城狮。

posted @ 2014-10-15 18:57  静心远行  阅读(7617)  评论(0编辑  收藏  举报