11

I use SpringWS for my soap service and validate it like this;

 <sws:interceptors>
    <bean id="payloadValidatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
        <property name="schema" value="/schemas/my.xsd"/>
        <property name="validateRequest" value="false"/>
        <property name="validateResponse" value="true"/>
    </bean>

@PayloadRoot(namespace = NAMESPACE,  localPart = "ServiceProvider")
@ResponsePayload
public ServiceProviderTxn getAccountDetails(@RequestPayload ServiceProviderrequest)
{ ...}

This works fine but when there is an error it returns a spring generated error response before it reaches to the endpoint, so I never have a chance to process them. But I want to be able to log and save the full error message to database. One way I found out is to do something like this in my other question;

Spring WS How to get all error messages when validation fails

But it does not work as I want.

Community
  • 1
  • 1
Spring
  • 11,333
  • 29
  • 116
  • 185

1 Answers1

8

you can extend PayloadValidationInterceptor and redefine the method

protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors)

If you look at the standard implementation (available here) you can see how it dumps all the parsing errors; you can also dump the incoming message since you have access to messageContext and its getRequest() method. Your class xould be something like

public class PayloadValidationgInterceptorCustom extends
PayloadValidatingInterceptor {

@Override
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors)
        throws TransformerException {
    messageContext.getRequest().writeTo(/*place your Outputstream here something like a ByteArrayOutputStream*/); //use this if you want to dump the message
    for (SAXParseException error : errors) {
        //dump the each error on the db o collect the stack traces in a single string and dump only one or to the database
       /*you can use something like this
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
         error.printStackTrace(pw);
         sw.toString();
         to get the stack trace
        */

    }
    return super.handleRequestValidationErrors(messageContext,errors);

}
Spring
  • 11,333
  • 29
  • 116
  • 185
Giovanni
  • 3,951
  • 2
  • 24
  • 30
  • 1
    tnx but could you extend your answer with code and combine with the answer that I described on my link? It more sounds like a theory rather than a working solution – Spring Jun 12 '15 at 07:49
  • 1
    @Spring: added a sample class – Giovanni Jun 12 '15 at 08:06
  • I tried but handleRequestValidationErrors is never called – Spring Jun 12 '15 at 09:05
  • I did a test case and my interceptor is called, my guess is that you are using the conf posted in the question where you must set it to true – Giovanni Jun 12 '15 at 09:44
  • tnx could you also look at http://stackoverflow.com/questions/30850182/spring-response-time – Spring Jun 15 '15 at 16:24