Spring WS:如何获取和保存 XSD 验证错误

Spring WS: How to get and save XSD validation errors

我将 SpringWS 用于我的 soap 服务并像这样验证它;

 <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)
{ ...}

这很好用,但是当出现错误时,returns spring 在它到达端点之前生成错误响应,所以我永远没有机会处理它们。但我希望能够 记录完整的错误消息并将其保存到数据库 。我发现的一种方法是在我的另一个问题中做这样的事情;

Spring WS How to get all error messages when validation fails

但它并不如我所愿。

您可以扩展 PayloadValidationInterceptor 并重新定义方法

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

如果您查看标准实现(可用 here),您可以看到它如何转储所有解析错误;您还可以转储传入消息,因为您可以访问 messageContext 及其 getRequest() 方法。你的 class x 应该是

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);

}