简体   繁体   English

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

[英]Spring WS: How to get and save XSD validation errors

I use SpringWS for my soap service and validate it like this; 我使用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)
{ ...}

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

But it does not work as I want. 但它不能按我的意愿工作。

you can extend PayloadValidationInterceptor and redefine the method 您可以扩展PayloadValidationInterceptor并重新定义该方法

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. 您也可以转储传入的消息,因为您可以访问messageContext及其getRequest()方法。 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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM