加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

用AXIS2发布WebService和客户端调用

发布时间:2020-12-17 00:14:14 所属栏目:安全 来源:网络整理
导读:照着做保你成功哦!!实例下载:点击打开链接 用AXIS2发布WebService的方法 ?Axis2+tomcat6.0 实现webService 服务端发布与客户端的调用(-) ? Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法。 第一步:首先要下载开发所需要的jar包 下



照着做保你成功哦!!实例下载:点击打开链接

用AXIS2发布WebService的方法

?Axis2+tomcat6.0 实现webService 服务端发布与客户端的调用(-)

?

Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法。

第一步:首先要下载开发所需要的jar包

下载:axis2-1.6.1-war.zip

http://www.apache.org/dist//axis/axis2/java/core/1.6.1/

下载完后解压至tomcat安装目录下的webapps文件夹下,启动tomcat后,在webapps目录下会生成axis2文件夹。

访问http://localhost:8080/axis2/能看到以下页面表示axis2运行成功。


第二部

在Eclipse下新建Web Project,工程名:webServe。新建包com.cm.service,新建类HelloWorld,代码如下:

package?com.cm.service;

public?classHelloWorld {

????public?String sayHello(String name){return?"Hello, "+name+".";}

????public?String saySorry(String name){return?"Sorry,"+name+".";}

????public?String getWorld(){return?"Hello,World";}

????

}

在WEB-INF目录下修改web.xml文件,内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5">????

<!--Axis2?config?start-->

<servlet>

????<servlet-name>AxisServlet</servlet-name>

????<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>

????<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>?

?<servlet-name>AxisServlet</servlet-name>??

?<url-pattern>/services/*</url-pattern>??

</servlet-mapping>?

<!--Axis2??end-->

??<welcome-file-list>

????<welcome-file>index.jsp</welcome-file>

??</welcome-file-list>

</web-app>

把tomcat安装目录下的webapps/axis2/WEB-INF下的modules、service和conf文件件拷至HelloWorld下的WEB-INF目录下。把lib下的如下jar包夜拷过去。

?


然后再services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:

<servicename="HelloWorld">??

????<description>??

????????HelloWorld Service Example

????</description>??

????<parametername="ServiceClass">??

????????com.cm.service.HelloWorld

????</parameter>??

????<operationname="sayHello">??

????????<messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>??

????</operation>??

????<operationname="saySorry">??

????<operationname="getWorld">??

????????<messageReceiverclass="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>??

</service>??

启动tomcat后访问http://127.0.0.1:8888/webServe/services/HelloWorld?wsdl能看到服务信息了。

到此Axis2的WebService服务已成功发布

?



axis2 客户端调用实例



Axis2+tomcat6.0 实现webService?服务端发布与客户端的调用(二)

下面看看利用axis2 客户端调用实例

新建一个客户端调用类AxisUtil

代码如下:

package?com.cm.client;

import?javax.xml.namespace.QName;

import?org.apache.axis2.addressing.EndpointReference;

import?org.apache.axis2.client.Options;

import?org.apache.axis2.rpc.client.RPCServiceClient;

?

public?class?AxisUtil {

????public?static?void?main(String[] args) {

???????

???????String xmlStr="xiaoxu.wang";

????????String url="http://127.0.0.1:8888/webServe/services/HelloWorld";

???????String method="saySorry";

???????AxisUtil.sendService(xmlStr,url,method);

????}

????public?static?String sendService(String xmlStr,String url,String method){

???????String xml=null;

??????

???????????RPCServiceClient serviceClient =?new?RPCServiceClient();

???????????Options options = serviceClient.getOptions();

???????????EndpointReference targetEPR =?new?EndpointReference(url);

???????????options.setTo(targetEPR);

???????????//?在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值

????????????QName opAddEntry =?new?QName("http://service.cm.com",method);

????????????//?参数,如果有多个,继续往后面增加即可,不用指定参数的名称

????????????Object[] opAddEntryArgs =?new?Object[] {xmlStr};

??????????????????????//?返回参数类型,这个和axis1有点区别

????????????// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;

????????????//?第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];

????????????//?第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。

????????????//?当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}

????????????//?如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,

????????????//?该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同

????????????Class[] classes =?new?Class[] { String.class?};

????????????xml=(String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0];

????????????System.out.println(xml);?

????????return?xml;

????}

}

?

运行结果:

Sorry,xiaoxu.wang.

?

总结:以上就是Axis2?服务发布与调用的简单案例。至于其他的实现方法有兴趣的可以继续研究。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读