使用Axis创建Web Service服务

先简短的说一下Web Service的概念,简略的总结一下:所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。 

    下面以Axis为例,创建一个简单的Web Service。首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-1.4的。下载之后观察其目录,lib下就是所要用到的所有jar包了。这里少2个jar包,分别是activation.jar和mail.jar,需要单独下载。 

    好了,现在我们创建一个Web Service项目,命名为webservice_begin。首先把lib目录下的所有jar包拷贝到WEB-INF的lib目录下。ok,现在我们创建接口和实现类。代码如下 
Java代码   收藏代码
  1. package server;  
  2.   
  3. public interface Hello {  
  4.   
  5.     public String getHello(String name);  
  6.   
  7. }  

Java代码   收藏代码
  1. package server;  
  2.   
  3. public class HelloService implements Hello {  
  4.       
  5.     public String getHello(String name) {  
  6.         return "hello, " + name;  
  7.     }  
  8. }  


接着我们修改下web.xml文件。修改web.xml文件的目的是为创建一个Axis的Servlet。Axis Servlet最核心的实现类为org.apache.axis.transport.http.AxisServlet,然后定义一个Servlet的请求路径为services/*,客户端就可以通过这个路径发布SOAP的请求消息了,下面是修改后的web.xml文件 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  3.      
  4.   <servlet>  
  5.     <servlet-name>AxisServlet</servlet-name>  
  6.     <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
  7.   </servlet>  
  8.     
  9.   <servlet-mapping>  
  10.     <servlet-name>AxisServlet</servlet-name>  
  11.     <url-pattern>/servlet/AxisServlet</url-pattern>  
  12.   </servlet-mapping>  
  13.     
  14.   <servlet-mapping>  
  15.     <servlet-name>AxisServlet</servlet-name>  
  16.     <url-pattern>*.jws</url-pattern>  
  17.   </servlet-mapping>  
  18.     
  19.   <servlet-mapping>  
  20.     <servlet-name>AxisServlet</servlet-name>  
  21.     <url-pattern>/services/*</url-pattern>  
  22.   </servlet-mapping>  
  23. </web-app>  


接下来,要配置wsdd文件server-config.wsdd。server-config.wsdd将定义服务的名称,具体的实现类,以及发布的方法和属性等。将其放在src目录下,也就是当前应用的classpath路径下,具体内容如下 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <deployment xmlns="http://xml.apache.org/axis/wsdd/"  
  3.     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  
  4.   <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>  
  5.     
  6.   <service name="hello" provider="java:RPC">  
  7.     <parameter name="className" value="server.HelloService"/>  
  8.     <parameter name="allowedMethods" value="getHello"/>  
  9.   </service>  
  10.     
  11.   <transport name="http">  
  12.     <requestFlow>  
  13.         <handler type="URLMapper"/>  
  14.     </requestFlow>  
  15.   </transport>  
  16. </deployment>  


好了,现在我们就可以发布Web Service服务了。 
部署成功后,启动你的应用服务器,在地址栏下输入 
http://localhost:8080/webservice_begin/services,如果没有错误提示,恭喜你,你的Web Service已经发布成功了。点击wsdl,还可以看到具体的wsdl的配置信息。 
这里面主要定义了以下内容:本例子的目标空间和命名空间、输入和输出变量的结构类型、请求和响应的消息、wsdl的接口portType、接口操作(Operation)、绑定的传输协议,具体内容如下 
Java代码   收藏代码
  1.   <?xml version="1.0" encoding="UTF-8" ?>   
  2. - <wsdl:definitions targetNamespace="http://localhost:8080/webservice_begin/services/hello" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/webservice_begin/services/hello" xmlns:intf="http://localhost:8080/webservice_begin/services/hello" 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">  
  3. - <!--   
  4. WSDL created by Apache Axis version: 1.4  
  5. Built on Apr 222006 (06:55:48 PDT)  
  6.   
  7.   -->   
  8. - <wsdl:message name="getHelloRequest">  
  9.   <wsdl:part name="name" type="soapenc:string" />   
  10.   </wsdl:message>  
  11. - <wsdl:message name="getHelloResponse">  
  12.   <wsdl:part name="getHelloReturn" type="soapenc:string" />   
  13.   </wsdl:message>  
  14. - <wsdl:portType name="HelloService">  
  15. - <wsdl:operation name="getHello" parameterOrder="name">  
  16.   <wsdl:input message="impl:getHelloRequest" name="getHelloRequest" />   
  17.   <wsdl:output message="impl:getHelloResponse" name="getHelloResponse" />   
  18.   </wsdl:operation>  
  19.   </wsdl:portType>  
  20. - <wsdl:binding name="helloSoapBinding" type="impl:HelloService">  
  21.   <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />   
  22. - <wsdl:operation name="getHello">  
  23.   <wsdlsoap:operation soapAction="" />   
  24. - <wsdl:input name="getHelloRequest">  
  25.   <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server" use="encoded" />   
  26.   </wsdl:input>  
  27. - <wsdl:output name="getHelloResponse">  
  28.   <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice_begin/services/hello" use="encoded" />   
  29.   </wsdl:output>  
  30.   </wsdl:operation>  
  31.   </wsdl:binding>  
  32. - <wsdl:service name="HelloServiceService">  
  33. - <wsdl:port binding="impl:helloSoapBinding" name="hello">  
  34.   <wsdlsoap:address location="http://localhost:8080/webservice_begin/services/hello" />   
  35.   </wsdl:port>  
  36.   </wsdl:service>  
  37.   </wsdl:definitions>  


到这里,服务器端的工作算是完成了。下面我们写一个客户端程序来试着调用一下。客户端程序可以是一个简单的javaProject。调用代码如下 
Java代码   收藏代码
  1. package junit.test;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import org.apache.axis.client.Call;  
  6. import org.apache.axis.client.Service;  
  7. import org.junit.Test;  
  8.   
  9.   
  10. public class WebServiceTest {  
  11.       
  12.     @Test public void testInvoke() throws Exception{  
  13.         //标识WebService的具体路径  
  14.         String endpoint = "http://localhost:8080/webservice_begin/services/hello";  
  15.         //创建Service实例  
  16.         Service service = new Service();  
  17.         //通过Service实例创建Call实例  
  18.         Call call = (Call)service.createCall();  
  19.         //将WebService的服务路径加入到Call实例中,并为Call设置服务的位置  
  20.         URL url = new URL(endpoint);  
  21.         call.setTargetEndpointAddress(url);  
  22.         //调用WebService方法  
  23.         call.setOperationName("getHello");  
  24.         //调用WebService传入参数  
  25.         String res = (String)call.invoke(new Object[] {"fengfeng925"});  
  26.         System.out.println(res);  
  27.   
  28.     }  
  29. }  


运行测试用例,哈哈,greenbar,运行成功 
输出为hello, fengfeng925 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值