Blog信息 |
blog名称:小鸟吹烟 日志总数:157 评论数量:424 留言数量:-1 访问次数:1252919 建立时间:2006年10月23日 |

| |
[webservice]使用Axis发布简单的WebService服务 文章收藏, 网上资源
tone 发表于 2007/4/23 18:10:54 |
(http://www.blogjava.net/gf7/archive/2006/01/10/27359.html)
使用Axis,要发布一个Web服务非常简单,简直不能再简单了,尽管看起来过程和相关代码有些长。我这个帖子里用到了这些软件:Axis 1.1、Eclipse 2.1和Eclipse的Tomcat插件2.2(Sysdeo Tomcat plugin)。发布的方法如下:
我要发布的服务是一个图书商店,公布的方法有添加图书addBook、列表图书listBooks、删除图书deleteBook等等,为简单起见这里就只发布一个添加图书方法,因为其他方法的发布是类似的。
1、首先在Eclipse里新建一个名为bookstore的Tomcat工程,注意要安装了前面说的Tomcat插件才有这个选项的。如果没有安装可以建立一个java工程,然后手动建立必要的目录结构(WEB-INF等),并在Tomcat的server.xml里手动增加与项目对应的<context>项。
2、接下来建立图书类(com.bookstore.model.Book),图书有名称、ISDN号和页数三个属性,这是一个Bean类,代码如下:
500)this.width=500'>package com.bookstore.model;500)this.width=500'>500)this.width=500'>public class Book {500)this.width=500'> private String name;500)this.width=500'> private String ISDN;500)this.width=500'> private int page;500)this.width=500'>500)this.width=500'> public String getISDN() {500)this.width=500'> return ISDN;500)this.width=500'> }500)this.width=500'>500)this.width=500'> public String getName() {500)this.width=500'> return name;500)this.width=500'> }500)this.width=500'>500)this.width=500'> public int getPage() {500)this.width=500'> return page;500)this.width=500'> }500)this.width=500'>500)this.width=500'> public void setISDN(String string) {500)this.width=500'> ISDN = string;500)this.width=500'> }500)this.width=500'>500)this.width=500'> public void setName(String string) {500)this.width=500'> name = string;500)this.width=500'> }500)this.width=500'>500)this.width=500'> public void setPage(int i) {500)this.width=500'> page = i;500)this.width=500'> }500)this.width=500'>500)this.width=500'>}500)this.width=500'>
3、接下来建立用来提供服务的类(com.bookstore.BookSvc),这个类就是实际的功能类了,它里面只有一个public的addBook()方法,而它的参数只有一个就是要添加的图书。代码如下:
500)this.width=500'>package com.bookstore;500)this.width=500'>500)this.width=500'>import com.bookstore.model.Book;500)this.width=500'>500)this.width=500'>public class BookSvc {500)this.width=500'> 500)this.width=500'> public void addBook(Book book){500)this.width=500'> //here you save a book into database500)this.width=500'> System.out.println("Book has been added.");500)this.width=500'> }500)this.width=500'>}500)this.width=500'>
4、现在,把下载来的Axis解压缩到一个文件夹,这里假设你解到C:\axis-1_1。把C:\axis-1_1\webapps\axis\WEB-INF\lib目录下的所有.jar文件复制到你的这个web应用程序的WEB-INF\lib下,再把C:\axis-1_1\webapps\axis\WEB-INF目录下的web.xml复制到你的web应用程序的WEB-INF下。这个步骤相当于在你的web应用程序中配置了Axis。
5、为了让Axis知道你要发布哪些服务,你得在WEB-INF下建立一个名为server-config.wsdd的文件,内容如下:
500)this.width=500'><?xml version="1.0" encoding="UTF-8"?>500)this.width=500'><deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">500)this.width=500'> <globalConfiguration>500)this.width=500'> <parameter name="adminPassword" value="admin"/>500)this.width=500'> <parameter name="attachments.Directory" value="C:\eclipse\workspace\bookstore\WEB-INF\attachments"/>500)this.width=500'> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>500)this.width=500'> <parameter name="sendXsiTypes" value="true"/>500)this.width=500'> <parameter name="sendMultiRefs" value="true"/>500)this.width=500'> <parameter name="sendXMLDeclaration" value="true"/>500)this.width=500'> <parameter name="axis.sendMinimizedElements" value="true"/>500)this.width=500'> <requestFlow>500)this.width=500'> <handler type="java:org.apache.axis.handlers.JWSHandler">500)this.width=500'> <parameter name="scope" value="session"/>500)this.width=500'> </handler>500)this.width=500'> <handler type="java:org.apache.axis.handlers.JWSHandler">500)this.width=500'> <parameter name="scope" value="request"/>500)this.width=500'> <parameter name="extension" value=".jwr"/>500)this.width=500'> </handler>500)this.width=500'> </requestFlow>500)this.width=500'> </globalConfiguration>500)this.width=500'> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>500)this.width=500'> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>500)this.width=500'> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>500)this.width=500'> <service name="Version" provider="java:RPC">500)this.width=500'> <parameter name="allowedMethods" value="getVersion"/>500)this.width=500'> <parameter name="className" value="org.apache.axis.Version"/>500)this.width=500'> </service>500)this.width=500'> <service name="BookSvc" provider="java:RPC">500)this.width=500'> <parameter name="allowedMethods" value="*"/>500)this.width=500'> <parameter name="className" value="com.bookstore.BookSvc"/>500)this.width=500'> </service>500)this.width=500'> <service name="AdminService" provider="java:MSG">500)this.width=500'> <parameter name="allowedMethods" value="AdminService"/>500)this.width=500'> <parameter name="enableRemoteAdmin" value="false"/>500)this.width=500'> <parameter name="className" value="org.apache.axis.utils.Admin"/>500)this.width=500'> <namespace>http://xml.apache.org/axis/wsdd/</namespace>500)this.width=500'> </service>500)this.width=500'> <transport name="local">500)this.width=500'> <responseFlow>500)this.width=500'> <handler type="LocalResponder"/>500)this.width=500'> </responseFlow>500)this.width=500'> </transport>500)this.width=500'> <transport name="http">500)this.width=500'> <requestFlow>500)this.width=500'> <handler type="URLMapper"/>500)this.width=500'> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>500)this.width=500'> </requestFlow>500)this.width=500'> </transport>500)this.width=500'></deployment>
这个文件里发布了三个服务:Version、AdminService和我们的BookSvc。还有一个方法可以生成这个文件,好象Axis推荐使用这种生成的方法,就是在同样目录下写一个deploy.wsdd文件(如果不想看可以直接跳到下一步),内容如下:
500)this.width=500'><deployment xmlns="http://xml.apache.org/axis/wsdd/"500)this.width=500'> xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">500)this.width=500'> <service name="BookSvc" provider="java:RPC">500)this.width=500'> <parameter name="className" value="com.bookstore.BookSvc"/>500)this.width=500'> <parameter name="allowedMethods" value="*"/>500)this.width=500'> </service>500)this.width=500'></deployment>
也就是说deploy.wsdd里只包含关于我们的服务的描述,确认Tomcat已经启动,然后在同一目录下用下面这个命令生成server-config.wsdd文件:
500)this.width=500'>java org.apache.axis.client.AdminClient -lhttp://localhost:8080/bookstore/services/AdminService deploy.wsdd
其中bookstore是我这个web应用程序的虚拟路径。
6、重新启动Tomcat,访问路径http://localhost:8080/bookstore/services,就可以看到现在发布了三个Web服务,如下图。点击每个服务后的wsdl链接可以看到对应的WSDL描述。
500)this.width=500'>
可以看出,在Axis里书写deploy.wsdd并利用org.apache.axis.client.AdminClient发布,其主要工作就是把<service>标签中的内容添加在server-config.wsdd里,所以一般直接编辑server-config.wsdd文件会更方便一些。不过当你还没有server-config.wsdd文件时,使用deploy.wsdd的方法会更方便些,因为AdminClient会帮你生成一些额外的xml元素(<handler>等等),而这些元素是必要的。
服务发布以后,就可以在IE浏览器里看到它的WSDL,一般是服务的URL后面加一个“?wsdl”,例如添加图书的WSDL可以通过http://localhost:8080/bookstore/services/BookSvc?wsdl看到。至于Java类是以何种规则映射到WSDL的,请参考JAX-RPC规范;WSDL本身的说明见这里;为了搞清生成的WSDL中各种URL格式的名称空间,最好对XML Schema有所了解,我觉得这篇文章还不错。
我们还可以通过IE浏览器直接调用服务,方法是在服务URL后加“method=xxx”,其中xxx是要调用的方法名称。例如可以通过http://localhost:8080/bookstore/services/BookSvc?method=addBook调用添加图书方法,按照我们的服务类,在Tomcat的控制台上应该可以看到打出了“Book has been added.”的字样。
因为添加图书方法的参数是一个自定义类型,所以在IE里调用时不能指定参数值(或者是可以以其他格式指定,但我还不知道);如果参数是简单类型,就可以指定了。例如我们可以为BookSvc增加一个echo()方法,参数是一个java.lang.String类型的值,如下所示,然后重新编译并启动Tomcat(server-config.wsdd文件不必更改)。
500)this.width=500'>public void echo(String str){500)this.width=500'> System.out.println("Hello "+str);500)this.width=500'>}500)this.width=500'>
在IE里输入http://localhost:8080/bookstore/services/BookSvc?method=echo&str=Mike,就会看到Tomcat的控制台里打出了“Hello Mike”。如果有多个参数,只要把这些参数都列在URL里即可。 |
|
回复:使用Axis发布简单的WebService服务 文章收藏, 网上资源
真不准发表评论于2007/5/19 18:30:23 |
|
» 1 »
|