Category Archives: Spring

Handling POX Errors in Spring-WS Part 2

In part one of this post, I showed you how to communicate XSD validation errors back to a Spring-WS POX client. In this part, I demonstrate how to tell the client they’ve submitted corrupt XML.

Let’s post some corrupt XML by leaving the closing Contacts XML element out of a ContactStore call.

Corrupt XML

<?xml version="1.0" encoding="UTF-8"?>
  <Contacts xmlns="">

If you’re using Tomcat for a servlet container, you get a big nasty error like this:

Tomcat's Bad XML Error

The HTTP response says that the problem is “XML document structures must start and end within the same entity.”   OK, that tells us what the client did wrong, but it’s not in a form that our ContactStore service can handle.   We need some way to catch the error and reformat it as an XML that fits the ContactStore XSD.

So what next?   Close examination of the stack trace gives us an idea where we can catch that error:

The Spring-WS MessageDispatcherServlet is the main servlet for handling all Spring-WS requests, and the exception passes through its doService method before it bubbles up to the servlet container. So we can subclass MessageDispatcherServlet and override doService to provide custom exception handling.


import ...

 * Custom MessageDispatcherServlet that can handle DomPoxMessageExceptions
 * @author lreeder
public class PoxMessageDispatcherServlet extends MessageDispatcherServlet
   protected void doService(HttpServletRequest request,
                         HttpServletResponse response)
                  throws Exception
         super.doService(request, response);
      catch (DomPoxMessageException domMessageException)
          final String NAMESPACE_URI = "";
          final String ENCODING = "UTF-8";
          String error = domMessageException.getMessage();
          //Create an error message for DomPoxMessageExceptions that
          // validates against the XSD.
          //TODO - escape XML characters in the error
          String errorXml =  "<!--?xml version=\"1.0\" encoding=\"" + ENCODING + "\"?-->\n"  +
                  "\n" +
                  error +

And now we just need to update web.xml to use our new dispatcher servlet:

<!-- PoxMessageDispatcherServlet wraps the default MessageDispatcherServlet
 and provides customer exception handling -->

What happens when we  post our invalid XML to the updated MessageDispatcherServlet?

<?xml version="1.0" encoding="UTF-8"?>
  <error xmlns="">
Could not parse request message; nested exception is org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.

Now we’ve got an error message that an XML client can handle.

Well, that’s it for this post.   I hope it’s given you some idea about how you can implement more flexible error handling with a POX service built with Spring-WS.