-->

Custom SOAP Faults in spring integration Fault cod

2019-07-18 21:18发布

问题:

I am trying to implement custom soap faults like below:

@SoapFault(faultCode = FaultCode.CUSTOM, customFaultCode="{namespace}Server Error", faultStringOrReason="Error encountered when processing request message.") 
public class SystemFault extends BusinessException{ }. 

The soap fault thrown is of the below format:

<.SOAP-ENV:Fault>
     <.faultcode xmlns:ns0="namespace">ns0:star:Server Error<./faultcode>
     <.faultstring xml:lang="en">Error Encountered when processing the request.<./faultstring>
 <./SOAP-ENV:Fault>

As you can see, fault code tag is appearing with a namespace declaration. Please let me know if there is anyway to avoid that. The format of soap fault client is expecting is:

<.soapenv:Fault xmlns:star="http://www.starstandard.org/STAR/5">
     <.faultcode>star:Custom Fault Code<./faultcode>
     <.faultstring>Custom Fault message<./faultstring>
<./soapenv:Fault>

I Checked AbstractSoapFaultDefinitionExceptionResolver.resolveExceptionInternal() method and it is expecting QName instance for fault code and not a string. Please let me know how to solve this.

回答1:

Looks like you almost there with your customFaultCode syntax!

Let's revise that JavaDoc one more time:

/**
 * The custom fault code, to be used if {@link #faultCode()} is set to {@link FaultCode#CUSTOM}.
 *
 * <p>The format used is that of {@link QName#toString()}, i.e. "{" + Namespace URI + "}" + local part, where the
 * namespace is optional.
 *
 * <p>Note that custom Fault Codes are only supported on SOAP 1.1.
 */
String customFaultCode() default "";

So, I've just decided to see what is that {@link QName#toString()}:

public String toString() {
    if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {
        return localPart;
    } else {
        return "{" + namespaceURI + "}" + localPart;
    }
}

Where that XMLConstants.NULL_NS_URI is like:

public static final String NULL_NS_URI = "";

From here it looks like it would be enough for you to declare that like plain string:

customFaultCode="Server Error"

Although if you want to move the xmlns declaration to the top-level you should consider to use more low-level API in your custom AbstractSoapFaultDefinitionExceptionResolver extension:

((SoapMessage) messageContext.getResponse()).getEnvelope().addNamespaceDeclaration("star", "http://www.starstandard.org/STAR/5");

And right, use your customFaultCode with raw prefix:

customFaultCode="star:Server Error"


回答2:

This seems to be an old post but just wanna try to help out. I think you should not worry about fault code and faultdescription. There is actually a third field in the soapFault element whitch is named detail. This element has two subelements named code and description. Use this to carry the fault information.

Check this post: http://memorynotfound.com/spring-ws-add-detail-soapfault-exception-handling/

Hope this can help. Thanks