Apache camel 骆驼验证程序返回整个消息
我有一条路线Apache camel 骆驼验证程序返回整个消息,apache-camel,Apache Camel,我有一条路线 <from uri="a"> <to uri="validator:schema.xsd"> <to uri="b"> 假设正在验证的XML文件缺少两个元素,那么一旦验证器找到第一个缺少的元素并返回一条消息说它缺少了,它就会停止 是否可以继续验证XML文件以查找任何其他缺少的元素,并在错误消息中返回这些元素,这样发送方就不必继续发送以找出哪些元素缺少或无效?如果验证失败,则抛出。此异常包含返回列表的方法getError()。 您可以使用它将
<from uri="a">
<to uri="validator:schema.xsd">
<to uri="b">
假设正在验证的XML文件缺少两个元素,那么一旦验证器找到第一个缺少的元素并返回一条消息说它缺少了,它就会停止
是否可以继续验证XML文件以查找任何其他缺少的元素,并在错误消息中返回这些元素,这样发送方就不必继续发送以找出哪些元素缺少或无效?如果验证失败,则抛出。此异常包含返回列表的方法getError()
。
您可以使用它将消息转换为OneException块中的列表
下面的代码捕获SchemaValidationException,将正文转换为SchemaValidationException.getErrors()
,并将异常标记为continued
,以继续路由并在输出路由上返回此列表
from("timer:simple?period=1000")
.setBody(constant(XML_INVALID))
.to("direct:a");
from("direct:a")
.onException(SchemaValidationException.class)
.to("direct:validationErrors")
.continued(true)
.end()
.to("validator:test.xsd")
.to("log:result");
from("direct:validationErrors")
.setBody(simple("${property.CamelExceptionCaught.errors}"))
.end();
注意:此示例使用以下资源进行了测试 XML_无效
<?xml version="1.0" encoding="utf-8"?>
<shiporder orderid="str1234">
<orderperson>str1234</orderperson>
<shipto>
<name>str1234</name>
<address>str1234</address>
</shipto>
<item>
<quantity>745</quantity>
<price>123.45</price>
</item>
</shiporder>
str1234
str1234
我向谷歌建议javax.xml.validation.Validator,看看它有什么属性和选项来打开|关闭这些功能。还可以在org.apache.camel.processor.validation上以调试级别运行,就像对验证器报告的每个警告/错误/致命错误进行camel调试一样。尽管抛出的异常被构造为返回错误/警告列表。那么您确定只看到1个错误而不是所有错误吗?
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>