?
转自:http://fengyouhua.iteye.com/blog/49383? 并做一些修改
从安装 Axis 开始,
? ? 1、编写DII方式web服务,
? ? 2、Dynamic Proxy 方式访问服务,
? ? 3、wsdd 发布web服务
? ? 4、stub client 访问web服务
上面四个步骤,详解如何使用Axis开发 Web Service 的一个实例。
?
2、到 http://ws.Apache.org/Axis/网站下载Axis安装包,也可以 ? ? ? ? ? ? 从http://download.csdn.net/download/fangmingrui/643150下载。
3、解压缩安装包,将Axis_ZIP_PATH\Axis-version\webapps下的Axis包拷贝到TOMCAT_HOME\webapps\下,以下约定Axis_HOME为该TOMCAT_HOME\webapps\Axis目录
4、启动tomcat,访问http://localhost:8080/Axis 检查安装是否成功
5、以上步骤执行成功,可以开发webservice例子了
?
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface ( DII)(?这种方式的局限性太大)
2、Stubs方式(推荐使用)
3、Dynamic Proxy方式
二、编写DII(Dynamic Invocation Interface )方式web服务
1.编写服务端程序HelloClient
? ? ? ?public class HelloClient
? ? ? {
? ? ? ? ? ? ? ?public String getName(String name)
??? {
??????? return "hello "+name;
??? }
? ? ?}
? ? ?注意:这个类在改写为?HelloClient.jws的时候上面的package路径要删掉
? ? ?2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws
?3、访问连接http://localhost:8080/Axis/HelloClient.jws?wsdl,页面显示Axis自动生成的wsdl
?
? ? 4、编写访问服务的客户端 TestHelloClient.java
import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class HelloClient { public static void main(String[] args) { try { String endpoint = "http://localhost:8080/Axis/HelloClient.jws"; Service service = new Service(); Call call = null; call = (Call) service.createCall(); call.setOperationName(new QName("http://localhost:8080/Axis/HelloClient.jws", "getName")); call.setTargetEndpointAddress(new java.net.URL(endpoint)); String ret = (String) call.invoke(new Object[] { "zhangsan" }); System.out.println("return value is " + ret); } catch (Exception ex) { ex.printStackTrace(); } } }?
三、编写Dynamic Proxy方式访问服务
1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
2、编写代理接口
? ? ? ? public interface HelloClientInterface?extends java.rmi.Remote
{
??? public String getName(String name)
? throws java.rmi.RemoteException;
}
3、编写并执行客户端程序TestHelloClient.java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory; import java.net.URL; import javax.xml.namespace.QName; public class TestHelloClient { public static void main(String[] args) { try { String wsdlUrl = "http://localhost:8080/Axis/HelloClient.jws?wsdl"; String nameSpaceUri = "http://localhost:8080/Axis/HelloClient.jws"; String serviceName = "HelloClientService"; String portName = "HelloClient"; ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName)); HelloClientInterface proxy = (HelloClientInterface) afService .getPort(new QName(nameSpaceUri, portName), HelloClientInterface.class); System.out.println("return value is " + proxy.getName("john")); } catch (Exception ex) { ex.printStackTrace(); } } }?
四、Stubs方式:编写wsdd发布web服务,编写stub client访问web服务?
1、编写服务端程序server,SayHello.java,编译
- package?server;??
- ??
- public?class?SayHello?{??
- ????public?String?getName(String?name)?{??
- ????????return?"hello?"?+?name;??
- ????}??
- }??
2、编写wsdd文件?
deploy.wsdd文件内容如下:?
- <deployment?xmlns="http://xml.apache.org/axis/wsdd/"?xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">??????
- <service?name="SayHello"?provider="java:RPC">??????
- <parameter?name="className"?value="server.SayHello"/>??????
- <parameter?name="allowedMethods"?value="*"/>??????
- </service>??????
- </deployment>???
3、发布服务:?
编辑一个deploy.bat,Axis_Lib为axis.jar路径。内容如下:?
- set?Axis_Lib=D:\workspace\test\WEB-INF\lib??????
- set?Java_Cmd=java?-Djava.ext.dirs=%Axis_Lib%??????
- set?Axis_Servlet=http://localhost:8080/test/servlet/AxisServlet??????
- %Java_Cmd%?org.apache.axis.client.AdminClient?-l%Axis_Servlet%?deploy.wsdd???
执行这个批处理文件,这时候,如果提示成功的话,访问http://localhost:8080/test/services 就会显示服务列表。?
4、生成客户端client stub文件?
在浏览器上访问服务器端的服务,可以下载到WSDL文件,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。?
编写一个WSDL2Java.bat文件,其内容如下:?
- set?Axis_Lib=D:\workspace\test\WEB-INF\lib??????
- set?Java_Cmd=java?-Djava.ext.dirs=%Axis_Lib%??????
- set?Output_Path=D:\workspace\test\src??????
- set?Package=server.SayHello??????
- %Java_Cmd%?org.apache.axis.wsdl.WSDL2Java?-o%Output_Path%?-p%Package%?SayHello.wsdl???
执行这个批处理文件就可以生成client stub.?
生成的stub client文件列表为:
? SayHello.java,
SayHelloService.java,
SayHelloServiceLocator.java,
SayHelloSoapBindingStub.java .?
5、编写客户端程序,编译并执行?
下面是一段junit测试客户端代码。?
- import?java.net.URL;??
- import?junit.framework.Test;??
- import?junit.framework.TestCase;??
- import?junit.framework.TestSuite;??
- ??
- public?class?TestWSClient?extends?TestCase?{??
- ??
- ????public?TestWSClient(String?string)?{??
- ????????super(string);??
- ????}??
- ??
- ????public?void?SayHelloClient()?throws?Exception?{??
- ??
- ????????SayHelloService?service?=?new?SayHelloServiceLocator();??
- ????????SayHello_PortType?client?=?service.getSayHello();??
- ????????String?retValue?=?client.getName("clientname");??
- ????????System.out.println(retValue);??
- ??
- ????}??
- ??
- ????public?static?Test?suite()?{??
- ????????TestSuite?suite?=?new?TestSuite();??
- ????????suite.addTest(new?TestWSClient("SayHelloClient"));??
- ????????return?suite;??
- ????}??
- } ?
?