ITPub博客

首页 > 应用开发 > IT综合 > AXIS开发WEB SERVICE 二

AXIS开发WEB SERVICE 二

原创 IT综合 作者:scyulin 时间:2007-11-30 16:43:30 0 删除 编辑
AXIS开发WEB SERVICE 二[@more@]
一、Axis安装
  1、环境 J2SE SDK 1.3 or 1.4:我使用 1.4.2
    Servlet Container:我使用的Tomcat 5.0

  2、到 http://ws.Apache.org/axis/网站下载Axis安装包

  3、解压缩安装包,将Axis UNZIP_PATHAxis-versionwebapps下的Axis包拷贝到TOMCAT_HOMEwebapps下,以下约定Axis_HOME为该TOMCAT_HOMEwebappsaxis目录

  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.编写服务端程序SayHello.java

public class SayHello {
public String getName(String name) {
return "hello "+name;
}
}

  2、将源码拷贝到Axis_HOME下,重命名为SayHello.jws

  3、访问连接http://localhost:8080/axis/SayHello.jws?wsdl,页面显示Axis自动生成的wsdl

  4、编写访问服务的客户端 SayHelloClient.java

import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;

public class SayHelloClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/axis/SayHello.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName("http://localhost:8080/axis/SayHello.jws",
"getName"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[] {"someone's name"});
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/SayHello.jws?wsdl";
String nameSpaceUri = "http://localhost:8080/axis/SayHello.jws";
String serviceName = "SayHelloService";
String portName = "SayHello";

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("someone's name") ) ;
} catch(Exception ex) {
ex.printStackTrace() ;
}
}
}

四、编写wsdd发布web服务,编写stub client访问web服务

  1、编写服务端程序SayHello.java,编译SayHello.java

package server;
public class SayHello {
public String getName(String name) {
return "hello "+name;
}
}

2. 编写LogHandler.java

import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

import java.util.Date;

public class LogHandler extends BasicHandler {
public void invoke(MessageContext msgContext) throws AxisFault {
/** Log an access each time we get invoked. */
try {
Handler serviceHandler = msgContext.getService();
Integer numAccesses = (Integer)serviceHandler.getOption("accesses");
if (numAccesses == null) numAccesses = new Integer(0);
numAccesses = new Integer(numAccesses.intValue() + 1);
Date date = new Date();
String result = date + ": service " + msgContext.getTargetService() + " accessed " + numAccesses + " time(s).";
serviceHandler.setOption("accesses", numAccesses);
System.out.println(result);
} catch (Exception e) {
throw AxisFault.makeFault(e);
}
}
}

  3、编写wsdd文件


Axis/wsdd/"
xmlns:java= "http://xml.apache.org/Axis/wsdd/providers/java">

<service name="SayHello" provider="java:RPC">





service>

  4、将编译后的SayHello.class和LogHandler.class文件拷贝到Axis_HOME/WEB-INF/classes下,如:D:tomcatwebappsaxisWEB-INFclasses

  5、发布服务:

  java org.apache.axis.client.AdminClient deploy.wsdd

  6、生成client stub文件

  方式一:jws即时自动部署

  将SayHello.java拷贝到Axis_HOME/services目录下,重命名为SayHello.jws,执行下面的命令生成client stub

java org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8080/axis/services/SayHello.jws?wsdl

  注意:这种方式要求在你写的类中不能有具体包的信息,因为这正是Axis即时部署不支持的。由于在SayHello.java中特意加入了"package server;",因此不能采用jws即时自动部署。

  自动部署相当简单,但当你需要:

  .映射自己的类型时
  .不需要暴露源代码时
  .需要自己的路径和包管理时
  .对用户操作Web Services的事件进行相应时

  就需要通过WSDD来自定义部署。

  方式二:通过WSDD自定义部署

  执行如下命令生成SayHello.wsdl

java org.apache.axis.wsdl.Java2WSDL -oSayHello.wsdl -lhttp://localhost:8080/axis/services/SayHello -nSayHello server.SayHello

  执行如下命令生成client stub

java org.apache.axis.wsdl.WSDL2Java SayHello.wsdl -p client

  这时在当前目录下生成了client子目录,在client目录中生成的stub client文件列表为:

  (1).SayHello_PortType.java

  (2).SayHelloService.java

  (3).SayHelloServiceLocator.java

  (4).SayHelloSoapBindingStub.java

  6、编写客户端程序,编译并执行

import client.*;

public class SayHelloClient {
public static void main(String[] args){
try{
SayHelloService service = new client.SayHelloServiceLocator();
client.SayHello_PortType client = service.getSayHello();
String retValue=client.getName("someone's name");
System.out.println(retValue);
} catch (Exception e) {
System.err.println("Execution failed. Exception: " + e);
}
}
}

  您也可以编写server-config.wsdd,存放在Axis_HOME/WEB-INF目录下。

  
  axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  
   
   value="org.apache.axis.attachments.AttachmentsImpl"/>
   
   
   
   axis.sendMinimizedElements" value="true"/>
   
    axis.handlers.JWSHandler">
     
    

    axis.handlers.JWSHandler">
     
     
    

   

  

  axis.transport.local.LocalResponder"/>
  axis.handlers.http.URLMapper"/>
  axis.handlers.SimpleAuthenticationHandler"/>
  
  <service name="sayhello" provider="java:RPC">
   
    
   

   
   
  service>
  
   
    
    axis.handlers.http.HTTPAuthHandler"/>
   

  

  
   
    
   

  

  

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1696355/viewspace-987046/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 论软件外包
请登录后发表评论 登录
全部评论
  • 博文量
    7
  • 访问量
    12244