背景简介
最近接触到一个银行接口的案子,临时需要用到axis2 webservice。自己现学现总结的一些东西,留给新手。少走弯路。
Axis2简介
①采用名为 AXIOM(AXIs Object Model)的新核心 XML 处理模型,利用新的XML解析器提供的灵活性按需构造对象模型。
②支持不同的消息交换模式。目前Axis2支持三种模式:In-Only、Robust-In和In-Out。In-Only消息交换模式只有SOAP请求,而不需要应答;Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。
③提供阻塞和非阻塞客户端 API。
④支持内置的 Web服务寻址 (WS-Addressing) 。
⑤灵活的数据绑定,可以选择直接使用 AXIOM,使用与原来的 Axis 相似的简单数据绑定方法,或使用 XMLBeans、JiBX 或 JAXB 2.0 等专用数据绑定框架。
⑥新的部署模型,支持热部署。
⑦支持HTTP,SMTP,JMS,TCP传输协议。
⑧支持REST (Representational State Transfer)。
测试环境
【jdk1.6.0】 +【tomcat-6.0.18】 + 【axis2-1.6.1】+【PHP Version 5.3.5】
未测试最低支持配置。
环境准备
一、部署Axis2环境.
1.下载安装
apache 官网下载地址:http://ws.apache.org/axis2/ 选择 Standard Binary Distribution 和 WAR Distribution
2.配置系统环境变量:
①添加AXIS2_HOME变量并指向 Standard Binary Distribution解压目标目录。例如:$AXIS2_HOME$ =D:\axis2-1.6.1;
②将axis2.bat所在目录添加到系统环境变量path里。例如:将 D:\axis2-1.6.1\bin添加到path现有值的最后面;
③将$AXIS2_HOME$\lib添加到系统环境变量classpath里。例如:将D:\axis2-1.6.1\lib添加到classpath现有值的最后面。
把WAR Distribution 解压到 $tomcat_home$\webapps\axis2下(新建axis2文件夹),当然你也可以参照axis2文档里列出的步骤使用ant 创建一个axis2.war ,放到$tomcat_home$\webapps下,然后启动tomcat ,那么tomcat会在webapps下自动创建一个axis2文件夹。
二、测试Axis2环境.
1.访问 http://localhost:[port]/axis2 (请将[port]修改成你的Tomcat对应端口,默认为8080);进入axis2的欢迎界面了。点击“Validate”。
如果有报错,则需根据错误信息检查上述步骤。如果没有错误信息,那么Axis2的环境测试算是通过了。
可以点击“Administration” 并使用初始用户名和密码:admin ;axis2登录,可以看到System Components以及可以使用Upload Service Tools。部署新的arr文件了。另可去$tomcat_home$\webapps\axis2\WEB-INF\conf\axis2.xml下修改用户名和密码。
创建Demo HelloWorld
一、service端开发
1.创建一个java项目
2.新建类HelloWorld.java
参考代码:
package sample;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HelloWorld {
public OMElement sayHello(OMElement in){
String name=in.getText();
String info="你好"+name+",给你推荐http://www.sietoo.com";
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://www.sietoo.com/","hw");
OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
resp.setText(info);
return resp;
}
}
3.新建文件META-INF \ services.xml
参考代码:
This is a sample Web Service.
sample.HelloWorld
二、项目打包并发布
1.可使用你熟悉的IDE进行打包成HelloWorld.aar
参考直接打包方式:
在命令符行境下,将当前目录切换到该项目包下。如博主的例子就需要切换到sample所在的文件夹,注意并非切换进sample。使用如下命令:jar cvf HelloWorld.aar . 完成在当前目录生成HelloWorld.aar 。请注意命令末尾的点“.”。
2.发布,使用前面提到的登录axis2后看到的Upload Service 工具 将HelloWorld.arr部署到Tomc上。
3.发布测试,如博主例子访问http://localhost:8088/axis2/services/HelloWorld?wsdl查看第2步骤中部署的HelloWrold的描述文件。
如果有报错,则需根据错误信息检查上述步骤。如果没有错误信息,那么HelloWorld的service端就算完成了。
三、简单客户端调用
1.一个简单的Java调用客户端。
参考代码:
package example.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class TestClient {
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://www.sietoo.com/","hw");
OMElement method=fac.createOMElement("sayHello",omNs);
method.setText("andy");
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=TestClient.getSayHelloOMElement();
OMElement result=sender.sendReceive(sayHello);
System.out.println(result);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
编译此文件,并执行。
如果有报错,则需根据错误信息检查上述步骤。如果没有错误信息,那么Demo HelloWorld就完满完成。
各类客户端调用实例
一、java调用axis2 webservice (包括单个参数和多个参数方法的调用)
参考代码:
package example.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class s2 {
private static EndpointReference targetEPR=new EndpointReference("http://www.sietoo.com/axis2/services/SVAMobileWebService");
public static OMElement getSayHelloOMElement(){
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://www.sietoo.com","andy");
//测试调用bandMobileNo (多参数方法)
OMElement bandMobileNo=fac.createOMElement("bandMobileNo",omNs);
OMElement UserId=fac.createOMElement("UserId",omNs);
OMElement password=fac.createOMElement("password",omNs);
OMElement bindingBank=fac.createOMElement("bindingBank",omNs);
UserId.addChild(fac.createOMText(UserId, "18629078140"));
password.addChild(fac.createOMText(password, "mynewpassword"));
bindingBank.addChild(fac.createOMText(bindingBank, "622260062001991159"));
bandMobileNo.addChild(UserId);
bandMobileNo.addChild(password);
bandMobileNo.addChild(bindingBank);
return bandMobileNo;
//测试调用getAccountInfo (单参数方法)
//OMElement getAccountInfo=fac.createOMElement("getAccountInfo",omNs);
//OMElement accountNum=fac.createOMElement("accountNum",omNs);
//accountNum.addChild(fac.createOMText(accountNum, "18629078140"));
//getAccountInfo.addChild(accountNum);
//return getAccountInfo;
}
public static void main(String args[]){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=s2.getSayHelloOMElement();
OMElement result=sender.sendReceive(sayHello);
System.out.println(result);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}}}
二、PHP调用axis2 webservice (包括调用多参数,但参数方法)
1.使用Soap调用(需要PHP的版本支持)
bandMobileNo
tel.
pwd.
cardno.
2 使用Nusoap调用 (需要下载nusoap.php附下载地址:http://download.csdn.net/detail/mr_z_andy/3845711)
分如下两种方式:
①直接调用
$document
SoapDocument;
?>
三、JS客户调用axis2 webservice (包括调用多参数,但参数方法)
1 实例①
function RequestWebService() {
//这是我们在第一步中创建的Web服务的地址
var URL = "http://localhost/YBWS/WebService.asmx";
//在这处我们拼接
var data;
data = '';
data = data + '
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com