Axis实践之Axis入门

关键字: web service axis apache

第一部分: 安装Axis

第一步: 下载Axis
从Apache网站下载Axis发布包: http://ws.apache.org/axis/

我下载的是 Axis 1.4 Final 版


第二步: 解压Axis压缩包到tomcat/webapps目录下
把下载的axis-bin-1_4.zip包解压缩到一个文件夹,可以是任意的,但如果文件夹包含中文名时,在中间有些过程需要对中文进行一些处理,否则可能出错;
我把axis-bin-1_4.zip解压到 F:/AXIS_Study,解压后的目录结构是这样的:
F:/AXIS_Study
    |
    |____axis-bin-1_4 
    |-----docs (文件夹)
    |-----lib (文件夹) 

    |-----samples (文件夹) 
    |-----webapps (文件夹) 
    |-----xmls (文件夹) 
    |-----LICENSE 
    |-----NOTICES 
    |-----README 
    |-----release-notes.html

第三步:启动tomcat,查看一下当前Axis中布署了哪些Web Service
打开浏览器,在地址栏输入: http://127.0.0.1:8000/axis/servlet/AxisServlet

 

浏览器显示结果:

 

这说明在Axis中已经布署了两个WebService,只不过这两个WebService都是Axis自带的,第一个是管理用的WebService,第二个是查看当前Axis版本信息的WebService。
大家可以点击相应的链接“wsdl”来看一下这两个WebService的 wsdl 文件。


结语: 至此,我们已经成功地把Axis 1.4 安装到Tomcat Web 服务器上了,接下来的目标是使用Axis来发布Web Service。


第二部分: 使用Axis发布Web Service
Axis提供两种方式将Java类发布成Web Services,这两种方式分别是:即时快速自动发布 和 通过配置文件进行发布,下面我们按顺序来讲如何使用这两种方式来发布WebService

即时快速自动发布:
第一步: 编写业务类
为了发布一个Web Service,首先必须要有业务类,为了节约时间,我写一个最简单的业务类:

public class SayHello {
 public String sayHello(String name) {
  return "I can fly. Hello " + name;
 }
}

 
注意,采用快速自动发布的Web Service的业务类不能带 package,也不能实现任何接口(Interface),因为Axis自动发布时不支持Package。


第二步:把SayHello发布为Web Service
把SayHello.java复制到 axis目录下的webapps/axis目录下,然后把文件扩展名改为:jws


第三步:访问刚发布的Web Service
打开浏览器,在地址栏里输入: http://127.0.0.1:8000/axis/SayHello.jws
浏览器显示结果如下:

There is a Web Service here

Click to see the WSDL

这表示Web Service已经发布成功了。我们可以点击连接“Click to see the WSDL ”来看看生成的wsdl文件:

Xml代码
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://127.0.0.1:8000/axis/SayHello.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8000/axis/SayHello.jws" xmlns:intf="http://127.0.0.1:8000/axis/SayHello.jws" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
  -->
- < wsdl:types >
- < schema elementFormDefault =" qualified " targetNamespace =" http://wsAgent " xmlns =" http://www.w3.org/2001/XMLSchema ">
- < element name =" request ">
- < complexType >
- < sequence >
  < element name =" str " type =" xsd:string " />
  </ sequence >
  </ complexType >
  </ element >
- < element name =" requestResponse ">
- < complexType >
- < sequence >
  < element name =" requestReturn " type =" xsd:string " />
  </ sequence >
  </ complexType >
  </ element >
  </ schema >
  </ wsdl:types >
- < wsdl:message name =" requestResponse ">
  < wsdl:part element =" impl:requestResponse " name =" parameters " />
  </ wsdl:message >
- < wsdl:message name =" requestRequest ">
  < wsdl:part element =" impl:request " name =" parameters " />
  </ wsdl:message >
- < wsdl:portType name =" ServerAgent ">
- < wsdl:operation name =" request ">
  < wsdl:input message =" impl:requestRequest " name =" requestRequest " />
  < wsdl:output message =" impl:requestResponse " name =" requestResponse " />
  </ wsdl:operation >
  </ wsdl:portType >
- < wsdl:binding name =" ServerAgentSoapBinding " type =" impl:ServerAgent ">
  < wsdlsoap:binding style =" document " transport =" http://schemas.xmlsoap.org/soap/http " />
- < wsdl:operation name =" request ">
  < wsdlsoap:operation soapAction ="" />
- < wsdl:input name =" requestRequest ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:input >
- < wsdl:output name =" requestResponse ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:output >
  </ wsdl:operation >
  </ wsdl:binding >
- < wsdl:service name =" ServerAgentService ">
- < wsdl:port binding =" impl:ServerAgentSoapBinding " name =" ServerAgent ">
  < wsdlsoap:address location =" http://127.0.0.1:8000/axis/services/HelloWorld2 " />
  </ wsdl:port >
  </ wsdl:service >
  </ wsdl:definitions >

第五步:写测试客户端
public static void main(String[] args) {
  try {
   String endpoint = "http://127.0.0.1:8000/axis/services/HelloWorldWSDD";

   Service service = new Service();
   Call call = (Call) service.createCall();

   call.setTargetEndpointAddress(new java.net.URL(endpoint));
   call.setOperationName("sayHello");

   String ret = (String) call.invoke(new Object[] { "Yu DouDou" });

   System.out.println("Return : [" + ret + "]");
  } catch (Exception e) {
   System.err.println(e.toString());
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值