注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 35岁技术人遭遇年龄坎儿,..
 帮助

基于Tomcat5.0和Axis2开发Web Service应用实例


2007-04-13 21:59:03
 标签:Tomcat web Service Axis2   [推送到博客圈]

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangjunhd.blog.51cto.com/113473/23690
本文将介绍如何使用Tomcat5.0Apache Axis2开发、部署及测试一个简单的Web Service应用。
author: ZJ 07-3-12
 
1工作环境
Eclipse 3.1.2+Lomboz+jdk1.5+ apache-tomcat-5.0.18+AXIS2:1.0(war版本和bin版本)
http://ws.apache.org/axis2/download/1_0/download.cgi页面下载AXIS2Binary Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zipwar Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip。把这两个文件解压,比如解压缩的后得目录为C:\axis2-std-1.0-binC:\axis2.war
Eclipse下通过菜单window—preferences…--Java—Build Path—User Libraries 新建一个user library,比如名字就叫axis2C:\axis2-std-1.0-bin\lib下的所有jar文件包含进来。把axis2.war拷贝到%TOMCAT-HOME%/webapps下面。
 
2.检验安装
Eclipse下启动Tomcat,在地址栏内输入http://localhost:8080/axis2/
点击Validate,将到达 Axis2 Happiness Page
3WebService中的HelloWorld
1)新建一个动态web工程,取名ZZaxis,右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2
 
2)新建package sample,建立HelloWorld.java,代码如下。
HelloWorld.java
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
 
public class HelloWorld {
       public OMElement sayHello(OMElement in){
              String name=in.getText();
              String info=name+"HelloWorld!";
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
              resp.setText(info);
              return resp;
       }
}
 
3)在WebContent\META-INF\建立services.xml,代码如下。
services.xml
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorld">
<description>
  This is a sample Web Service.
</description>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
<operation name="sayHello">
  <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
 
4)将目录sample和目录META-INF组织如下(新建目录example)。
+-example
|-------- +-sample
    |------- HelloWorld.class
|---------+-META-INF
       |------- services.xml
 
5)打包生成aar文件。
在命令符环境下,将当前目录转到example
jar cvf HelloWorld.aar . //注意最后一个点,在当前目录下生成HelloWorld.aar
 
6)在Eclipse中启动Tomcat,在地址栏下键入http://localhost:8080/axis2/。选择Administration,输入用户名admin,密码axis2。选择左侧工具栏Tools- Upload Service,上传之前打包的HelloWorld.aar。该文件将在<CATALINA_HOME>/webapps/axis2\WEB-INF\services目录下。
 
7)编写客户端检验代码。新建Java Project,取名为ZZaxisClient。右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2
 
8)新建package example.client。建立TestClient.java,代码如下。
TestClient.java
package example.client;
 
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
 
public class TestClient {
       private static EndpointReference targetEPR=new EndpointReference
         ("http://localhost:8080/axis2/services/HelloWorld");
       public static OMElement getSayHelloOMElement(){
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement method=fac.createOMElement("sayHello",omNs);
              method.setText("ZJ");
              return method;
       }
       public static void main(String[] args){
              try{
                     Options options=new Options();
                     options.setTo(targetEPR);
                     ServiceClient sender=new ServiceClient();
                     sender.setOptions(options);
                     OMElement sayHello=TestClient.getSayHelloOMElement();
                     OMElement result=sender.sendReceive(sayHello);
                     System.out.println(result);
              }
              catch(Exception axisFault){
                     axisFault.printStackTrace();
              }
       }
}
 
9)测试,run TestClient.java as Java Application。结果:
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/"
xmlns:tns="http://ws.apache.org/axis2">
ZJHelloWorld!
</hw:sayHelloResponse>
 
4.后续
详细介绍clientserver端代码。《基于Tomcat5.0和Axis2开发Web Service代码详解

本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/23690





    文章评论
 <<   1   2   3   >>   页数 ( 1/3 )  
2007-04-15 20:22:59
学习学习。。。

2007-05-18 16:28:55
不错啊,写的很好啊,能给初学者带来很大的收获啊.
楼主,顶你一个,继续加油啊,期待你更精彩的技术文章
哈哈

2007-07-31 15:32:09
今天在网上找到博主的文章。我刚开始接触Tomcat and Axis2.手上有一个项目。博主的文章写的很细致,很简单明了,基本上都理解了。但我有一个问题,
OMNamespace后http的内容是自己任意设置吗?

2007-07-31 18:50:26
to maomao-katze,
设置NAMESPACE,看你自己的XML SCHEMA,如果需要验证,则应该提供有效的地址

2007-08-01 22:25:26
XML schema? 在网上看到很多这样的内容,还是很糊涂。博主上面的程序,在我机子上也无法成功运行!

错误提示是:

org.apache.axis2.AxisFault: The SERVICE_OBJECT_SUPPLIER parameter is not specified.
     at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:434)
     at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:373)
     at org.apache.axis2.description.OutInAxisOperationClient.execute(OutInAxisOperation.java:294)
     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:520)
     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:500)
     at sample.client.TestClient.main(TestClient.java:37)

2007-08-01 23:06:23
博主,不好意思,还是我。

还想问一个问题!

在Client端输入多次String流,使service程序能够相应的每次返回刚输入的String的内容。就是说,service程序怎样才能被多次调用,并且每次都能返回新内容呢? 或者,最后一次性的返回输入的所有内容呢?
我自己试了一下,不行,返回的内容我copy了,如下:

gib den Name an:
maomao
...........
maomao

gib den Nickname an:
katze
...........
maomao

当第二次输入katze时,返回的还是第一次输入的内容。这就是说,service程序无法读取第二次输入的nickname.

在service程序上尝试用while(), for() or if()编译。但不行,提示说:service程序返回值应为OMElement类型。我想我这个思路可能不对!

希望博主能够提供帮助。先谢谢了!

2007-08-01 23:12:25
to   maomao-katze
看一下博客中的另两篇文章,使用Axis2来构建Web Service客户端 & 使用Axis2的底层API开发Web Service

2007-08-01 23:33:09
博主,谢谢这么快提供帮助,正在看你推荐的两篇文章。我看到你写了很多原创的关于tomcat,axis2,ant,dom.。。。etc的文章。我想问一下,这些必须都得全部掌握吗?我现在在实习。看得越来越糊涂了!原来只接触过c++!

2007-08-02 09:20:59
to   maomao-katze,
知识永远都学不完,需要什么就学什么。如何去学习以及如何解决实际工作中的问题是最重要的。我个人认为。

2007-08-09 17:20:14
请教版主,我按照你的例子写了一个,在同一台电脑三是可以的,但是不在同一台的时候就报以下错误,请指点,我应该怎么修改!

org.apache.axis2.AxisFault: Mime parts not found. Stream ended while searching for the boundary
     at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:434)
     at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:373)
     at org.apache.axis2.description.OutInAxisOperationClient.execute(OutInAxisOperation.java:294)
     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:520)
     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:500)
     at sample.FileTransferClient.upload(FileTransferClient.java:34)
     at sample.FileTransferClient.main(FileTransferClient.java:120)
is upload success: false
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:324)
     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

 <<   1   2   3   >>   页数 ( 1/3 )  

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: