使用axis自动生成webservice客户端代码

利用AXIS生成客户端文件,使得开发webservice更加简单,以下是我的个人代码,

工具:

Myeclipse7.0,axis1.4,tomcat5.5,jdk1.5

一、             下载axis的jar包,我的是axis-bin-1_4.zip包,解压后有axis的lib目录,一会儿我们要用,

二、             在myeclip se中创建自己的webservice工程,这个在的文库里有,

三、             在C盘目录下创建一个批处理文件,我的名字是wsdltojava.bat,内容如下:

set Axis_Lib=E:\jar\axis\asixTool\axis-bin-1_4\axis-1_4\lib

set Java_Cmd=java-Djava.ext.dirs=%Axis_Lib%

set Axis_Servlet=http://localhost:8090/myAxis/services/HelloWorld?wsdl

%Java_Cmd%org.apache.axis.wsdl.WSDL2Java -u %Axis_Servlet%

备注:红色字体我的axis的目录,也就是刚才我解压出来的axis,

           蓝色是我的webservice的访问地址,其他的不用处理,

然后双击这个文件,产生以下几个文件:

(1)、目录是:C:\localhost\myAxis\services\HelloWorld

(2)、


四、             将这四个文件拷贝到你的客户端,创建客户端


运行即可

 

 

附录代码:

服务端

HelloWorld.java

package webservice.axis.test;
 
public class HelloWorld {
    public String hello(){
       return"Hello"+"aaaaaaaaaaaaaaaaaa";
    }
}


 

客户端(通过刚才命令生成的四个文件)

HelloWorld.java

/**
 *HelloWorld.java
 *
 *This file was auto-generated from WSDL
 * bythe Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */
 
package webservice.axis.test.batClient;
 
public interface HelloWorld extendsjava.rmi.Remote {
   public java.lang.String hello() throws java.rmi.RemoteException;
}


HelloWorldService.java

/**
 *HelloWorldService.java
 *
 *This file was auto-generated from WSDL
 * bythe Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */
 
package webservice.axis.test.batClient;
 
public interface HelloWorldService extendsjavax.xml.rpc.Service {
   public java.lang.String getHelloWorldAddress();
 
   public webservice.axis.test.batClient.HelloWorld getHelloWorld() throwsjavax.xml.rpc.ServiceException;
 
   public webservice.axis.test.batClient.HelloWorldgetHelloWorld(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}



HelloWorldServiceLocator.java

/**
 *HelloWorldServiceLocator.java
 *
 *This file was auto-generated from WSDL
 * bythe Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */
 
package webservice.axis.test.batClient;
 
public class HelloWorldServiceLocatorextends org.apache.axis.client.Service implementswebservice.axis.test.batClient.HelloWorldService {
 
    public HelloWorldServiceLocator() {
    }
 
 
   public HelloWorldServiceLocator(org.apache.axis.EngineConfigurationconfig) {
       super(config);
    }
 
   public HelloWorldServiceLocator(java.lang.String wsdlLoc,javax.xml.namespace.QName sName) throws javax.xml.rpc.ServiceException {
       super(wsdlLoc, sName);
    }
 
   // Use to get a proxy class for HelloWorld
   private java.lang.String HelloWorld_address ="http://localhost:8090/myAxis/services/HelloWorld";
 
   public java.lang.String getHelloWorldAddress() {
       return HelloWorld_address;
    }
 
   // The WSDD service name defaults to the port name.
   private java.lang.String HelloWorldWSDDServiceName ="HelloWorld";
 
   public java.lang.String getHelloWorldWSDDServiceName() {
       return HelloWorldWSDDServiceName;
    }
 
   public void setHelloWorldWSDDServiceName(java.lang.String name) {
       HelloWorldWSDDServiceName = name;
    }
 
   public webservice.axis.test.batClient.HelloWorld getHelloWorld() throwsjavax.xml.rpc.ServiceException {
      java.net.URL endpoint;
       try {
           endpoint = new java.net.URL(HelloWorld_address);
       }
       catch (java.net.MalformedURLException e) {
           throw new javax.xml.rpc.ServiceException(e);
       }
       return getHelloWorld(endpoint);
    }
 
   public webservice.axis.test.batClient.HelloWorldgetHelloWorld(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
       try {
           webservice.axis.test.batClient.HelloWorldSoapBindingStub _stub = newwebservice.axis.test.batClient.HelloWorldSoapBindingStub(portAddress, this);
           _stub.setPortName(getHelloWorldWSDDServiceName());
           return _stub;
       }
       catch (org.apache.axis.AxisFault e) {
           return null;
       }
    }
 
   public void setHelloWorldEndpointAddress(java.lang.String address) {
       HelloWorld_address = address;
    }
 
   /**
    * For the given interface, get the stub implementation.
    * If this service has no port for the given interface,
    * then ServiceException is thrown.
    */
   public java.rmi.Remote getPort(Class serviceEndpointInterface) throwsjavax.xml.rpc.ServiceException {
       try {
           if(webservice.axis.test.batClient.HelloWorld.class.isAssignableFrom(serviceEndpointInterface)){
               webservice.axis.test.batClient.HelloWorldSoapBindingStub _stub = newwebservice.axis.test.batClient.HelloWorldSoapBindingStub(new java.net.URL(HelloWorld_address),this);
               _stub.setPortName(getHelloWorldWSDDServiceName());
                return _stub;
           }
       }
       catch (java.lang.Throwable t) {
           throw new javax.xml.rpc.ServiceException(t);
       }
       throw new javax.xml.rpc.ServiceException("There is no stubimplementation for the interface:  "+ (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
    }
 
   /**
    * For the given interface, get the stub implementation.
    * If this service has no port for the given interface,
    * then ServiceException is thrown.
    */
   public java.rmi.Remote getPort(javax.xml.namespace.QName portName, ClassserviceEndpointInterface) throws javax.xml.rpc.ServiceException {
       if (portName == null) {
           return getPort(serviceEndpointInterface);
       }
       java.lang.String inputPortName = portName.getLocalPart();
       if ("HelloWorld".equals(inputPortName)) {
           return getHelloWorld();
       }
       else  {
           java.rmi.Remote _stub = getPort(serviceEndpointInterface);
           ((org.apache.axis.client.Stub) _stub).setPortName(portName);
           return _stub;
       }
    }
 
   public javax.xml.namespace.QName getServiceName() {
       return newjavax.xml.namespace.QName("http://localhost:8090/myAxis/services/HelloWorld","HelloWorldService");
    }
 
   private java.util.HashSet ports = null;
 
   public java.util.Iterator getPorts() {
       if (ports == null) {
           ports = new java.util.HashSet();
           ports.add(newjavax.xml.namespace.QName("http://localhost:8090/myAxis/services/HelloWorld","HelloWorld"));
       }
       return ports.iterator();
    }
 
   /**
    *Set the endpoint address for the specified port name.
   */
   public void setEndpointAddress(java.lang.String portName,java.lang.String address) throws javax.xml.rpc.ServiceException {
       
if("HelloWorld".equals(portName)) {
           setHelloWorldEndpointAddress(address);
       }
       else
{ // UnknownPort Name
           throw new javax.xml.rpc.ServiceException(" Cannot set EndpointAddress for Unknown Port" + portName);
       }
    }
 
   /**
    *Set the endpoint address for the specified port name.
   */
   public void setEndpointAddress(javax.xml.namespace.QName portName,java.lang.String address) throws javax.xml.rpc.ServiceException {
       setEndpointAddress(portName.getLocalPart(), address);
    }
 
}


 


HelloWorldSoapBindingStub.java

/**
 *HelloWorldSoapBindingStub.java
 *
 *This file was auto-generated from WSDL
 * bythe Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */
 
package webservice.axis.test.batClient;
 
public class HelloWorldSoapBindingStubextends org.apache.axis.client.Stub implementswebservice.axis.test.batClient.HelloWorld {
   private java.util.Vector cachedSerClasses = new java.util.Vector();
   private java.util.Vector cachedSerQNames = new java.util.Vector();
   private java.util.Vector cachedSerFactories = new java.util.Vector();
   private java.util.Vector cachedDeserFactories = new java.util.Vector();
 
   static org.apache.axis.description.OperationDesc [] _operations;
 
   static {
       _operations = new org.apache.axis.description.OperationDesc[1];
       _initOperationDesc1();
    }
 
   private static void _initOperationDesc1(){
       org.apache.axis.description.OperationDesc oper;
       org.apache.axis.description.ParameterDesc param;
       oper = new org.apache.axis.description.OperationDesc();
       oper.setName("hello");
       oper.setReturnType(newjavax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema","string"));
       oper.setReturnClass(java.lang.String.class);
       oper.setReturnQName(new javax.xml.namespace.QName("","helloReturn"));
       oper.setStyle(org.apache.axis.constants.Style.RPC);
       oper.setUse(org.apache.axis.constants.Use.ENCODED);
       _operations[0] = oper;
 
    }
 
   public HelloWorldSoapBindingStub() throws org.apache.axis.AxisFault {
        this(null);
    }
 
   public HelloWorldSoapBindingStub(java.net.URL endpointURL,javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
        this(service);
        super.cachedEndpoint =endpointURL;
    }
 
   public HelloWorldSoapBindingStub(javax.xml.rpc.Service service) throwsorg.apache.axis.AxisFault {
       if (service == null) {
           super.service = new org.apache.axis.client.Service();
        } else {
           super.service = service;
       }
       ((org.apache.axis.client.Service)super.service).setTypeMappingVersion("1.2");
    }
 
   protected org.apache.axis.client.Call createCall() throwsjava.rmi.RemoteException {
       try {
           org.apache.axis.client.Call _call = super._createCall();
           if (super.maintainSessionSet) {
               _call.setMaintainSession(super.maintainSession);
           }
           if (super.cachedUsername != null) {
               _call.setUsername(super.cachedUsername);
           }
           if (super.cachedPassword != null) {
               _call.setPassword(super.cachedPassword);
           }
           if (super.cachedEndpoint != null) {
                _call.setTargetEndpointAddress(super.cachedEndpoint);
           }
           if (super.cachedTimeout != null) {
               _call.setTimeout(super.cachedTimeout);
           }
           if (super.cachedPortName != null) {
               _call.setPortName(super.cachedPortName);
           }
           java.util.Enumeration keys = super.cachedProperties.keys();
           while (keys.hasMoreElements()) {
                java.lang.String key =(java.lang.String) keys.nextElement();
                _call.setProperty(key,super.cachedProperties.get(key));
           }
           return _call;
       }
       catch (java.lang.Throwable _t) {
           throw new org.apache.axis.AxisFault("Failure trying to get the Callobject", _t);
       }
    }
 
   public java.lang.String hello() throws java.rmi.RemoteException {
       if (super.cachedEndpoint == null) {
           throw new org.apache.axis.NoEndPointException();
       }
       org.apache.axis.client.Call _call = createCall();
       _call.setOperation(_operations[0]);
       _call.setUseSOAPAction(true);
       _call.setSOAPActionURI("");
       _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
       _call.setOperationName(new javax.xml.namespace.QName("http://test.axis.webservice","hello"));
 
       setRequestHeaders(_call);
       setAttachments(_call);
 try{        java.lang.Object _resp =_call.invoke(new java.lang.Object[] {});
 
       if (_resp instanceof java.rmi.RemoteException) {
           throw (java.rmi.RemoteException)_resp;
       }
       else {
           extractAttachments(_call);
           try {
                return (java.lang.String)_resp;
            } catch (java.lang.Exception_exception) {
                return (java.lang.String)org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String.class);
           }
       }
  }catch (org.apache.axis.AxisFault axisFaultException) {
 throw axisFaultException;
}
    }
 
}


最后要感谢网上资源

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Axis2调用WebService服务需要以下步骤: 1. 下载Axis2库 首先需要下载Axis2库,可以从官方网站或者Maven仓库中获取到Axis2的jar包。下载好后,需要在项目中引入Axis2相关的jar包。 2. 创建客户端代理 使用wsdl2java命令,可以根据服务端的wsdl文件生成客户端代码。命令格式如下: ``` wsdl2java -uri http://localhost:8080/axis2/services/MyService?wsdl -p com.example.client -d /path/to/output ``` 其中,-uri参数指定服务端的wsdl文件地址,-p参数指定生成的客户端代码的包名,-d参数指定生成的客户端代码的输出目录。 生成的客户端代码包括服务端的接口类、接口实现类、客户端代理类等。 3. 创建客户端 创建客户端代码如下: ``` // 创建服务地址 String url = "http://localhost:8080/axis2/services/MyService"; // 创建服务代理 MyServiceStub stub = new MyServiceStub(url); // 调用服务方法 MyServiceStub.MyMethod request = new MyServiceStub.MyMethod(); request.setParam1("param1"); request.setParam2("param2"); MyServiceStub.MyMethodResponse response = stub.myMethod(request); ``` 其中,url参数指定服务地址,MyServiceStub是客户端代理类,MyMethod是服务端的方法,MyMethodResponse是服务端方法的返回值。 4. 调用服务 调用服务的代码如下: ``` // 创建请求消息 OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://example.com", "ns"); OMElement method = fac.createOMElement("myMethod", omNs); OMElement param1 = fac.createOMElement("param1", omNs); param1.setText("param1"); method.addChild(param1); OMElement param2 = fac.createOMElement("param2", omNs); param2.setText("param2"); method.addChild(param2); // 创建服务客户端 ServiceClient serviceClient = new ServiceClient(); Options options = new Options(); options.setTo(new EndpointReference(url)); serviceClient.setOptions(options); // 调用服务 OMElement response = serviceClient.sendReceive(method); ``` 其中,OMFactory和OMNamespace是Axis2提供的类,用于构建SOAP消息。OMElement是SOAP消息的元素,addChild方法用于添加子元素。ServiceClient是Axis2提供的服务客户端类,Options类用于设置服务调用的选项,包括服务地址、超时时间等。sendReceive方法用于发送请求消息并接收服务端的响应消息。 以上就是使用Axis2调用WebService服务的基本步骤。需要注意的是,调用服务时需要根据服务端的要求构建SOAP消息,具体内容需要根据服务端的接口文档进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值