dcsimg
June 24, 2018
Hot Topics:

User Defined Exceptions: Improve Error Handling in Web Services

  • March 29, 2005
  • By Ayyappan Gandhirajan
  • Send Email »
  • More Articles »

MyCustomException class

The pseudo implementation for the custom exception is given below:

package WS.exception;
public class MyCustomException extends Exception{
   private String   errorCode;
   private String   errorDescription;
   private String[] variables;

   public MyCustomException(String errorCode,
                            String errorDescription,
                            String[] variables){
      this.errorCode = errorCode;
      this.errorDescription = errorDescription;
      this.variables = variables;
   }
   //all setter and getter methods need to be implemented here
}

WSDL Fault

The description of wsdl:fault is given below:

  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:stns="java:WS.exception"
    elementFormDefault="qualified"
    attributeFormDefault="qualified"
    targetNamespace="java:WS.exception">
   <xsd:import namespace="java:language_builtins.lang">
   </xsd:import>
   <xsd:element type="stns:MyCustomException"
     name="MyCustomException">
   </xsd:element>
   <xsd:complexType name="MyCustomException">
    <xsd:sequence>
     <xsd:element type="xsd:string"
       name="errorCode"
       minOccurs="1"
       nillable="true"
       maxOccurs="1">
     </xsd:element>
     <xsd:element type="xsd:string"
       name="errorDescription"
       minOccurs="1"
       nillable="true"
       maxOccurs="1">
     </xsd:element>
     <xsd:element xmlns:tp="java:language_builtins.lang"
       type="tp:ArrayOfString"
       name="variables"
       minOccurs="1"
       nillable="true"
       maxOccurs="1">
     </xsd:element>
    </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

<message name="MyCustomException">
  <part  xmlns:partns="java:WS.exception"
    type="partns:MyCustomException"
    name="MyCustomException">
  </part>
 </message>

<portType name="ESPort">
  <operation name="echo">
   <input message="tns:echo">
   </input>
   <output message="tns:echoResponse">
   </output>
   <fault name="MyCustomException"
     message="tns:MyCustomException">
   </fault>
  </operation>
 </portType>

Client Implementation

Web Services can be invoked using two approaches:

  1. Using stubs
  2. Using Dynamic Invocation Interface (DII)

Following is the client implementation using DII:

import java.util.*;
import javax.xml.namespace.QName;
import javax.xml.rpc.*;
import javax.xml.rpc.soap.SOAPFaultException;

public class ExceptionService{
   public static void main(String[] args) throws Exception{
     System.setProperty(
         ServiceFactory.SERVICEFACTORY_PROPERTY,
         "weblogic.webservice.core.rpc.ServiceFactoryImpl");

      ServiceFactory factory = ServiceFactory.newInstance();
      Service service = factory.createService(new QName(""));
      Call call = service.createCall();

      call.addParameter("intVal",
                        new QName("http://www.w3.org/2001/XMLSchema",
                        "int"), ParameterMode.IN);
      call.addParameter("string",
         new QName("http://www.w3.org/2001/XMLSchema", "string"),
         ParameterMode.IN);
      call.setOperationName(new QName("http://tempuri.org", "echo"));
      call.setReturnType(
         new QName("http://www.w3.org/2001/XMLSchema", "string"));
      call.setTargetEndpointAddress("http://localhost/exception/ES");
      Object[] argArray = {new Integer(args[0]), "how are you"};
      Object resultObj = null;
      try{
         resultObj = call.invoke(argArray);
      }catch(SOAPFaultException fault){
         fault.printStackTrace();
         resultObj = "ERROR Caught";

         System.out.println("Fault Detail : "+ fault.getDetail());
         System.out.println("Fault Actor : "+ fault.getFaultActor());
         System.out.println("Fault String: "+ fault.getFaultString());
      }

      System.out.println("Result: "+resultObj);
   }
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date