来源:http://www.myexception.cn/web/952419.html
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>
二、项目打包并发布
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的版本支持)
<?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 (包括调用多参数,但参数方法)
1 实例①
<%@ 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直接调用,不推荐
下面仅贴出JS段代码,供参考。
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 ; }