WebService学习之旅(五)基于Apache Axis2发布第一个WebService

来源:互联网 发布:mac excel怎么加 编辑:程序博客网 时间:2024/05/29 13:49

上篇博文介绍了如何將axis2 webservice引擎安装到Web容器中,本节开始介绍如何基于apache axis2发布第一个简单的WebService。

一、WebService服务端发布步骤
1.打开Eclipse工程,新建一个Java Project,例如笔者工程名为axis2-recipe01。

2.编写Web服务接口及实现类
HelloWorld.java

package com.csdn.ws.axis2.recipe01;public interface HelloWorld {    String sayHello(String name);}

HelloWorldImpl.java

package com.csdn.ws.axis2.recipe01;public class HelloWorldImpl implements HelloWorld {    @Override    public String sayHello(String name) {        return "hello," + name;    }}

大家可能已经注意到了,前面我们使用jax-ws发布webservice时,web服务接口和实现类需要使用注解@WebService和@WebMethod修饰,但是使用axis2引擎时并不需要。

2.在工程根路径下,新建一个META-INF目录,然后在该目录下新建一个services.xml文件,注意名称不能改变。

3.打开services.xml,添加webservice的描述信息,内容如下:

<?xml version="1.0" encoding="UTF-8"?><service name="HelloWorld">    <description>        HelloWorld Service Example    </description>    <parameter name="ServiceClass">        com.csdn.ws.axis2.recipe01.HelloWorldImpl    </parameter>    <parameter name="useOriginalwsdl">        false    </parameter>    <operation name="sayHello">        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />    </operation></service>

根节点为service,它的name属性指定webservice名称,parameter标签用于指定一些配置参数,上面的ServiceClass参数指定webservice接口,useOriginalwsdl参数指定是否使用axis2自动生成wsdl,该参数通常为false,表示由axis2自动生成wsdl。operation标签指定对外发布的方法名。

4.在工程上点击右键=>export=>Jar File,点击Next按钮,去掉.classpath和.project文件选择。

5.保存文件名为axis2-recipe01.aar,將此文件复制到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services目录下。
(注:axis2安装到tomcat请参考前面的文章)

6.重启tomcat服务器,打开浏览器访问http://localhost:8080/axis2/axis2-web/index.jsp
然后点击Services链接,看到如下界面说明我们的WebService发布成功。
这里写图片描述

点击HelloWorld链接,可以查看该WebService的wsdl文档。
这里写图片描述

本节内容就到此为止吧,基于axis2的客户端调用放在下节介绍。

0 0
原创粉丝点击