axis2
下载axis2-1.5.4-bin.zip文件并解压。
设置axis2的环境变量,如下图所示:
利用axis2中的wsdl2java.bat生成客户端程序。
先启动Tomcat7并在IE里运行http://localhost:8081/TestAxis2Ws/services/HelloService?wsdl 看看服务端是否正常解析。
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8081/TestAxis2Ws/services/HelloService?wsdl -paxis2.service.server -o stub
其中-uri参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。在执行完上面的命令后,读者就会发现在当前目录下多了个stub目录,可以找到一个HelloServiceStub.java文件,该文件复杂调用WebService,读者可以在程序中直接使用这个类。
运行-->cmd-->具体命令如下:
要先进入axis2的bin目录然后运行wsdl2java.bat
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Jambhala>d:
D:\>cd D:\WebService\Axis\axis2\axis2-1.5.4-bin\axis2-1.5.4\bin
D:\WebService\Axis\axis2\axis2-1.5.4-bin\axis2-1.5.4\bin>wsdl2java -uri http://l
ocalhost:8081/TestAxis2Ws/services/HelloService?wsdl -p axis2.service.server -o
stub
Using AXIS2_HOME: D:\WebService\Axis\axis2\axis2-1.5.4-bin\axis2-1.5.4
Using JAVA_HOME: C:\Java\jdk1.6.0
Retrieving document at 'http://localhost:8081/TestAxis2Ws/services/HelloService?
wsdl'.
D:\WebService\Axis\axis2\axis2-1.5.4-bin\axis2-1.5.4\bin>
这时到axis2的bin目录下会发现出现了一个stub目录,如下图所示:
其中HelloServiceStub.java是客户端程序,HelloServiceCallbackHandler.java是异步客户端程序。
把这两个类复制到上次已经建立好的服务端工程里,如下图所示:
要参考上一次写的MyEclipse整合Axis2插件的文章(http://shihuan830619.iteye.com/blog/1136607)
注意:这个地方复制进来后会报错的,因为在D:\WebService\Axis\axis2\axis2-1.5.4-bin\axis2-1.5.4\bin\stub\src\axis2\service\server下的类路径为package axis2.service.server;需要改成packageaxis2.service.client;并且需要把HelloServiceStub.java和HelloServiceCallbackHandler.java相关的报错位置都把路径改一下,不要闲烦,要改的地方还满多的呢。
创建带main方法的StubClient.java类文件,代码如下:
package axis2.service.domain;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import axis2.service.client.HelloServiceStub;
public class StubClient {
public static void main(String[] args) {
HelloServiceStub stub = null;
try {
stub = new HelloServiceStub();
HelloServiceStub.SayHello sh = new HelloServiceStub.SayHello();
sh.setName("于士博");
System.out.println(stub.sayHello(sh).get_return());
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
运行后会看到如下结果:
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
Hello, 于士博!
解释:
上面的代码大大简化了调用WebService的步骤,并使代码更加简洁。但要注意的是,wsdl2java.bat命令生成的Stub类将WebService方法的参数都封装在了相应的类中,类名(SayHello)为方法名,例如,sayHello方法的参数都封装在了SayHello类中,要想调用sayHello方法,必须先创建SayHello类的对象实例。
--------------------------------------------------------------------------------------------------
下面介绍纯手动编写Java代码客户端程序:
package axis2.service.domain;
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 {
public static void main(String[] args) {
//使用RPC方式调用WebService
RPCServiceClient serviceClient = null;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用WebService的URL
EndpointReference targetEPR = newEndpointReference("http://localhost:8081/TestAxis2Ws/services/HelloService");
options.setTo(targetEPR);
//指定sayHello方法的参数值
Object[] opAddEntryArgs = new Object[] {"于士博"};
//指定sayHello方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
//指定要调用的sayHello方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://server.service.axis2", "sayHello");
//调用sayHello方法并输出该方法的返回值
String result = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)
[0].toString();
serviceClient.cleanupTransport(); //为了防止连接超时
System.out.println(result);
//System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)
[0]);
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
【注】:在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;第二个
参数表示要调用的WebService方法的参数值,参数类型为Object[];第三个参数表示WebService方法的
返回值类型的Class对象,参数类型为Class[]。
当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,该方法只
有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是
<wsdl:definitions>元素的targetNamespace属性值。
axis2的更多相关文章
- axis2+struts拦截地址冲突问题
axis2和struts在整合过程中,struts会把axis的地址也拦截了,默认当成一个action处理, 会因为找不到action而报错: <!-- struts配置 --> < ...
- axis2+spring集成
转载自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html 1.新建一个web project项目,最终工程目录如下: 注意 ...
- eclispe+axis2+webservice入门
配置eclipse相关插件并启动axis2: http://blog.csdn.net/pilihaotian/article/details/52513697 这里你可能会遇到使用eclipse 启 ...
- axis2 调用webservice
maven配置:主要引用包及plugins <properties> <axis2.version>1.6.1</axis2.version> </prope ...
- axis2开发webservice程序
一.环境 eclipse + jdk 6.0 + win7 64位 +tomcat7.0 二.创建服务端程序 1.新建web项目,webserviceTest 2.下载axis2,将lib目录下的ja ...
- Java借助axis2发布WebService
Webservice: 1.Xml: 2.WSDL: Web service描述语言(WSDL)就是这样一个基于XML(标准通用标记语言下的一个子集)的语言,用于描述Web service及其函数.参 ...
- 使用axis2 soapmonitor监控soap数据
Using the SOAP Monitor SOAPMonitor使用 使用SOAPMonitor axis2开发笔记-消息监控 WebService大讲堂之Axis2(10):使用soapmoni ...
- webservice发布服务:AXIS2及客户端调用
1.Axis2: 到官网下载axis2的压缩包. 解压后: 1.将lib文件下的jar包复制到项目中 2.在web-inf下创建services->META-INF->services.x ...
- axis2打包方式发布
参照http://gao-xianglong.iteye.com/blog/1744557这篇文章,注意的是打包services.xml的时候要将它的上级目录meta-inf一起打包,放到axis2\ ...
- java 调用axis2 webservice
import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apach ...
随机推荐
- JSP九大内置对象及其方法
内置对象包括 request response pageContext session application out config page exception 1.out (1)clear()清除 ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- Netscaler重置密码的方法
Netscaler重置密码的方法 http://blog.51cto.com/caojin/1898401 有时候我们会碰到忘记Netscaler的密码,或接手别人的设备而不知道密码的情况.在这种情况 ...
- 【题解】HNOI2016树
大概最近写的这些题目都是仿生的代码……在这里先说明一下.可能比起做题记录来说更加像是学习笔记吧.之所以这样做主要还是因为感受到最近做的很多题目自己会做的都比较简单,不会做的又不敢触及,虽然也有所进步. ...
- 【模拟赛·polyline】
Input file: polyline.in Output file: polyline.out Time limit: 1s Memory limit: 128M 有若⼲个类似于下⾯的函数: 定义 ...
- 原生ajax方法封装
/** * @function ajax request * @fields ajaxName:请求名称,method:请求方法,headers:setRequestHeader自定义部分,url:接 ...
- im4java学习----查看文档和test用例
im4java下载地址:http://sourceforge.net/projects/im4java/files/(谷歌搜索出来的第一个官方地址打不开) 我们需要下载bin和src 这2个压缩包. ...
- 创建 React 项目
依次输入命令: npm install -g create-react-app create-react-app react16 cd my-app npm start 在浏览器中输入 local:3 ...
- Spring MVC框架下 从后台读取数据库并显示在前台页面【笔记自用 不推荐作为参考】
1.书写jsp页面 people.jsp 1.设计显示格式以及内容显示 2.设计显示内容的范围 2.书写entity实体类 PeopleFormMap.java 书写传入的参数主要包括 要引用的数据 ...
- [USACO1.3]虫洞
Luogu链接 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他 ...