Axis2接口的使用

王朝干货·作者佚名  2016-09-20
窄屏简体版  字體: |||超大  

java创建webservice服务端、调用,这是自己写的一个案例,仅供参考。

工具/原料

axis2的zip包:axis2-1.6.2-bin.zip、axis2-1.6.2-war.zip。(版本会更新)从官网下载

tomcat6.x

jdk1.6及以上

java ide myeclipse8.5

方法/步骤

1:将bin包和war包解压,

2:将里面的axis2.war解压

3:用myeclipse新建java web项目testaxis2,将axis2.war解压出来的web-inf里面除了class文件夹外的所有文件拷贝到新建的项目的web-inf下

4:接下来开始写webservice服务端。

在项目src下右击新建类,类名叫helloservice,放在默认包下,类里面有两个放法:sayhello()、sayhellotoname(string name);

5:代码如下:

6:在项目的web-inf/services/axis2/meta-inf/services.xml里配置接口的类,信息如下:

<?xml version="1.0" encoding="utf-8"?>

<servicegroup>

<service name="helloservice">

<parameter name = "serviceclass">helloservice</parameter>

<parameter name="load-on-startup">true</parameter>

<messagereceivers>

<messagereceiver mep="http://w3.org/2004/08/wsdl/in-only"

class="org.apache.axis2.rpc.receivers.rpcinonlymessagereceiver" />

<messagereceiver mep="http://w3.org/2004/08/wsdl/in-out"

class="org.apache.axis2.rpc.receivers.rpcmessagereceiver" />

</messagereceivers>

</service>

</servicegroup>

解释:<service name="helloservice">中,helloservice是服务的类名,可以随便起,这里就个项目里的类名保持一致了;

<parameter name = "serviceclass">helloservice</parameter>中,helloservice就是想目中的类名。

7:将axis2.war文件拷贝到tomcat的webapps下,运行tomcat,在浏览器里输入:http://localhost:8080/axis2,将会出现下图,说明axis2搭建成功了。

8:将刚才自己建的web项目发布到相同的tomcat里。(如果上面的tomcat每关闭要先关闭)启动tomcat。在浏览器里输入地址:http://localhost:8080/testaxis2/axis2-web/index.jsp,回车,点击services,可以看到自己写的服务helloservice和服务的方法sayhello、sayhellotoname,如图下图,至此说明webservice服务接口发布成功,接下来就是调用接口。

9:在项目里新建一个测试类,代码如下:

package cozl.test;

import javax.xml.namespace.qname;

import org.apache.axis2.addressing.endpointreference;

import org.apache.axis2.rpc.client.rpcserviceclient;

public clascallaxis2 {

@suppresswarnings("unchecked")

public static void main(string args) {

tr{

//调用webservice

rpcserviceclient serviceclient = new rpcserviceclient();

org.apache.axis2.client.optionoption= serviceclient.getoptions();

endpointreference targetepr = new endpointreference(

"http://192.168.99.152:8080/testaxis2/services/helloservice");//接口地址

options.setto(targetepr);

qname opgetweather = new qname("http://ws.apache.org/axis2", "sayhellotoname");//命名空间、接口方法

string str="美女";

object opgetweatherarg= new object {str};//请求文

clasreturntype= new clas{ string.clas};

object response = serviceclient.invokeblocking(opgetweather,opgetweatherargs, returntypes);

string result = (string) response[0];//返回结果

systeout.println(result);

} catch (exception e) {

e.printstacktrace();

}

}

}

运行后控制台会输出:hello 美女。说明接口调用成功。

希望本文Axis2接口的使用能帮到你。

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航