一、搭建简单的axis web服务

33 篇文章 0 订阅
33 篇文章 9 订阅

在官方网站下载axis 的工程(这个等下就有用的)和源码、jar 包等,下载地址是:

http://labs.renren.com/apache-mirror//ws/axis/1_4/

2 、解压下载的工程或源码(两个中任意一个都可以),解压axis-bin-1.4 可以看到大致目录是这样的:

docs 是文档、libjar 包、sample 是示例、xmls 是当前工程所需的xmlwebapps 是当前工程的webroot 目录;

我们打开webapps 目录就可以看到一个axis 的文件夹,这个文件夹里面有WEB-INF 文件夹和一些页面,将axis 复制到你的tomcatwebapps 目录下。然后启动tomcat 服务,访问 http://localhost:8080/axis/ ,看到下面的解码就说明部署成功了:


以后我们将和这个工程不离不弃,它将在我们的axis1.xwebService 中发挥很大的作用!

 

3 、创建我们自己的web 工程,这里我新建的AxisWebService ;创建好工程后,将刚才解压的axis-bin 中的libjarcopy 到当前工程的lib 中;

axis-ant.jar

axis.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

saaj.jar

wsdl4j-1.5.1.jar

activation-1.1.jar

mail-1.4.jar

 

创建webService 类文件,代码如下:

package com.hoo.service;

 

/**

  * <b> function: </b> jws axis WebService

  * @author hoojo

  * @createDate Dec 15, 2010 17:03:49 PM

  * @file HelloWorldService.java

  * @package com.hoo.service

  * @project AxisWebService

  * @blog http://blog.csdn.net/IBM_hoojo

  * @email hoojo_@126.com

  * @version 1.0

  */

public class HelloWorldService {

    public String sayHello(String name, int age) {

       return name + " say : hello world! [axis] my age is " + age;

    }

}

 

4 复制HelloWorldService.java 到我们刚才复制的axis 文件夹下即可;也就是tomcat 下的webapps 下的axis 下即可;注意:还有重要的一般就是要将这个java 文件中的包名去掉,并且将这个文件重命名为 HelloWorldService.jws ;如果带包名的话,请求后编译的class 将会在包路径下,这样我们在全球当前jws 的时候就会出现找不到class ,详细的你可以到发布在tomcat 下的工程看看WEB-INF 目录下的jwsClass 就一目了然了。

上面的工作完成后,启动tomcat 服务器,访问 http://localhost:8080/axis/HelloWorldService.jws

你会看到:

There is a Web Service here

Click to see the WSDL

如果你和我看到的是一样的,就证明你已经成功的部署了一个axis1.xwebService 。然后我们点击下就可以看到wsdlxml 文件了,内容如下:

 

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

- < wsdl:definitions targetNamespace =" http://localhost:8080/axis/HelloWorldService.jws " xmlns:apachesoap =" http://xml.apache.org/xml-soap " xmlns:impl =" http://localhost:8080/axis/HelloWorldService.jws " xmlns:intf =" http://localhost:8080/axis/HelloWorldService.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:message name =" sayHelloResponse ">

  < wsdl:part name =" sayHelloReturn " type =" xsd:string " />

  </ wsdl:message >

- < wsdl:message name =" sayHelloRequest ">

  < wsdl:part name =" name " type =" xsd:string " />

  < wsdl:part name =" age " type =" xsd:int " />

  </ wsdl:message >

- < wsdl:portType name =" HelloWorldService ">

- < wsdl:operation name =" sayHello " parameterOrder =" name age ">

  < wsdl:input message =" impl:sayHelloRequest " name =" sayHelloRequest " />

  < wsdl:output message =" impl:sayHelloResponse " name =" sayHelloResponse " />

  </ wsdl:operation >

  </ wsdl:portType >

- < wsdl:binding name =" HelloWorldServiceSoapBinding " type =" impl:HelloWorldService ">

  < wsdlsoap:binding style =" rpc " transport =" http://schemas.xmlsoap.org/soap/http " />

- < wsdl:operation name =" sayHello ">

  < wsdlsoap:operation soapAction ="" />

- < wsdl:input name =" sayHelloRequest ">

  < wsdlsoap:body encodingStyle =" http://schemas.xmlsoap.org/soap/encoding/ " namespace =" http://DefaultNamespace " use =" encoded " />

  </ wsdl:input >

- < wsdl:output name =" sayHelloResponse ">

  < wsdlsoap:body encodingStyle =" http://schemas.xmlsoap.org/soap/encoding/ " namespace =" http://localhost:8080/axis/HelloWorldService.jws " use =" encoded " />

  </ wsdl:output >

  </ wsdl:operation >

  </ wsdl:binding >

- < wsdl:service name =" HelloWorldServiceService ">

- < wsdl:port binding =" impl:HelloWorldServiceSoapBinding " name =" HelloWorldService ">

  < wsdlsoap:address location =" http://localhost:8080/axis/HelloWorldService.jws " />

  </ wsdl:port >

  </ wsdl:service >

  </ wsdl:definitions >

 

 

分析下wsdlxml 文件内容:

targetNamespace = http://localhost:8080/axis/HelloWorldService.jws

是我们部署的webservice 命名空间,也就是我们访问的webService 路径。

 

< wsdl:message name =" sayHelloResponse ">

           < wsdl:part name =" sayHelloReturn " type =" xsd:string " />

  </ wsdl:message >

是返回值的信息, sayHelloResponse 代表响应,即返回值,type 是返回值的类型

 

< wsdl:message name =" sayHelloRequest ">

           < wsdl:part name =" name " type =" xsd:string " />

           < wsdl:part name =" age " type =" xsd:int " />

  </ wsdl:message >

请求方法参数信息, sayHelloRequest 即请求,part 是参数parametertype 是参数的类型

 

< wsdl:portType name =" HelloWorldService ">

            < wsdl:operation name =" sayHello " parameterOrder =" name age ">

           < wsdl:input message =" impl:sayHelloRequest " name =" sayHelloRequest " />

           < wsdl:output message =" impl:sayHelloResponse " name =" sayHelloResponse " />

        </ wsdl:operation >

  </ wsdl:portType >

portType name 是当前webService 的名称,operation 是一个操作,即可以调用的方法。name 就是方法名称了,parameterOrder 是参数,input 输入即传入参数,output 输出即返回的值;

 

< wsdl:service name =" HelloWorldServiceService ">

< wsdl:port binding =" impl:HelloWorldServiceSoapBinding " name =" HelloWorldService ">

  < wsdlsoap:address location =" http://localhost:8080/axis/HelloWorldService.jws " />

  </ wsdl:port >

  </ wsdl:service >

webService 的名称和绑定的信息,以及访问的url 地址。

 

5 、下面编写客户端代码

代码如下:


 

package com.hoo.client;

 

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

public class HelloWorldClient {

 

    /**

      * <b> function: </b> jws axis WebService 客户端

      * @author hoojo

      * @createDate 2010 - 12 - 15 下午 05:10:28

      * @param args

      * @throws ServiceException

      * @throws RemoteException

      */

    public static void main(String[] args) throws ServiceException, RemoteException {

       //webService 访问地址

       //String url = "http://localhost:8080/axis/HelloWorldService.jws";

       String url = "http://localhost:8080/AxisWebService/HelloWorldService.jws" ;

       // 创建服务

       Service service = new Service();

       // 创建调用句柄

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

       // 设置请求地址

       call.setTargetEndpointAddress(url);

       /**

         * 设置调用的方法和方法的命名空间;

         * 因为这里是手动发布到 webroot 目录下的,所以命名空间和请求地址一致

         * 当然 null 也可以,因为本身它就没有设置命名空间,一般方法的命名空间是

         * 包名倒写组成,如 com.hoo.service,ns=http://service.hoo.com

         */

       call.setOperationName( new QName( null , "sayHello" ));

       /**

         * call 调用 sayHello 方法,设置请求的参数,返回的就是返回值了

         */

       String result = (String) call.invoke( new Object[] { "jack" , 99 });

       System. out .println(result);

    }

}

 

分析上面的代码

url 是根据xml 文件中的wsdlsoap:address location 的信息得到的,命名空间和方法名称是根据

<wsdl:operation name="sayHello">

  <wsdlsoap:operation soapAction="" />

- <wsdl:input name="sayHelloRequest">

  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded" />

  </wsdl:input>

- <wsdl:output name="sayHelloResponse">

  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWorldJWS.jws" use="encoded" />

  </wsdl:output>

的信息得到的,而请求参数和返回值的详细信息是在

<wsdl:message name="sayHelloRequest">

  <wsdl:part name="name" type="xsd:string" />

  <wsdl:part name="age" type="xsd:int" />

  </wsdl:message>

- <wsdl:message name="sayHelloResponse">

  <wsdl:part name="sayHelloReturn" type="xsd:string" />

  </wsdl:message>

- <wsdl:portType name="HelloWorldJWS">

- <wsdl:operation name="sayHello" parameterOrder="name age">

  <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />

  <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />

  </wsdl:operation>

  </wsdl:portType>

里可以很详细的看到。

至于代码的call.invokejava 中反射机制,不懂的建议看看jdk 文档java.lang.reflect 包下的内容。

运行上面的代码就可以看到控制台输出:

jack say : hello world! [axis] my age is 99

 

好了,axis 的就完成了,下面我们不用官方的axis 的工程,我们写一个自己的AxisWebService 工程,然后发布的tomcatwebapps 中看看。

 

6 、刚才copylib 下的jar 包,现在要copyweb.xml 中的内容,去掉里面的AdminServlet 这个配置,其他的都可保留。

然后像刚才一样,将HelloWorldService.java 复制到webroot 目录下,去掉包名,并且修改后缀为HelloWorldService.jws 即可。(如果有兴趣可以看看,发布在tomcat 目录下的当前工程的web-inf 目录,看看里面是否多了些东西)最后发布当前web 工程,访问 http://localhost:8080/AxisWebService/HelloWorldService.jws ,如果看到和刚才一样的界面,证明你快成功了。点击链接看到wsdlxml 就成功了。

好了,还没有完。看看web.xml 中的配置,你大概就知道为什么了。

web.xml 中最主要的文件就是org.apache.axis.transport.http.AxisServlet ,它就是webService 的中央控制器;即配置jws 的后缀也在web.xml 中定义的,在看看还有services/* ,这就表明上面的访问路径也可以是这样的: http://localhost:8080/AxisWebService/services/HelloWorldService

当然如果要这样写就需要用wsdd 的发布方式,详细请看下文!

 

参考文档: http://ws.apache.org/axis/java/user-guide.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值