第一步
创建一个pojo类
package com.service;
public class SimpleService {
public String getGreeting(String name){
return "Hello,"+name+"service.xml";
}
}
第二步
创建services.xml并进行编辑
注意事项,假如带文字注释,请使用UTF或GBK编码
<!-- 使用service.xml发布ws服务-->
<!-- 服务端提供的服务 name:服务的名称,供客户端调用 -->
<service name="SimpleService">
<!-- 指定服务所对应的类名 -->
<parameter name="serviceClass">
com.service.SimpleService
</parameter>
<!-- 服务级消息接收器
Axis2中消息接收器是特殊的处理器,是In路径(请求路径)中的最后一个处理器。Web服务中的每个操作都有他自己的消息接收器,而且不同的操作可以有不同的消息接收器。消息接收器是依赖于消息交换模式的,所以我们必须为不同的消息交换模式指定不同的消息接收器。
怎样才能给所有的操作指定相同的消息接收器呢?只要添加服务级消息接收器即可。如此我们就不必在操作级别指定消息接收器了。我们要做的是指定服务级消息接收器。而在部署时,Axis2会自动给操作选择正确的消息接收器-->
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
</service>
第三步
在d盘下创建一个ws文件夹
将services.xml文件和编译后的SimpleService.class文件放入ws文件中
ws目录结构如下
D:\ws\com\service
D:\ws\META-INF\services.xml
第四步
将com文件和META-INF文件打成jar包
具体操作如下:
打开cmd输入命令d: 回车 输入命令cd ws 回车 输入命令jar cvf ws.jar com META-INF 回车
然后便会看到在ws文件下生成了一个ws.jar
第五步
将ws.jar复制到如下目录
D:\apache-tomcat-7.0.64\webapps\axis2\WEB-INF\services
如果没有axis2文件,请将axis2.war包放在tomcat\webapps下。在bin目录中点startup.bat运行tomcat.便会生成一个axis2文件
axis2.war来自于axis2-1.5.4-war.zip包。 下载地址:http://download.youkuaiyun.com/detail/wwgzj6736/7855659
第五步
在tomcat bin目录中点startup.bat命令运行tomcat
然后在浏览器中输入http://localhost:8080/axis2
如果出现如下页面
恭喜你,没错,你成功了。接下来点services找到你发布的服务名称点击便会看到一个wsdl文件。
第六步
需要jar:axis2-1.5.4-bin.zip 下载地址:http://download.youkuaiyun.com/detail/wwgzj6736/7855659
使用客户端调用服务端方法
客户端代码:
package com.test;
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 test {
public static void main(String[] args) throws AxisFault {
//使用RPC方式调用webservice
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用web service的URL
EndpointReference targetEPR = new EndpointReference(
"http://192.168.59.1/axis2/services/SimpleService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[] {"美女"};
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service.com", "getGreeting");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}
}
然后点击运行,控制台会输出以下结果:
log4j:WARN No appenders could be found for logger (org.apache.axis2.util.Loader).
log4j:WARN Please initialize the log4j system properly.
Hello,美女service.xml
可能出现的BUG
org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is /PQ/services/PQInfoService?wsdl and the WSA Action = null
解决方法:去掉services.xml中的注释