Web Services学习3——用Axis2发布Web Services的方法

Axis2+tomcat6.0 实现webService 服务端发布与客户端的调用

 

Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法。

第一步:首先要下载开发所需要的jar包

 

下载:axis2-1.6.1-war.zip

http://www.apache.org/dist//axis/axis2/java/core/1.6.1/

下载完后解压至tomcat安装目录下的webapps文件夹下,启动tomcat后,在webapps目录下会生成axis2文件夹。

访问http://localhost:8080/axis2/能看到以下页面表示axis2运行成功。

第二部

在Eclipse下新建Web Project,工程名:webServe。新建包com.cm.service,新建类HelloWorld,代码如下:

package com.cm.service;

public class HelloWorld {

	public String sayHello(String name){return "Hello, "+name+".";}
	public String saySorry(String name){return "Sorry,"+name+".";}
	public String getWorld(){return "Hello,World";}
	
}


按照“Web Services学习2”的方法实现自动发布,产生webServeClient工程

 

启动tomcat后访问http://127.0.0.1:8080/webServe/services/HelloWorld?wsdl能看到服务信息了。

到此Axis2的WebService服务已成功发布

 

Axis2客户端调用:

下面看看利用axis2 客户端调用实例

 

新建一个客户端调用类AxisUtil

代码如下:

package com.cm.client;


import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class AxisUtil {
	public static void main(String[] args) {
		
		String xmlStr="xiaoxu.wang";
		String url="http://127.0.0.1:8080/webServe/services/HelloWorld";
		String method="saySorry";
		AxisUtil.sendService(xmlStr,url,method);
	}
	public static String sendService(String xmlStr,String url,String method){
		String xml=null;
		try {
			
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference(url);
			options.setTo(targetEPR);
			// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
			 QName opAddEntry = new QName("http://service.cm.com",method);
			 // 参数,如果有多个,继续往后面增加即可,不用指定参数的名称
			 Object[] opAddEntryArgs = new Object[] {xmlStr};
			 // 返回参数类型,这个和axis1有点区别
			 // invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
			 // 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
			 // 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
			 // 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
			 // 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
			 // 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
			 Class[] classes = new Class[] { String.class };
			 xml=(String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
			 System.out.println(xml); 


		} catch (Exception e) {
			e.printStackTrace();
			long end = System.currentTimeMillis();
		}
		return xml;
	}
}


运行结果:

Sorry,xiaoxu.wang.

 

 

运用AXIOM方式的客户端调用

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
 
 
public class Test {
	
	private static EndpointReference targetEPR=new EndpointReference("http://127.0.0.1:8080/webServe/services/HelloWorld");
	public static void main(String[] args) {
		try{
			Options options=new Options();
			options.setTo(targetEPR);
			ServiceClient sender=new ServiceClient();
			sender.setOptions(options);
			options.setAction("urn:sayHello");
			
			//创建request SOAP包。 
			OMFactory fac=OMAbstractFactory.getOMFactory();  
			// OMNamespace指定此SOAP文档名称空间。
			OMNamespace omNs = fac.createOMNamespace("http://service.cm.com","tc");
			//创建调用方法元素sayHello,并指定其在omNs指代的名称空间中。
			OMElement methodSH = fac.createOMElement("sayHello",omNs);
			//创建sayHello的参数元素,指定其在omNs指代的名称空间。
			OMElement valueSH = fac.createOMElement("name", omNs);
			//指定元素的文本内容。
			valueSH.addChild(fac.createOMText(valueSH,"张三"));
			methodSH.addChild(valueSH);
			//发出request SOAP,
			//同时将得到的远端由sayHello方法返回的信息保存到result。
			//通过services.xml能准确找到sayHello方法所在的文件。
			OMElement result=sender.sendReceive(methodSH); 
			System.out.println(result);
			
		}catch(Exception axisFault){
			axisFault.printStackTrace();  
		}
	}
}

返回结果<ns:sayHelloResponse xmlns:ns="http://service.cm.com"><ns:return>Hello, 张三.</ns:return></ns:sayHelloResponse>

 

总结:以上就是Axis2 服务发布与调用的简单案例。至于其他的实现方法有兴趣的可以继续研究。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值