调用webService学习小结

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

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

  一、经过axis实现发布本地webService并调用。apache

  发布本地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包。this

  二、经过xFire调用webService,还以本机发布的webService为例。spa

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

package com.lsk.webClient;

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

 

  而后,写个测试类和方法测试调用webService,以下:orm

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基本上算是了解了,还不能说精通,还需继续努力,人毕竟须要时刻进步,况且咱们伟大的攻城狮。

相关文章
相关标签/搜索