【问题标题】:Spring WS: How to get and save XSD validation errorsSpring WS:如何获取和保存 XSD 验证错误
【发布时间】:2015-06-05 12:22:24
【问题描述】:

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

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

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

Spring WS How to get all error messages when validation fails

但它并没有按我的意愿工作。

【问题讨论】:

    标签: java spring spring-ws


    【解决方案1】:

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

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

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

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

    【讨论】:

    • tnx 但你能用代码扩展你的答案并结合我在链接中描述的答案吗?这听起来更像是一个理论,而不是一个可行的解决方案
    • @Spring:添加了一个示例类
    • 我试过了,但从未调用过 handleRequestValidationErrors
    • 我做了一个测试用例并调用了我的拦截器,我猜你正在使用问题中发布的conf,其中 你必须设置它是真的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多