Spring WS:如何获取和保存XSD验证错误 [英] Spring WS: How to get and save XSD validation errors

查看:104
本文介绍了Spring WS:如何获取和保存XSD验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SpringWS作为我的肥皂服务并像这样验证它;

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如何在验证失败时获取所有错误消息

但它不能正常工作。

推荐答案

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

you can extend PayloadValidationInterceptor and redefine the method

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

如果查看标准实现(可用这里)你可以看到它如何转储所有解析错误;您也可以转储传入的消息,因为您可以访问messageContext及其getRequest()方法。您的类可能类似于

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 WS:如何获取和保存XSD验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆