bookpriceproxy.java

来自「java web services how to program」· Java 代码 · 共 92 行

JAVA
92
字号
// BookPriceProxy.java
// BookPriceProxy is a service proxy for each book store抯 
// Book Price Web service.
package jws1casestudy.pricefinder;

// Java core packages
import java.net.URL;

// Java XML packages
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

// Axis packages
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;

// Deitel packages
import jws1casestudy.pricefinder.common.PriceQuote;

public class BookPriceProxy
{
   // Web service invocation object
   private Call call;
   
   // constructor
   public BookPriceProxy( URL serviceWSDL, 
      QName serviceNamespace, QName priceQuoteNamespace,
      String portName ) throws ServiceException
   {    
      // construct a service object bases on WSDL
      Service service = 
         new Service( serviceWSDL, serviceNamespace );
      
      // create getPrice identifier
      QName getPriceNamespace = 
         new QName( null, portName );
      
      // create a Call that specifies the service port and
      // method to call
      call = ( Call )service.createCall(
         getPriceNamespace, "getPrice" );
      
      // create serializer and deserializer
      BeanSerializerFactory serializerFactory =
         new BeanSerializerFactory( PriceQuote.class, 
            priceQuoteNamespace );
      
      BeanDeserializerFactory deserializerFactory =
         new BeanDeserializerFactory( PriceQuote.class,
            priceQuoteNamespace );
      
      // register type mapping for PriceQuote's serializer
      // and deserializer
      call.registerTypeMapping( PriceQuote.class,
         priceQuoteNamespace, serializerFactory, 
         deserializerFactory );

   } // end constructor
   
   // get price from one book store using DII
   public PriceQuote getPrice( String isbn )
   {
      // response object
      PriceQuote response = null;
      
      // invoke Book Price Web service
      try {

         // invoke Web service method
         Object responseObj = call.invoke( 
            new Object[] { isbn } );

         // cast return object to PriceQuote
         response = ( PriceQuote ) responseObj;

      } // end try

      // handle exception in accessing Web service
      catch ( Exception exception){
         exception.printStackTrace();
      }

      return response;

   } // end method getPrice
   
} // end class BookPriceProxy

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?