这是用户在 2024-3-28 22:15 为 https://camel.apache.org/manual/validator.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Validator 验证器

Validator performs declarative validation of the message according to the declared Input Type and/or Output Type on a route definition which declares the expected message type.
验证器根据路由定义中声明的输入类型和/或输出类型对消息进行声明性验证,该路由定义了预期的消息类型。

Data type format 数据类型格式

scheme:name

where scheme is the type of data model like java, xml or json, and name is the individual data type name.
其中 scheme 是数据模型的类型,如 javaxmljson ,而 name 是个体数据类型的名称。

Supported Validators 支持的验证器

Validator 验证器 Description 描述

Predicate Validator 谓词验证器

Validate with using Expression or Predicate
使用表达式或谓词进行验证

Endpoint Validator 终端验证器

Validate by forwarding to the Endpoint to be used with validation component such as Validation Component or Bean Validation Component.
通过转发到要与验证组件(如验证组件或 Bean 验证组件)一起使用的终端进行验证。

Custom Validator 自定义验证器

Validate with using custom validator class. Validator must be a subclass of org.apache.camel.spi.Validator
使用自定义验证器类进行验证。验证器必须是 org.apache.camel.spi.Validator 的子类。

Common Options 常见选项

All validators have following common options to specify which data type is supported by the validator. type must be specified.
所有验证器都有以下共同选项,用于指定验证器支持的数据类型。必须指定 type

Name 姓名 Description 描述

type 类型

Data type to validate 数据类型验证

Predicate Validator Options
谓词验证器选项

Name 姓名 Description 描述

expression 表达

Expression or Predicate to be used for validation
用于验证的表达式或谓词

Here is an example to specify a validation predicate:
这里有一个指定验证谓词的示例:

Java DSL: Java DSL:

validator()
    .type("csv:CSVOrder")
    .withExpression(bodyAs(String.class).contains("{name:XOrder}"));

XML DSL: XML 领域特定语言

<predicateValidator Type="csv:CSVOrder">
    <simple>${body} contains '{name:XOrder}'</simple>
</predicateValidator>

Endpoint Validator Options
终端验证器选项

Name 姓名 Description 描述

ref

Reference to the Endpoint ID
参考终端标识符

uri

Endpoint URI 终端 URI

Here is an example to specify endpoint URI in Java DSL:
这是一个在 Java DSL 中指定端点 URI 的示例

validator()
    .type("xml")
    .withUri("validator:xsd/schema.xsd");

And here is an example to specify endpoint ref in XML DSL:
这里是一个在 XML DSL 中指定端点引用的示例

<endpointValidator uri="validator:xsd/schema.xsd" type="xml"/>

Note that the Endpoint Validator just forwards the message to the specified endpoint. In above example, camel forwards the message to the validator: endpoint, which actually is a Validation Component. You can also use any other validation component like Bean Validation Component.
请注意,终端验证器只是将消息转发到指定的终端。在上面的示例中,Camel 将消息转发到 validator: 终端,实际上它是一个验证组件。您还可以使用其他任何验证组件,如 Bean 验证组件。

Custom Validator Options
自定义验证器选项

The validator must be an implementation of org.apache.camel.spi.Validator
验证器必须是 org.apache.camel.spi.Validator 的实现

Name 姓名 Description 描述

ref

Reference to the custom Validator bean ID
参考自定义验证器 bean 的 ID

className 类名

Fully qualified class name of the custom Validator class
自定义验证器类的完全限定类名

Here is an example to specify custom Validator class:
这里是一个指定自定义验证器类的示例:

Java DSL: Java DSL:

validator()
    .type("json")
    .withJava(com.example.MyCustomValidator.class);

XML DSL: XML 领域特定语言

<customTransformer className="com.example.MyCustomValidator" type="json"/>

Examples 例子

For example to declare the Endpoint Validator which uses validator component to validate xml:ABCOrder, we can do as follows:
例如,声明使用验证器组件来验证 xml:ABCOrder 的端点验证器,我们可以按照以下方式进行:

Java DSL: Java DSL:

validator()
    .type("xml:ABCOrder")
    .withUri("validator:xsd/schema.xsd");

And in XML DSL:
在 XML DSL 中:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <validators>
        <endpointValidator uri="validator:xsd/schema.xsd" type="xml:ABCOrder"/>
    </validators>
</camelContext>

If you have following route definition, above validator will be applied when direct:abc endpoint receives the message. Note that inputTypeWithValidate is used instead of inputType in Java DSL, and the validate attribute on the inputType declaration is set to true in XML DSL:
如果您有以下路由定义,则当 direct:abc 端点接收到消息时,上述验证器将被应用。请注意,在 Java DSL 中使用 inputTypeWithValidate 而不是 inputType ,并且在 XML DSL 中,inputType 声明中的 validate 属性设置为 true

Java DSL: Java DSL:

from("direct:abc")
    .inputTypeWithValidate("xml:ABCOrder")
    .log("${body}");

XML DSL: XML 领域特定语言

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:abc"/>
        <inputType urn="xml:ABCOrder" validate="true"/>
        <log message="${body}"/>
    </route>
</camelContext>

See Also 请参见

The Transformer is a related functionality.
变压器是一个相关的功能。