开发者

How to transform exceptions into return codes using Spring-WS?

开发者 https://www.devze.com 2023-01-25 08:19 出处:网络
I\'m currently facing a problem with error codes and messages using Spring WS. We are using Spring WS 2.0 with a JAXB2 binding and the @Endpoint and @PayloadRoot annotations for convenience.

I'm currently facing a problem with error codes and messages using Spring WS.

We are using Spring WS 2.0 with a JAXB2 binding and the @Endpoint and @PayloadRoot annotations for convenience.

Our endpoint looks like this:

@Endpoint
public class MyEndpoint() {
    private static final String MY_NAMESPACE=...;

    @PayloadRoot(namespace=MY_NAMESPACE, localPart="myPart")
    public MyPartResponse handleMyPart(MyPart myPart) {
        ....
    }
}

We use soap only as a thin wrapper around a POX message, defined by the XSD. This also means we use return codes and messages instead of faults.

Each response inherits from

<xs:complexType name="ResultBase">
    <xs:sequence>
        <xs:element name="errorCode" type="tns:ErrorCode" />
        <xs:element name="errorMessage" type="xs:string" />
    </xs:sequence>
</xs:complexType>

and adds some specifics in case of success, like this:

<xs:element name="MySpecificResponse">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="tns:ResultBase">
                <xs:sequence>
                    <xs:element name="mySpecificElement" type="tns:MySpecificType" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent&g开发者_运维百科t;
    </xs:complexType>
</xs:element>

All the exceptions being thrown inside the handleMyPart method can be cleanly mapped.

However two types of errors remain uncaught and generate faults instead of a clear error message:

  • XSD Validations Errors
  • Malformed XML Errors

At the end of the day, these are issues that are relevant to every POX webservice using Spring WS. How should one intercept these exceptions and map then onto a response object?

Remember though: all response objects are slightly different as they all inherit from the common one, but add some unique optional content to it.


One approach that's worked well for me is this:

For XSD validation errors, extend the AbstractValidatingInterceptor to provide custom handling of XSD validation errors, and set it as the validatingInterceptor bean in the Spring context.

For malformed XML, extend MessageDispatcherServlet. Override doService to catch DomPoxMessageException, and add your own custom handling when you catch that exception. Set your customized MessageDispatcherServlet as the spring-ws servlet in the web.xml.

I've written this up with details ad nauseum in my blog post here:

http://www.dev-garden.org/2011/09/03/handling-pox-errors-in-spring-ws-part-1/

-Larry

0

精彩评论

暂无评论...
验证码 换一张
取 消