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.项目目录结构