How to convert exceptions to return codes using Spring -WS?

I am currently encountering a problem with error codes and messages using Spring WS.

We use Spring WS 2.0 with JAXB2 binding and @Endpoint and @PayloadRoot annotations for convenience.

Our endpoint is as follows:

@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 wrap around the POX message defined by XSD. It also means that instead of errors, we use return codes and messages.

Each answer 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 features if successful, for example:

 <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> </xs:complexType> </xs:element> 

All exceptions that are thrown inside the handleMyPart method can be clearly displayed.

However, two types of errors remain invisible and generate errors instead of an explicit error message:

  • XSD Validation Errors
  • Invalid XML Errors

At the end of the day, these are problems that apply to every POX web service using Spring WS . How should these exceptions be caught and then the response object displayed?

Remember that: all response objects are slightly different , since they all inherit from the general, but add some unique additional content to it.

+7
java soap spring-ws web-services jaxb2
source share
1 answer

One approach that 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 a validatingInterceptor bean in the Spring context.

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

I wrote this with ad nauseum details on my blog:

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

-Larry

+4
source share

All Articles