选择合适的版本下载axis2 .
下载地址:http://axis.apache.org/axis2/java/core/.
需要的文件为:axis2-1.7.3-bin.zip 和 axis2-1.7.3-war.zip环境初始化.
(1).将axis2-1.7.3-war.zip解压.
(2).将文件axis2.war放在tomcat的…\webapps\下.
(3).启动tomcat,通过浏览器访问 http://127.0.0.1:8080/axis2.- 编写服务端代码.
MyService.java
public class MyService {
public String HelloWebService(String str){
//返回字符串
return "Hello WebService, " + str;
}
}
将MyService.java编译后的MyService.class 放在tomcat下的…\webapps\axis2\WEB-INF\pojo(如果没有pojo则创建)中.
4.查看service的URL等信息.
浏览器访问http://127.0.0.1:8080/axis2/services/MyService?wsdl.
可以看到service的url 为http://127.0.0.1:8080/axis2/services/MyService.
方法名是HelloWebService.
5.编写客户端代码.
用到的jar包:http://download.csdn.net/detail/sinat_18831033/9554741.
ServiceClient.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;
public class ServiceClient {
public static void main(String[] args) {
getString("I Coming !");
}
public static void getString(String str){
try {
//使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://127.0.0.1:8086/axis2/services/MyService");
options.setTo(targetEPR);
// 指定参数值
Object[] opAddEntryArgs =
new Object[] {str};
// 指定返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://ws.apache.org/axis2", "HelloWebService");
// 调用方法并输出该方法的返回值
String reStr = serviceClient.invokeBlocking(
opAddEntry, opAddEntryArgs, classes)[0].toString();
System.out.println(reStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行后输出:Hello WebService, I Coming !
6.参考博客.
(1) http://www.cnblogs.com/samsongbest/archive/2011/10/19/2218719.html.
(2) http://www.voidcn.com/article/p-wvztugvh-bcu.html.