title: How to transform exceptions into return codes using Spring-WS?
body: <p>I'm currently facing a problem with error codes and messages using Spring WS.</p>
  
  <p>We are using Spring WS 2.0 with a JAXB2 binding and the @Endpoint and @PayloadRoot annotations for convenience.</p>
  
  <p>Our endpoint looks like this:</p>
  
  <pre><code>@Endpoint
  public class MyEndpoint() {
      private static final String MY_NAMESPACE=...;
  
      @PayloadRoot(namespace=MY_NAMESPACE, localPart="myPart")
      public MyPartResponse handleMyPart(MyPart myPart) {
          ....
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Each response inherits from</p>
  
  <pre><code>&lt;xs:complexType name="ResultBase"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="errorCode" type="tns:ErrorCode" /&gt;
          &lt;xs:element name="errorMessage" type="xs:string" /&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>and adds some specifics in case of success, like this:</p>
  
  <pre><code>&lt;xs:element name="MySpecificResponse"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="tns:ResultBase"&gt;
                  &lt;xs:sequence&gt;
                      &lt;xs:element name="mySpecificElement" type="tns:MySpecificType" /&gt;
                  &lt;/xs:sequence&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p>All the exceptions being thrown inside the <em>handleMyPart</em> method can be cleanly mapped.</p>
  
  <p>However two types of errors remain uncaught and generate faults instead of a clear error message:</p>
  
  <ul>
  <li>XSD Validations Errors</li>
  <li>Malformed XML Errors</li>
  </ul>
  
  <p>At the end of the day, <strong>these are issues that are relevant to every POX webservice using Spring WS</strong>. How should one intercept these exceptions and map then onto a response object?</p>
  
  <p>Remember though: <strong>all response objects are slightly different</strong> as they all inherit from the common one, but add some unique optional content to it.</p>