RPC方式调用axis2发布的webservice服务

2 篇文章 0 订阅
1 篇文章 0 订阅

1.在前Axis2发布及调用WebService简单案例基础上进行

2.编写2个简单的服务类HiService.java、HelloService.java

package com.szcatic.service;

/**
 * http://localhost:8088/axis2-service/services/hiService?wsdl
 * @author zsx
 *
 */
public class HiService {
	
	public String sayHi(String name) {
		if (name != null) {
			System.out.println(name);
		}
		return "hi, " + name;
	}

	public void printContent(String content) {
		System.out.println("调用HiService的方法打印:" + content);
	}
}

 

package com.szcatic.service;

/**
 * http://localhost:8088/axis2-service/services/HelloService?wsdl
 * @author zsx
 *
 */
public class HelloService {
	
	public String sayHello(String name) {
		if (name != null) {
			System.out.println(name);
		}
		return "hello, " + name;
	}

	public void printContent(String content) {
		System.out.println("调用HelloService的方法打印:" + content);
	}
}

3.在/axis2-service/src/main/webapp/WEB-INF/services/rpc/META-INF目录下添加services.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<serviceGroup>
	<service name="hiService" scope="application">
	    <description>
	       	一个简单的Service
	    </description>
	    <messageReceivers>
	        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
	    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
	        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out"
	    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
	    </messageReceivers>
	    <parameter name="ServiceClass">com.szcatic.service.HiService</parameter>
	</service>
	<service name="helloService" scope="application" >
	    <description>
	       	下一个简单的Service
	    </description>
	    <operation name="getName">
	        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
	    </operation>
	    <operation name="printContent">
	        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
	    </operation>
	    <parameter name="ServiceClass">com.szcatic.service.HelloService</parameter>
	</service>
</serviceGroup>

4.把项目添加到tomcat,启动tomcat服务

5.打开浏览器,输入链接

5.1http://localhost:8088/axis2-service/services/hiService?wsdl

5.2http://localhost:8088/axis2-service/services/helloService?wsdl

从以上结果看出服务发布成功

6.编写客户端HiClient.java、HelloClient.java

package com.szcatic.client;

import javax.xml.namespace.QName;

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

public class HiClient {
	
	private static String url = "http://localhost:8088/axis2-service/services/hiService";
	
	public static void main(String[] args) {
		testSayHi();
	}
	
	private static void testSayHi() {
		try {
			RPCServiceClient client = new RPCServiceClient();
			Options options = client.getOptions();
			System.out.println("======sayHi========");
			EndpointReference endpoint = new EndpointReference(url);
			options.setTo(endpoint);
			QName qname = new QName("http://service.szcatic.com", "sayHi");
			Object[] methodArgs = new Object[] { "Tom" };
			Class<?>[] returnType = new Class[] { String.class };
			String result = (String) client.invokeBlocking(qname, methodArgs, returnType)[0];
			System.out.println(result);
		} catch (AxisFault e) {
			e.printStackTrace();
		}
	}
	
}
package com.szcatic.client;

import javax.xml.namespace.QName;

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

public class HelloClient {
	
	private static String url = "http://localhost:8088/axis2-service/services/helloService";
	
	public static void main(String[] args) {
		testSayHello();
	}
	
	private static void testSayHello() {
		try {
			RPCServiceClient client = new RPCServiceClient();
			Options options = client.getOptions();
			System.out.println("======sayHello========");
			EndpointReference endpoint = new EndpointReference(url);
			options.setTo(endpoint);
			QName qname = new QName("http://service.szcatic.com", "sayHello");
			Object[] methodArgs = new Object[] { "Tom" };
			Class<?>[] returnType = new Class[] { String.class };
			String result = (String) client.invokeBlocking(qname, methodArgs, returnType)[0];
			System.out.println("result:" + result);
		} catch (AxisFault e) {
			e.printStackTrace();
		}
	}
}

 

7.分别运行客户端HiClient.java、HelloClient.java

log4j:WARN No appenders could be found for logger (org.apache.axis2.context.AbstractContext).
log4j:WARN Please initialize the log4j system properly.
======sayHi========
hi, Tom
log4j:WARN No appenders could be found for logger (org.apache.axis2.context.AbstractContext).
log4j:WARN Please initialize the log4j system properly.
======sayHello========
result:hello, Tom

 从输出结果看出客户端调用服务成功

8.项目目录结构

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值