`
e_ville
  • 浏览: 27572 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

简单的Web Service例子

阅读更多
原创:简单的Web Service例子                 

(ALin 2006-12-23 13:11)

 
写本文的目的在于提供一个简单的、一定能行的、版本又不是太老的Web Service的入门例子:HelloService,让大家对Web Service有个大致的体会。先声明:我也是个初学者,最近几天有点进展了,所以写出来,或许对某些人能有所帮助。
既然是简单的,就不使用大型的IDE,如EclipseJBuilderNetBeans之类的,只使用一个文本编辑器即可,我一直比较喜欢Editplus,本例中也使用Editplus来编写相关的代码和文件,当然也可以使用其他的文本编辑器,至于编译、打包、部署之类的,都使用手动方式进行,我想这样给大家的印象是最直观的。既然是一定能行的,自然是经我实践过的,在近期内可用的,不过版本升级后可能也会不行了。
 
环境:
1. Axis2 V1.1
2. Tomcat5.5
3. JDK1.5
相关说明及注意事项:

<!---->1.     <!---->Axis2Web Service部署目录:

<TOMCAT_HOME>\webapps\axis2\WEB-INF\services
2. 调用Web Service时方法的参数命名为param0, param1……之类的,而不是与源文件中一致。这个可能是由WSDL文件决定的,所以自已手写WSDL的话不一定是这样子。但在本例中请注意这点。后面有详细的说明。
 
一、在任意位置建立本Web Service的目录结构:
Hello
+------src
         +--------META-INF
                    +---------- services.xml
         +--------Hello.java
+------bin
        +--------META-INF
                   +---------- services.xml
        +--------Hello.class
 
很简单,一个src目录下,放源文件和META-INF目录,其中META-INF目录是用来放置配置文件services.xml的。bin目录下的META-INF完全是src目录下的拷贝。
 
二、编写相关源文件。
Web Service中只有一个源文件:Hello.java和一个配置文件:services.xml。分别如下所示:
源代码:
代码1Hello.java
// Hello.java -- A simple class.
// 2006-12-11 00:22
 
import java.util.Date;
 
public class Hello {

   public String hello(String name) {

       if(name == null)

           name = "NO NAME";

       return "Hello, [" + name + "], welcome to the world of Web Service!";

   }
 

   public String getDate() {

       Date d = new Date();

       return d.toString();

   }
}
 
Hello.java中定义了2个方法,其中hello可以带参数,getDate只是简单地返回当前时间的字符串形式。
 
配置文件:
代码2services.xml

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

<service name="HelloService" scope="application">

    <description>Hello Sample Service</description>

    <messageReceivers>

        <messageReceiver

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

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

        <messageReceiver

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

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

    </messageReceivers>

   <schema schemaNamespace="http://quickstart.samples/xsd"/>

    <parameter name="ServiceClass">Hello</parameter>

</service>
 
services.xml文件中注意以下内容:

<service name=”HelloService” ……> 这个就是在Axis2List Services时所显示的名字。

最后面的<parameter name="ServiceClass">Hello</parameter>换成自己的相关类即可。这里是Hello

最终在Axis2List Services时会显示所有可用的Web Service的名字,方法列表,URL等。如下所示:

<!----><!----><!---->

HelloService

Service EPR : http://localhost:8080/axis2/services/HelloService
Service REST epr : http://localhost:8080/axis2/services/HelloService
: http://localhost:8080/axis2/rest/HelloService

Service Description : HelloService

Service Status : Active
Available Operations
  • getDate
  • hello
 
三、编译、打包、部署
1. 编译,只有一个Hello.java要编译,这个地球人都会吧。编译好了把生成的Class文件放到bin目录下。这个可以借助于ANT来完成。这里不说了,直接手动完成。
 
2. 打包,这一步不是必须的,其实只要把bin文件夹放到最终的部署目录下就行了。当然啦,为了便于区分,把目录名改成HelloService,也可以改成其他的任意名字。
一个Web Service项目在打包生成的是.aar文件。使用jar命令即可。

命令行进入到我们的bin目录下,执行命令jar cvf HelloService.aar .(注意最后一个点不可少,反正就是打包命令咯,不管用什么方法,只要正确打包就行了)

F:\JAVA_File\RPC_RMI_CORBA_Example\Web Service\Hello\bin>jar cvf HelloService.aar .

标明清单(manifest)

忽略项 META-INF/

增加:META-INF/services.xml(读入= 619) (写出= 288)(压缩了 53%)

增加:Hello.class(读入= 523) (写出= 337)(压缩了 35%)

 

F:\JAVA_File\RPC_RMI_CORBA_Example\Web Service\Hello\bin>

 
3. 部署,执行完第2步后,在bin目录下可以看到生成的HelloService.aar文件,这个就是最终要部署的,把它复制到部署目录下就行了。当然也可以按照第2步中的不打包,直接部署。
部署目录是:
<TOMCAT_HOME>\webapps\axis2\WEB-INF\services
我自己的为:

C:\Tomcat 5.5\webapps\axis2\WEB-INF\services

 
四、测试Web Service
1. 访问:http://localhost:8080/axis2/
这个是Axis2 Web应用程序的主页,有3个链接:
Services

      View the list of all the available services deployed in this server.

* Validate

      Check the system to see whether all the required libraries are in place and view the system information.

* Administration

      Console for administering this Axis2 installation.

 
Services:列出Web Service。出错的Web Service在最下面。
Validate:检查Axis2相关的包是否存在以及环境是否配置正确。
AdministrationAxis2默认的管理员帐号和密码为:admin/axis2
 

<!---->2.     <!---->测试HelloService

查看相关的WSDLXSD
http://localhost:8080/axis2/services/HelloService?wsdl
http://localhost:8080/axis2/services/HelloService?xsd
HelloService换成其他的Web Service就可以查看相应的WSDLXSD了。查看WSDL的时候要注意一点:看方法参数的名字。方法的参数命令为param0, param1……之类的。在调用这些方法的时候就要使用param0,而不是使用在Java源代码中方法的参数名。这一点非常重要。
访问以下页面:
http://localhost:8080/axis2/rest/HelloService/hello?param0=Ben
可以看到输出结果为:

<!----><!----><!---->

 −
<ns:helloResponse>
<ns:return>Hello, [Ben], welcome to the world of Web Service!</ns:return>
</ns:helloResponse>
访问页面:
http://localhost:8080/axis2/rest/HelloService/getDate
输出结果为:

<!----><!----><!---->

 <ns:getDateResponse>
<ns:return>Sat Dec 23 19:19:38 CST 2006</ns:return>
</ns:getDateResponse>
这就表示Web Service运行正常。
 
五、总结
编写Web Service的编译、打包、部署之类的工作可以使用ANT来做,很方便。也可以使用其他的IDE来做,很多IDE其实也是内部使用ANT来完成的。不过,直接手动操作可以加深印象,让人知道是怎么一回事,而不会知其然,不知其所以然。
 
 
 
[参考资料]
1. Axis2文档。
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics