Axis2服务端与客户端程序

本文介绍了一种使用Axis2实现的WebService调用示例,包括服务端提供的Hello服务和加法服务,以及客户端如何通过Axis2客户端库进行调用的过程。

仿照搜索到的方法

服务端 WSDLTest.java

 /** 
	     * 提供了一个说Hello的服务 
	     * @return 
	     */  
	    public String sayHello(String name){  
	        return "Hello "+name;  
	    }  
	      
	    /** 
	     * 提供了一个做加法的服务 
	     * @param a 
	     * @param b 
	     * @return 
	     */  
	    public int add(int a,int b){  
	        return a + b;  
	    }  

客户端程序 

Axis2WBClient.java

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;

public class Axis2WBClient {

	public static void main(String[] args) {

		String xmlStr = "xiao.liu";
		String url = "http://localhost:8080/WSDL_Test/services/uimWBService";
		String method = "sayHello";
		Axis2WBClient.sendService(xmlStr, url, method);
	}

	@Test
	public void testOne() {
		try {

			// 使用RPC方式调用WebService
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			// 指定调用WebService的URL
			EndpointReference targetEPR = new EndpointReference(
					"http://localhost:8080/WSDL_Test/services/uimWBService");
			options.setTo(targetEPR);
			// 指定sayHelloToPerson方法的参数值
			Object[] opAddEntryArgs = new Object[] { 1,100 };
			// 指定sayHelloToPerson方法返回值的数据类型的Class对象
			Class[] classes = new Class[] { String.class };
			// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
			QName opAddEntry = new QName("http://wsdl.founder.com",
					"add");
			// 调用sayHelloToPerson方法并输出该方法的返回值
			System.out.println(serviceClient.invokeBlocking(opAddEntry,
					opAddEntryArgs, classes)[0]);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	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://wsdl.founder.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];
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(xml);
		return xml;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值