webservice中使用自定义类型

服务器端部署文件

server-config.wsdd

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="C:\jakarta-tomcat\webapps\axis\WEB-INF\attachments"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="axis.sendMinimizedElements" value="true"/>
  <requestFlow>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="session"/>
   </handler>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="request"/>
    <parameter name="extension" value=".jwr"/>
   </handler>
  </requestFlow>
 </globalConfiguration>
 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <service name="AdminService" provider="java:MSG">
  <parameter name="allowedMethods" value="AdminService"/>
  <parameter name="enableRemoteAdmin" value="false"/>
  <parameter name="className" value="org.apache.axis.utils.Admin"/>
  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
 </service>
 <service name="urn:netstoreservice" provider="java:RPC">
  <parameter name="allowedMethods" value="authenticate,getFeaturedItems,getItemDetailView"/>
  <parameter name="className" value="netstore.service.NetstoreServiceImpl"/>
 </service>
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
 <beanMapping languageSpecificType="java:netstore.catalog.view.ItemDetailView" qname="ns1:ItemDetailView" xmlns:ns1="urn:BeanService"/>
 <beanMapping languageSpecificType="java:java.util.List" qname="ns2:List" xmlns:ns2="urn:BeanService"/>
 <beanMapping languageSpecificType="java:netstore.catalog.view.ItemSummaryView" qname="ns3:ItemSummaryView" xmlns:ns3="urn:BeanService"/>
 <beanMapping languageSpecificType="java:java.util.Date" qname="ns4:Date" xmlns:ns4="urn:BeanService"/>
 <beanMapping languageSpecificType="java:netstore.customer.view.UserView" qname="ns5:UserView" xmlns:ns5="urn:BeanService"/>
</deployment>

 

客户端程序(java):

NetstoreWebServiceDelegate.java:

 

package netstore.service;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.List;
import java.util.Date;
import javax.servlet.ServletContext;
import netstore.catalog.view.ItemDetailView;
import netstore.catalog.view.ItemSummaryView;
import netstore.customer.view.UserView;
import netstore.framework.exceptions.*;
import netstore.service.INetstoreService;

/**
* This class is a business delegate that supports the implementation of the
* INetstoreService interface using the Netstore session bean.
*/
public class NetstoreWebServiceDelegate implements INetstoreService {

  private Service netstoreService;
  ServletContext servletContext = null;
  String endpoint=null;
  public NetstoreWebServiceDelegate(  ) {
    init(  );
  }

  private void init(  ) {
    try {
      endpoint = "http://localhost:8080/axis/services/netstoreservice";
      netstoreService = new Service();
    }
    catch (Exception e) {
      throw new RuntimeException(e.getMessage(  ));
    }
  }

  public UserView authenticate( String email, String password )
  throws InvalidLoginException, ExpiredPasswordException,
  AccountLockedException, DatastoreException {
    try {
        Call     call    = (Call) netstoreService.createCall();
        QName qn = new QName("urn:BeanService","UserView");
        Class cls = UserView.class;
        call.registerTypeMapping(cls, qn,
        new org.apache.axis.encoding.ser.BeanSerializerFactory(cls, qn),
        new org.apache.axis.encoding.ser.BeanDeserializerFactory(cls, qn));

        QName dateqn = new QName("urn:BeanService","Date");
        Class datecls = Date.class;
        call.registerTypeMapping(datecls, dateqn,
        new org.apache.axis.encoding.ser.BeanSerializerFactory(datecls, dateqn),
        new org.apache.axis.encoding.ser.BeanDeserializerFactory(datecls, dateqn));

        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
        call.setOperationName(new QName("urn:netstoreservice", "authenticate") );
        call.addParameter( "arg1",  org.apache.axis.encoding.XMLType.XSD_STRING , ParameterMode.IN );
        call.addParameter( "arg2",  org.apache.axis.encoding.XMLType.XSD_STRING , ParameterMode.IN );
        call.setReturnType(qn,UserView.class);

        return (UserView) call.invoke( new Object[] { email,password } );

    }
    catch (javax.xml.rpc.ServiceException e) {
        throw DatastoreException.datastoreError(e);
    }catch (java.net.MalformedURLException e) {
        throw DatastoreException.datastoreError(e);
    }catch(java.rmi.RemoteException e){
        throw DatastoreException.datastoreError(e);
    }
  }

  public List getFeaturedItems(  ) throws DatastoreException {
    try {
      Call     call    = (Call) netstoreService.createCall();

      QName sumqn = new QName("urn:BeanService","ItemSummaryView");
      Class sumcls = ItemSummaryView.class;
      call.registerTypeMapping(sumcls, sumqn,
      new org.apache.axis.encoding.ser.BeanSerializerFactory(sumcls, sumqn),
      new org.apache.axis.encoding.ser.BeanDeserializerFactory(sumcls, sumqn));


      QName dateqn = new QName("urn:BeanService","Date");
      Class datecls = Date.class;
      call.registerTypeMapping(datecls, dateqn,
      new org.apache.axis.encoding.ser.BeanSerializerFactory(datecls, dateqn),
      new org.apache.axis.encoding.ser.BeanDeserializerFactory(datecls, dateqn));

      QName qn = new QName("urn:BeanService","List");
      Class cls = List.class;
      call.registerTypeMapping(cls, qn,
      new org.apache.axis.encoding.ser.BeanSerializerFactory(cls, qn),
      new org.apache.axis.encoding.ser.BeanDeserializerFactory(cls, qn));

 

      call.setTargetEndpointAddress( new java.net.URL(endpoint) );
      call.setOperationName(new QName("urn:netstoreservice", "getFeaturedItems") );
      call.setReturnType(qn,List.class);
      return (List) call.invoke(new Object[] {} );

    }
    catch (javax.xml.rpc.ServiceException e) {
      throw DatastoreException.datastoreError(e);
    }catch (java.net.MalformedURLException e) {
      throw DatastoreException.datastoreError(e);
    }catch(java.rmi.RemoteException e){
      e.printStackTrace();
      throw DatastoreException.datastoreError(e);
    }
  }

  public ItemDetailView getItemDetailView( String itemId )
  throws DatastoreException {
    try {
      Call     call    = (Call) netstoreService.createCall();
      call.setTargetEndpointAddress( new java.net.URL(endpoint) );

      QName dateqn = new QName("urn:BeanService","Date");
      Class datecls = Date.class;
      call.registerTypeMapping(datecls, dateqn,
      new org.apache.axis.encoding.ser.BeanSerializerFactory(datecls, dateqn),
      new org.apache.axis.encoding.ser.BeanDeserializerFactory(datecls, dateqn));

      QName qn = new QName("urn:BeanService","ItemDetailView");
      Class cls = ItemDetailView.class;
      call.registerTypeMapping(cls, qn,
      new org.apache.axis.encoding.ser.BeanSerializerFactory(cls, qn),
      new org.apache.axis.encoding.ser.BeanDeserializerFactory(cls, qn));
      call.setOperationName(new QName("urn:netstoreservice", "getItemDetailView") );
      call.addParameter( "arg1",  org.apache.axis.encoding.XMLType.XSD_STRING , ParameterMode.IN );
      call.setReturnType(qn,ItemDetailView.class);

      return (ItemDetailView) call.invoke( new Object[]{ itemId } );

    }
    catch (javax.xml.rpc.ServiceException e) {
      throw DatastoreException.datastoreError(e);
    }catch (java.net.MalformedURLException e) {
      throw DatastoreException.datastoreError(e);
    }catch(java.rmi.RemoteException e){
      throw DatastoreException.datastoreError(e);
    }
  }

  public void logout( String email ) {
  // Do nothing for this example
  }

  public void destroy(  ) {
  // Do nothing for this example
  }
  public void setServletContext( ServletContext ctx ){
    this.servletContext = ctx;
  }

  public ServletContext getServletContext(){
    return servletContext;
  }

  public static void main(String args[])throws Exception{
    NetstoreWebServiceDelegate service=new  NetstoreWebServiceDelegate();
    List l=service.getFeaturedItems();

    ItemDetailView view2= service.getItemDetailView("110");
    System.out.println(view2.getDescription()+";"+view2.getProductFeature());

    UserView view3=service.authenticate("[email protected]","weiqin");
    System.out.println(view3.getFirstName()+view3.getLastName());

  }

}

 

详细代码见:孙卫琴<<精通 struts>>书带源码。

 

 

你可能感兴趣的:(java,apache,xml,.net,webservice)