天天看點

使用WSDL2java得到用戶端存根

存根:用戶端代理(stub),用于完成底層套接字程式設計

骨架:伺服器端代理(skeleton),也是完成底層套接字程式設計

 結構化方式描述

         Axis用三種方式支援WSDL

1、  url+wsdl方式獲得自動生成的wsdl文檔

2、  wsdl2Java工具能生成java用戶端存根和伺服器端骨架(常用指令-v:列印資訊,-p:指定包結構 –o:輸出指定的java檔案到指定目錄)

3、  java2WSDL工具可以通過java建構wsdl文檔

Axis的Wsdl2Java可以支援生成用戶端存根和骨架,下面是生成用戶端存根的例子:

伺服器端:

package cn.com.test.ce.webservice;

import cn.com.test.ce.pojo.Person;

public class Service

{

    public Person getPerson()

    {

             Person person = new Person();

                 person.setId(1);

                 person.setName("張三");

             return person;

    }

}

伺服器端PoJo類:

package cn.com.test.ce.pojo;

public class Person {

    private Integer id;

    private String name;

    public Integer getId() {

       return id;

    }

    public void setId(Integer id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

}

用戶端通過-v(列印資訊) –p(指定包名) cn.com.test.webservice.service –o(指定目錄名) src http://localhost:8080/MyAxisPoJoWebService/services/MyService?wsdl類似這樣的指令生成用戶端存根

然後用戶端可以回生成以下java檔案:

package cn.com.test.webservice.service;

public class MyServiceSoapBindingStub extends org.apache.axis.client.Stub implements cn.com.test.webservice.service.Service {

    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("getPerson");

        oper.setReturnType(new javax.xml.namespace.QName("urn:Service", "Person"));

        oper.setReturnClass(cn.com.test.webservice.service.Person.class);

        oper.setReturnQName(new javax.xml.namespace.QName("", "getPersonReturn"));

        oper.setStyle(org.apache.axis.constants.Style.RPC);

        oper.setUse(org.apache.axis.constants.Use.ENCODED);

        _operations[0] = oper;

    }

    public MyServiceSoapBindingStub() throws org.apache.axis.AxisFault {

         this(null);

    }

    public MyServiceSoapBindingStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {

         this(service);

         super.cachedEndpoint = endpointURL;

    }

    public MyServiceSoapBindingStub(javax.xml.rpc.Service service) throws org.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");

            java.lang.Class cls;

            javax.xml.namespace.QName qName;

            javax.xml.namespace.QName qName2;

            java.lang.Class beansf = org.apache.axis.encoding.ser.BeanSerializerFactory.class;

            java.lang.Class beandf = org.apache.axis.encoding.ser.BeanDeserializerFactory.class;

            java.lang.Class enumsf = org.apache.axis.encoding.ser.EnumSerializerFactory.class;

            java.lang.Class enumdf = org.apache.axis.encoding.ser.EnumDeserializerFactory.class;

            java.lang.Class arraysf = org.apache.axis.encoding.ser.ArraySerializerFactory.class;

            java.lang.Class arraydf = org.apache.axis.encoding.ser.ArrayDeserializerFactory.class;

            java.lang.Class simplesf = org.apache.axis.encoding.ser.SimpleSerializerFactory.class;

            java.lang.Class simpledf = org.apache.axis.encoding.ser.SimpleDeserializerFactory.class;

            java.lang.Class simplelistsf = org.apache.axis.encoding.ser.SimpleListSerializerFactory.class;

            java.lang.Class simplelistdf = org.apache.axis.encoding.ser.SimpleListDeserializerFactory.class;

            qName = new javax.xml.namespace.QName("urn:Service", "Person");

            cachedSerQNames.add(qName);

            cls = cn.com.test.webservice.service.Person.class;

            cachedSerClasses.add(cls);

            cachedSerFactories.add(beansf);

            cachedDeserFactories.add(beandf);

    }

    protected org.apache.axis.client.Call createCall() throws java.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));

            }

            // All the type mapping information is registered

            // when the first call is made.

            // The type mapping information is actually registered in

            // the TypeMappingRegistry of the service, which

            // is the reason why registration is only needed for the first call.

            synchronized (this) {

                if (firstCall()) {

                    // must set encoding style before registering serializers

                    _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);

                    _call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP11_ENC);

                    for (int i = 0; i < cachedSerFactories.size(); ++i) {

                        java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);

                        javax.xml.namespace.QName qName =

                                (javax.xml.namespace.QName) cachedSerQNames.get(i);

                        java.lang.Object x = cachedSerFactories.get(i);

                        if (x instanceof Class) {

                            java.lang.Class sf = (java.lang.Class)

                                 cachedSerFactories.get(i);

                            java.lang.Class df = (java.lang.Class)

                                 cachedDeserFactories.get(i);

                            _call.registerTypeMapping(cls, qName, sf, df, false);

                        }

                        else if (x instanceof javax.xml.rpc.encoding.SerializerFactory) {

                            org.apache.axis.encoding.SerializerFactory sf = (org.apache.axis.encoding.SerializerFactory)

                                 cachedSerFactories.get(i);

                            org.apache.axis.encoding.DeserializerFactory df = (org.apache.axis.encoding.DeserializerFactory)

                                 cachedDeserFactories.get(i);

                            _call.registerTypeMapping(cls, qName, sf, df, false);

                        }

                    }

                }

            }

            return _call;

        }

        catch (java.lang.Throwable _t) {

            throw new org.apache.axis.AxisFault("Failure trying to get the Call object", _t);

        }

    }

    public cn.com.test.webservice.service.Person getPerson() 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://webservice.ce.test.com.cn", "getPerson"));

        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 (cn.com.test.webservice.service.Person) _resp;

            } catch (java.lang.Exception _exception) {

                return (cn.com.test.webservice.service.Person) org.apache.axis.utils.JavaUtils.convert(_resp, cn.com.test.webservice.service.Person.class);

            }

        }

  } catch (org.apache.axis.AxisFault axisFaultException) {

  throw axisFaultException;

}

    }

}

package cn.com.test.webservice.service;

public class Person  implements java.io.Serializable {

    private java.lang.Integer id;

    private java.lang.String name;

    public Person() {

    }

    public Person(

           java.lang.Integer id,

           java.lang.String name) {

           this.id = id;

           this.name = name;

    }

    public java.lang.Integer getId() {

        return id;

    }

    public void setId(java.lang.Integer id) {

        this.id = id;

    }

    public java.lang.String getName() {

        return name;

    }

    public void setName(java.lang.String name) {

        this.name = name;

    }

    private java.lang.Object __equalsCalc = null;

    public synchronized boolean equals(java.lang.Object obj) {

        if (!(obj instanceof Person)) return false;

        Person other = (Person) obj;

        if (obj == null) return false;

        if (this == obj) return true;

        if (__equalsCalc != null) {

            return (__equalsCalc == obj);

        }

        __equalsCalc = obj;

        boolean _equals;

        _equals = true &&

            ((this.id==null && other.getId()==null) ||

             (this.id!=null &&

              this.id.equals(other.getId()))) &&

            ((this.name==null && other.getName()==null) ||

             (this.name!=null &&

              this.name.equals(other.getName())));

        __equalsCalc = null;

        return _equals;

    }

    private boolean __hashCodeCalc = false;

    public synchronized int hashCode() {

        if (__hashCodeCalc) {

            return 0;

        }

        __hashCodeCalc = true;

        int _hashCode = 1;

        if (getId() != null) {

            _hashCode += getId().hashCode();

        }

        if (getName() != null) {

            _hashCode += getName().hashCode();

        }

        __hashCodeCalc = false;

        return _hashCode;

    }

    // Type metadata

    private static org.apache.axis.description.TypeDesc typeDesc =

        new org.apache.axis.description.TypeDesc(Person.class, true);

    static {

        typeDesc.setXmlType(new javax.xml.namespace.QName("urn:Service", "Person"));

        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();

        elemField.setFieldName("id");

        elemField.setXmlName(new javax.xml.namespace.QName("", "id"));

        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "int"));

        elemField.setNillable(true);

        typeDesc.addFieldDesc(elemField);

        elemField = new org.apache.axis.description.ElementDesc();

        elemField.setFieldName("name");

        elemField.setXmlName(new javax.xml.namespace.QName("", "name"));

        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));

        elemField.setNillable(true);

        typeDesc.addFieldDesc(elemField);

    }

    public static org.apache.axis.description.TypeDesc getTypeDesc() {

        return typeDesc;

    }

    public static org.apache.axis.encoding.Serializer getSerializer(

           java.lang.String mechType,

           java.lang.Class _javaType, 

           javax.xml.namespace.QName _xmlType) {

        return

          new  org.apache.axis.encoding.ser.BeanSerializer(

            _javaType, _xmlType, typeDesc);

    }

    public static org.apache.axis.encoding.Deserializer getDeserializer(

           java.lang.String mechType,

           java.lang.Class _javaType, 

           javax.xml.namespace.QName _xmlType) {

        return

          new  org.apache.axis.encoding.ser.BeanDeserializer(

            _javaType, _xmlType, typeDesc);

    }

}

package cn.com.test.webservice.service;

public interface Service extends java.rmi.Remote {

    public cn.com.test.webservice.service.Person getPerson() throws java.rmi.RemoteException;

}

package cn.com.test.webservice.service;

public interface ServiceService extends javax.xml.rpc.Service {

    public java.lang.String getMyServiceAddress();

    public cn.com.test.webservice.service.Service getMyService() throws javax.xml.rpc.ServiceException;

    public cn.com.test.webservice.service.Service getMyService(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;

}

package cn.com.test.webservice.service;

public class ServiceServiceLocator extends org.apache.axis.client.Service implements cn.com.test.webservice.service.ServiceService {

    public ServiceServiceLocator() {

    }

    public ServiceServiceLocator(org.apache.axis.EngineConfiguration config) {

        super(config);

    }

    public ServiceServiceLocator(java.lang.String wsdlLoc, javax.xml.namespace.QName sName) throws javax.xml.rpc.ServiceException {

        super(wsdlLoc, sName);

    }

    // Use to get a proxy class for MyService

    private java.lang.String MyService_address = "http://localhost:8080/MyAxisPoJoWebService/services/MyService";

    public java.lang.String getMyServiceAddress() {

        return MyService_address;

    }

    // The WSDD service name defaults to the port name.

    private java.lang.String MyServiceWSDDServiceName = "MyService";

    public java.lang.String getMyServiceWSDDServiceName() {

        return MyServiceWSDDServiceName;

    }

    public void setMyServiceWSDDServiceName(java.lang.String name) {

        MyServiceWSDDServiceName = name;

    }

    public cn.com.test.webservice.service.Service getMyService() throws javax.xml.rpc.ServiceException {

       java.net.URL endpoint;

        try {

            endpoint = new java.net.URL(MyService_address);

        }

        catch (java.net.MalformedURLException e) {

            throw new javax.xml.rpc.ServiceException(e);

        }

        return getMyService(endpoint);

    }

    public cn.com.test.webservice.service.Service getMyService(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {

        try {

            cn.com.test.webservice.service.MyServiceSoapBindingStub _stub = new cn.com.test.webservice.service.MyServiceSoapBindingStub(portAddress, this);

            _stub.setPortName(getMyServiceWSDDServiceName());

            return _stub;

        }

        catch (org.apache.axis.AxisFault e) {

            return null;

        }

    }

    public void setMyServiceEndpointAddress(java.lang.String address) {

        MyService_address = address;

    }

    public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {

        try {

            if (cn.com.test.webservice.service.Service.class.isAssignableFrom(serviceEndpointInterface)) {

                cn.com.test.webservice.service.MyServiceSoapBindingStub _stub = new cn.com.test.webservice.service.MyServiceSoapBindingStub(new java.net.URL(MyService_address), this);

                _stub.setPortName(getMyServiceWSDDServiceName());

                return _stub;

            }

        }

        catch (java.lang.Throwable t) {

            throw new javax.xml.rpc.ServiceException(t);

        }

        throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface:  " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));

    }

    public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {

        if (portName == null) {

            return getPort(serviceEndpointInterface);

        }

        java.lang.String inputPortName = portName.getLocalPart();

        if ("MyService".equals(inputPortName)) {

            return getMyService();

        }

        else  {

            java.rmi.Remote _stub = getPort(serviceEndpointInterface);

            ((org.apache.axis.client.Stub) _stub).setPortName(portName);

            return _stub;

        }

    }

    public javax.xml.namespace.QName getServiceName() {

        return new javax.xml.namespace.QName("http://localhost:8080/MyAxisPoJoWebService/services/MyService", "ServiceService");

    }

    private java.util.HashSet ports = null;

    public java.util.Iterator getPorts() {

        if (ports == null) {

            ports = new java.util.HashSet();

            ports.add(new javax.xml.namespace.QName("http://localhost:8080/MyAxisPoJoWebService/services/MyService", "MyService"));

        }

        return ports.iterator();

    }

    public void setEndpointAddress(java.lang.String portName, java.lang.String address) throws javax.xml.rpc.ServiceException {

if ("MyService".equals(portName)) {

            setMyServiceEndpointAddress(address);

        }

        else

{ // Unknown Port Name

            throw new javax.xml.rpc.ServiceException(" Cannot set Endpoint Address for Unknown Port" + portName);

        }

    }

    public void setEndpointAddress(javax.xml.namespace.QName portName, java.lang.String address) throws javax.xml.rpc.ServiceException {

        setEndpointAddress(portName.getLocalPart(), address);

    }

}

最後通過面向對象的方式進行測試,不再像以前那樣繁瑣:

package cn.com.test.webservice;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import cn.com.test.webservice.service.Person;

import cn.com.test.webservice.service.Service;

import cn.com.test.webservice.service.ServiceServiceLocator;

public class Client02

{

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

    {

             Service service=   new ServiceServiceLocator().getMyService();

                       Person person =service.getPerson();

      System.out.println("張三"+person.getName());

    }

}