Skip to main content

Provider examples

The following shows a Provider implementation that works with SOAPMessage objects in message mode.

import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="stockQuoteReporterPort" 
   serviceName="stockQuoteReporter")
@ServiceMode(value="Service.Mode.MESSAGE")
public class  stockQuoteReporterProvider implements Provider<SOAPMessage> {
   public stockQuoteReporterProvider() {}

   public SOAPMessage invoke(SOAPMessage request) {
      SOAPBody requestBody = request.getSOAPBody();
      if (requestBody.getElementName.getLocalName.equals("getStockPrice")) {
         MessageFactory mf = MessageFactory.newInstance();
         SOAPFactory sf = SOAPFactory.newInstance();

         SOAPMessage response = mf.createMessage();
         SOAPBody respBody = response.getSOAPBody();
         Name bodyName = sf.createName("getStockPriceResponse");
         respBody.addBodyElement(bodyName);
         SOAPElement respContent = respBody.addChildElement("price");
         respContent.setValue("123.00");
         response.saveChanges();
         return response;
      }
      ...
   }
}

The code does the following:

  1. Specifies that the following class implements a Provider object that implements the service whose wsdl:service element is named stockQuoteReporter and whose wsdl:port element is named stockQuoteReporterPort .

  2. Specifies that this Provider implementation uses message mode.

  3. Provides the required default public constructor.

  4. Provides an implementation of the invoke() method that takes a SOAPMessage object and returns a SOAPMessage object.

  5. Extracts the request message from the body of the incoming SOAP message.

  6. Checks the root element of the request message to determine how to process the request.

  7. Creates the factories needed for building the response.

  8. Builds the SOAP message for the response.

  9. Returns the response as a SOAPMessage object.

The following shows an example of a Provider implementation using DOMSource objects in payload mode.

import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="stockQuoteReporterPort" 
   serviceName="stockQuoteReporter")
@ServiceMode(value="Service.Mode.PAYLOAD")
public class stockQuoteReporterProvider implements Provider<DOMSource> {
   public stockQuoteReporterProvider() {}

   public DOMSource invoke(DOMSource request) {
      DOMSource response = new DOMSource();
      ...
      return response;
   }
}

The code does the following:

  1. Specifies that the class implements a Provider object that implements the service whose wsdl:service element is named stockQuoteReporter and whose wsdl:port element is named stockQuoteReporterPort .

  2. Specifies that this Provider implementation uses payload mode.

  3. Provides the required default public constructor.

  4. Provides an implementation of the invoke() method that takes a DOMSource object and returns a DOMSource object.

The following shows a Provider implementation that works with SOAPMessage objects in message mode.

import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="stockQuoteReporterPort" 
   serviceName="stockQuoteReporter")
@ServiceMode(value="Service.Mode.MESSAGE")
public class  stockQuoteReporterProvider implements Provider<SOAPMessage> {
   public stockQuoteReporterProvider() {}

   public SOAPMessage invoke(SOAPMessage request) {
      SOAPBody requestBody = request.getSOAPBody();
      if (requestBody.getElementName.getLocalName.equals("getStockPrice")) {
         MessageFactory mf = MessageFactory.newInstance();
         SOAPFactory sf = SOAPFactory.newInstance();

         SOAPMessage response = mf.createMessage();
         SOAPBody respBody = response.getSOAPBody();
         Name bodyName = sf.createName("getStockPriceResponse");
         respBody.addBodyElement(bodyName);
         SOAPElement respContent = respBody.addChildElement("price");
         respContent.setValue("123.00");
         response.saveChanges();
         return response;
      }
      ...
   }
}

The code does the following:

  1. Specifies that the following class implements a Provider object that implements the service whose wsdl:service element is named stockQuoteReporter and whose wsdl:port element is named stockQuoteReporterPort .

  2. Specifies that this Provider implementation uses message mode.

  3. Provides the required default public constructor.

  4. Provides an implementation of the invoke() method that takes a SOAPMessage object and returns a SOAPMessage object.

  5. Extracts the request message from the body of the incoming SOAP message.

  6. Checks the root element of the request message to determine how to process the request.

  7. Creates the factories needed for building the response.

  8. Builds the SOAP message for the response.

  9. Returns the response as a SOAPMessage object.

The following shows an example of a Provider implementation using DOMSource objects in payload mode.

import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="stockQuoteReporterPort" 
   serviceName="stockQuoteReporter")
@ServiceMode(value="Service.Mode.PAYLOAD")
public class stockQuoteReporterProvider implements Provider<DOMSource> {
   public stockQuoteReporterProvider() {}

   public DOMSource invoke(DOMSource request) {
      DOMSource response = new DOMSource();
      ...
      return response;
   }
}

The code does the following:

  1. Specifies that the class implements a Provider object that implements the service whose wsdl:service element is named stockQuoteReporter and whose wsdl:port element is named stockQuoteReporterPort .

  2. Specifies that this Provider implementation uses payload mode.

  3. Provides the required default public constructor.

  4. Provides an implementation of the invoke() method that takes a DOMSource object and returns a DOMSource object.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!