Announcement Announcement Module
Collapse
No announcement yet.
How to extract the fault detail content from the customized SoapFault? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to extract the fault detail content from the customized SoapFault?

    I have a custom exception resolver in which I add detail tag to the SoapFault using SoapFaultDetail.

    But while extracting the detail from SoapFault element, I couldn't find proper API to extract detail content.

    For example:
    My SoapFault looks like-
    <soapenv:Fault>
    <faultcode>soapenv:Server</faultcode>
    <faultstring xml:lang="locale">string</faultstring>
    <detail>
    <EntityDetail>detail</EntityDetail>
    </detail>
    </soapenv:Fault>

    How can I get the content from <EntityDetail> ?

  • #2
    You catch SoapFaultClientException on client side, and extract SoapFaultDetail instance via ex.getSoapFault().getFaultDetail();

    After that fetch detail Source via :
    SoapFaultDetailElement detailElementChild = (SoapFaultDetailElement) soapFaultDetail.getDetailEntries().next();
    Source detailSource = detailElementChild.getSource();

    Here's how I do it when using marshallers. My custom SoapFaultWithDetailException containing unmarshalled detail object will get thrown in case when there is detail contained in SOAP fault response.

    Code:
        private <R> R executeMarshalWebServiceRequest(Object request, Class<R> requiredType) {
            if (request == null) {
                throw new IllegalArgumentException("request is null");
            }
            if (requiredType == null) {
                throw new IllegalArgumentException("requiredType is null");
            }
    
            try {
                // send request ...
                Object response = webServiceTemplate.marshalSendAndReceive(request);
                return requiredType.cast(response);
    
            } catch (SoapFaultClientException e) {
                SoapFaultDetail soapFaultDetail = e.getSoapFault().getFaultDetail();
    
                // if there is no fault detail ...
                if (soapFaultDetail == null) {
                    throw e;
                }
                // cannot simply use SoapFaultDetail.getSource() since it returns <detail> as root XML element
                SoapFaultDetailElement detailElementChild = (SoapFaultDetailElement) soapFaultDetail.getDetailEntries().next();
                Source detailSource = detailElementChild.getSource();
    
                try {
                    Object detail = webServiceTemplate.getUnmarshaller().unmarshal(detailSource);
                    throw new SoapFaultWithDetailException(detail);
    
                } catch (IOException e1) {
                    throw new IllegalArgumentException("cannot unmarshal SOAP fault detail object: " + soapFaultDetail.getSource());
                }
            }
        }

    Comment

    Working...
    X