ITPub博客

首页 > Linux操作系统 > Linux操作系统 > axis2 webservice入门知识(JS,Java,PHP调用实例源码)

axis2 webservice入门知识(JS,Java,PHP调用实例源码)

原创 Linux操作系统 作者:mr_z_andy 时间:2011-11-27 22:39:57 0 删除 编辑

背景简介

最近接触到一个银行接口的案子,临时需要用到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现有值的最后面。

 

3. 把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的环境测试算是通过了。

2. 可以点击“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 mNs=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 mNs=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 ptions=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 mNs=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 ptions=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的版本支持)

 

$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'
';

}catch(SoapFault $e){echo $e->getMessage();}

}

?>

  bandMobileNo

 

   

tel.

   

   

 

pwd.

   

   

   

cardno.

   

   

   

   

   

 

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 <<

 

   

   $document

   

 

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 <<

 

   

   $document

   

 

SoapDocument;

?>

 

三、JS客户调用axis2 webservice (包括调用多参数,但参数方法)

1 实例①

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

   

   

        function RequestWebService() {

            //这是我们在第一步中创建的Web服务的地址

            var URL = "http://localhost/YBWS/WebService.asmx";

            //在这处我们拼接

            var data;

            data = '';

            data = data + '';

            data = data + '';

            data = data + '';

            data = data + '';

            data = data + '';

            //创建异步对象

            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;

        }        

   

   

   

       

   

   

   

   

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  ;

}


请尊重版权,转载请保留出处:http://hi.baidu.com/mr_z_andy/blog/item/78459f1a558f571a34fa4141.html

欢迎更多讨论:andy#sietoo.com

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22783925/viewspace-712188/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-10-17

  • 博文量
    2
  • 访问量
    3013