Wednesday 21 October 2015

Implementing SOAP Fault in Top Down Web Service


Step-1

SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication 

resulting from incorrect message format, header-processing problems, or incompatibility 

between applications.


























Define the detail message in XSD

<complexType name="ConsultantFaultMessage">
<sequence>
<element  name="name" type="string" minOccurs="1" maxOccurs="1">
</element>
<element  name="email" type="string" maxOccurs="1" minOccurs="1">
<annotation>
<documentation>email id of the consultants</documentation>
</annotation>
</element>

<element  name="mobile" type="string" maxOccurs="1" minOccurs="1">
<annotation>
<documentation>mobile id of the consultants</documentation>
</annotation>
</element>

<element  name="description" type="string" maxOccurs="1" minOccurs="1">
<annotation>
<documentation>description of error message</documentation>
</annotation>
</element>
</sequence>

</complexType>

Step-2
Define the element of above complex type inside XSD
    

<element name="ConsultantFaultMessage" type="tns:ConsultantFaultMessage"/>

Step-3
Associate this element with WSDL message

   <wsdl:message name="ConsultantFaultMessageException">
<wsdl:part name="tempFault" element="tns:ConsultantFaultMessage" />

  </wsdl:message>


Step-4
attached above message as soap fault element in PortType with operation
<wsdl:portType name="ConsultantWebService">
<wsdl:documentation>Creating contract for the soap web service
</wsdl:documentation>
      <wsdl:operation name="uploadConsultant">
      <wsdl:input message="tns:wconsultantRequest" />
     <wsdl:output message="tns:wconsultantResponse" />
      <wsdl:fault name="tConsultantFaultMessageException" message="tns:ConsultantFaultMessageException"/>
</wsdl:operation>

<wsdl:operation name="findAllConsultants">
<wsdl:input message="tns:wvoidRequest" />
<wsdl:output message="tns:wallConsultantsResponse" />
</wsdl:operation>

</wsdl:portType>

Step-5
Define above fault finally in binding tag
<wsdl:binding name="ConsultantWebServiceBinding" type="tns:ConsultantWebService">
<wsdl:documentation>
http://schemas.xmlsoap.org/soap/http- this fix for http protocol
</wsdl:documentation>
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="uploadConsultant">
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
<wsdl:fault name="tConsultantFaultMessageException">
<soap:fault name="tConsultantFaultMessageException"/>
</wsdl:fault>

</wsdl:operation>
</wsdl:binding>

Generate the SEI from above WSDL

 /**
     * 
     * @param pconsultant
     * @return
     *     returns java.lang.String
     * @throws ConsultantFaultMessageException
     */
    @WebMethod
    @WebResult(name = "sresponse", targetNamespace = "http://www.consultants.com/it", partName = "presponse")
    public String uploadConsultant(
        @WebParam(name = "consultantRequest", targetNamespace = "http://www.consultants.com/it", partName = "pconsultant")
        Consultant pconsultant)
        throws ConsultantFaultMessageException ;


@WebService(name = "ConsultantWebService", targetNamespace = "http://www.consultants.com/it",
endpointInterface = "com.consultants.it.ConsultantWebService", portName = "ConsultantWebServicePort", 
serviceName = "ConsultantWebServiceService")
@XmlSeeAlso({ ObjectFactory.class })
public class ConsultantWebServiceProvider implements ConsultantWebService {

public String uploadConsultant(Consultant pconsultant) throws ConsultantFaultMessageException {
if(pconsultant.getEid().equals("400")) {
ConsultantFaultMessage faultInfo=new ConsultantFaultMessage();
faultInfo.setDescription("This id is not acceptable at all.....");
faultInfo.setEmail(pconsultant.getEmail().get(0));
faultInfo.setMobile(pconsultant.getMobile().getVendor());
faultInfo.setName(pconsultant.getName());
ConsultantFaultMessageException consultantFaultMessageException=new ConsultantFaultMessageException("Id cannot be 400", faultInfo);
throw consultantFaultMessageException;
}
return "uploaded";

}


Soap Fault with response :-

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>Id cannot be 400</faultstring>
         <detail>
            <ConsultantFaultMessage:ConsultantFaultMessage xmlns:ConsultantFaultMessage="http://www.consultants.com/it" xmlns="http://www.consultants.com/it">
               <name>Nagendra</name>
               <email>nagendra@gmail.com</email>
               <mobile>Nokia</mobile>
               <description>This id is not acceptable at all.....</description>
            </ConsultantFaultMessage:ConsultantFaultMessage>
         </detail>
      </S:Fault>
   </S:Body>

</S:Envelope>


No comments:

Post a Comment