axis2 webservice入门学识(JS,Java,PHP调用实例源码)
来源:http://www.myexception.cn/web/952419.html
?
axis2 webservice入门知识(JS,Java,PHP调用实例源码)
背景简介 最近接触到一个银行接口的案子,临时需要用到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.1bin添加到path现有值的最后面; ③将$AXIS2_HOME$lib添加到系统环境变量classpath里。例如:将D:axis2-1.6.1lib添加到classpath现有值的最后面。 3. 把WAR Distribution 解压到 $tomcat_home$webappsaxis2下(新建axis2文件夹),当然你也可以参照axis2文档里列出的步骤使用ant 创建一个axis2.war ,放到$tomcat_home$webapps下,然后启动tomcat ,那么tomcat会在webapps下自动创建一个axis2文件夹。 二、测试Axis2环境. 1.访问 http://localhost:[port]/axis2 (请将[port]修改成你的Tomcat对应端口,默认为8080);进入axis2的欢迎界面了。点击“Validate”。 如果有报错,则需根据错误信息检查上述步骤。如果没有错误信息,那么Axis2的环境测试算是通过了。 2. 可以点击“Administration” 并使用初始用户名和密码:admin ;axis2登录,可以看到System Components以及可以使用Upload Service Tools。部署新的arr文件了。另可去$tomcat_home$webappsaxis2WEB-INFconfaxis2.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 <?xml version="1.0" encoding="UTF-8"?> <service name="HelloWorld"> <description> This is a sample Web Service. </description> <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> </service> 二、项目打包并发布 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(); } } } 编译此文件,并执行。 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 $wsdl='http://www.sietoo.com/axis2/services/SVAMobileWebService?wsdl'; $soap=new SoapClient($wsdl,array( 'trace'=>false,'cache_wsdl'=>WSDL_CACHE_NONE ) ); $soap=new SoapClient($wsdl); $method="bandMobileNo"; if(isset($_POST['passwd'])&&isset($_POST['UserId'])&&isset($_POST['bindingBank'])){ $params=array( 'UserId'=>$_POST['UserId'],'password'=>$_POST['passwd'],'bindingBank'=>$_POST['bindingBank']); try{ $result=$soap->$method($params); echo$result->return; echo'<br>'; }catch(SoapFault $e){echo $e->getMessage();} } ?> <html> <head> <title>bandMobileNo</title> </head> <body> <form method="Post" action=""> tel. <input type="text" name="UserId" value="18629078888"/> pwd. <input type="password" name="passwd" value="admin" /> cardno. <input type="text" name="bindingBank" value="622260062001991159"/> <input type="submit" name="submit" value="Submit"/> </form> </body> </html> 2 使用Nusoap调用 (需要下载nusoap.php附下载地址:http://download.csdn.net/detail/mr_z_andy/3845711) <? /*************************************************************/ /* 文件名 : soapclient.php /* 说 明 : WebService接口客户端例程 /* 作 者 :www.sietoo.com /*************************************************************/ include ('NuSoap.php'); // 创建一个soapclient对象,参数是server的WSDL $client = new soapclient ( 'http://localhost/Webservices/Service.asmx?WSDL','wsdl' ); // 参数转为数组形式传递 $aryPara = array ('strUsername' => 'username','strPassword' => MD5 ( 'password' ) ); // 调用远程函数 $aryResult = $client->call ( 'login',$aryPara ); //echo $client->debug_str; /* if (!$err=$client->getError()) { print_r($aryResult); } else { print "ERROR: $err"; } */ $document = $client->document; echo <<<SoapDocument <?xml version="1.0" encoding="GB2312"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> <SOAP-ENV:Body> $document </SOAP-ENV:Body> </SOAP-ENV:Envelope> SoapDocument; ?> ②代理调用 <? /*************************************************************/ /* 文件名 : soapclient.php /* 说 明 : WebService接口客户端例程 /* 作 者 :www.sietoo.com /*************************************************************/ require ('NuSoap.php'); //创建一个soapclient对象,参数是server的WSDL $client = new soapclient ( 'http://localhost/Webservices/Service.asmx?WSDL','wsdl' ); //生成proxy类 $proxy = $client->getProxy (); //调用远程函数 $aryResult = $proxy->login ( 'username',MD5 ( 'password' ) ); //echo $client->debug_str; /* if (!$err=$proxy->getError()) { print_r($aryResult); } else { print "ERROR: $err"; } */ $document = $proxy->document; echo <<<SoapDocument <?xml version="1.0" encoding="GB2312"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> <SOAP-ENV:Body> $document </SOAP-ENV:Body> </SOAP-ENV:Envelope> SoapDocument; ?> 三、JS客户调用axis2 webservice (包括调用多参数,但参数方法) <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function RequestWebService() { //这是我们在第一步中创建的Web服务的地址 var URL = "http://localhost/YBWS/WebService.asmx"; //在这处我们拼接 var data; data = '<?xml version="1.0" encoding="utf-8"?>'; data = data + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'; data = data + '<soap12:Body>'; data = data + '<HelloWorld xmlns="http://tempuri.org/" />'; data = data + '</soap12:Body>'; data = data + '</soap12:Envelope>'; //创建异步对象 var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.Open("POST",URL,false); xmlhttp.SetRequestHeader("Content-Type","application/soap+xml"); xmlhttp.Send(data); document.getElementById("data").innerHTML = xmlhttp.responseText; } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="One" type="button" value="JsCallWebService" onclick="RequestWebService()" /> </div> <div id="data"> </div> </form> </body> </html> 2实例② Ajax直接调用,不推荐 var xmlHttp = null; var bankno=null; var telno=null; var checkid=false; function createXMLHttpRequest() { if(window.XMLHttpRequest){ //Firefox,Mozillar,Opera,Safari,IE7,IE8 xmlHttp = new XMLHttpRequest(); //Mozillar浏览器的BUG进行修正的 if(xmlHttp.overrideMimeType){ xmlHttp.overrideMimeType("text/html"); } }else if(window.ActiveXObject){ //针对IE的各种版本 var versions = [ 'Microsoft.XMLHTTP','MSXML.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP' ]; //尝试创建XMLHttpRequest对象 for ( var i = 0; i < versions.length; i++) { try { xmlHttp = new ActiveXObject(versions[i]); break; } catch (e) { } } } } function AsynRequest() { createXMLHttpRequest(); if (xmlHttp == null) { alert("不能创建 XmlHttpRequest 对象"); return ; } xmlHttp.open("GET","http://www.sietoo.com/axis2/services/demoService/doTest?UserId="+telno+"&bindingBank="+"&bindingBank="+bankno,false); xmlHttp.setRequestHeader("Connection","close"); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { if(xmlHttp.responseXML==null) { return ; } var res2=xmlHttp.responseXML.getElementsByTagName("ns:return")[0].firstChild.nodeValue; //res2即为返回值。 return ; } } } } xmlHttp.send(); return ; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |