Validation 骆驼验证错误消息

Validation 骆驼验证错误消息,validation,exception,apache-camel,Validation,Exception,Apache Camel,我有下面的骆驼路线 <route> <from uri="target/in"/> <doTry> <to uri="validator:schema.xsd"/> <to uri="file:target/messages/validation/valid?fileName=a.xml"/> <doCatch> <except

我有下面的骆驼路线

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>             
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
我想在XML文件未通过本文中所述的验证时获得错误消息


但是,在Spring DSL中,我该如何做到这一点呢?

就像克劳斯在引用的线程中所说的那样:

异常原因=exchange.getProperty(exchange.Exception\u被捕获, 例外情况(类别)

因此,此路由应保存您的异常:

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform 
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${property.CamelExceptionCaught}

与引用线程中的克劳斯状态类似:

异常原因=exchange.getProperty(exchange.Exception\u被捕获, 例外情况(类别)

因此,此路由应保存您的异常:

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform 
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${property.CamelExceptionCaught}

如果希望将异常信息存储在文件中,如示例代码中所示,则需要将其转换为字符串。否则,您将得到另一个关于无法存储文件的异常,因为Camel无法将架构验证异常转换为java.io.inputStream

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform>
            <transform>
               <simple>${bodyAs(String)}</simple>
            </transform>
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${property.CamelExceptionCaught}
${bodyAs(字符串)}

如果要将异常信息存储在示例代码中的文件中,则需要将其转换为字符串。否则,您将得到另一个关于无法存储文件的异常,因为Camel无法将架构验证异常转换为java.io.inputStream

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform>
            <transform>
               <simple>${bodyAs(String)}</simple>
            </transform>
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${property.CamelExceptionCaught}
${bodyAs(字符串)}

您可以在一个文件中发送异常,在另一个文件中发送错误的xml

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
            <setBody>
                <simple>Got "${exception.message}" with this stack\n${exception.stacktrace}\n${body}</simple>
            </setBody>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml.error</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
        </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${file:onlyname.noext}.${date:now:yyyyymmdd_hhmmssss}.xml
已使用此堆栈获取“${exception.message}”\n${exception.stacktrace}\n${body}
${file:onlyname.noext}.${date:now:yyyyymmdd_hhmmssss}.xml.error

您可以在一个文件中发送异常,在另一个文件中发送错误的xml

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
            <setBody>
                <simple>Got "${exception.message}" with this stack\n${exception.stacktrace}\n${body}</simple>
            </setBody>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml.error</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
        </doCatch>
    </doTry> 
</route>

org.apache.camel.ValidationException
${file:onlyname.noext}.${date:now:yyyyymmdd_hhmmssss}.xml
已使用此堆栈获取“${exception.message}”\n${exception.stacktrace}\n${body}
${file:onlyname.noext}.${date:now:yyyyymmdd_hhmmssss}.xml.error

谢谢,这很有效。它会发回一条消息,但这不是我所期望的消息。我正在获取soap:服务器无法存储文件:target\messages\validation\invalid\a.xml引发异常后,将xml文件放在另一个文件夹中是否有问题?谢谢,可以。它会发回一条消息,但这不是我所期望的消息。我正在获取soap:服务器无法存储文件:target\messages\validation\invalid\a.xml引发异常后,将xml文件放在另一个文件夹中是否有问题?