12.6. 示例
假设我们有一个具有以下注解的 java bean
car.java
public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 5, max = 14, groups = OptionalChecks.class) private String licensePlate; // getter and setter }
以及自定义验证组的接口定义
OptionalChecks.java
public interface OptionalChecks { }
使用以下 Camel 路由,只有 @NotNull 对属性 manufacturer 和 licensePlate 约束将被验证(Camel 使用默认组 javax.validation.groups.Default
)。
from("direct:start") .to("bean-validator://x") .to("mock:end")
如果要从组 OptionalChecks
检查限制,您必须定义如下路由:
from("direct:start") .to("bean-validator://x?group=OptionalChecks") .to("mock:end")
如果要检查这两个组中的限制,您必须首先定义一个新接口
AllChecks.java
@GroupSequence({Default.class, OptionalChecks.class}) public interface AllChecks { }
然后您的路由定义应如下所示
from("direct:start") .to("bean-validator://x?group=AllChecks") .to("mock:end")
另外,如果您必须提供自己的消息交换器、遍历解析器和约束验证器,您必须编写类似如下的路由
<bean id="myMessageInterpolator" class="my.ConstraintValidatorFactory" /> <bean id="myTraversableResolver" class="my.TraversableResolver" /> <bean id="myConstraintValidatorFactory" class="my.ConstraintValidatorFactory" />
from("direct:start") .to("bean-validator://x?group=AllChecks&messageInterpolator=#myMessageInterpolator &traversableResolver=#myTraversableResolver&constraintValidatorFactory=#myConstraintValidatorFactory") .to("mock:end")
也可以将您的限制描述为 XML,而不是 Java 注解。在这种情况下,您必须提供 META-INF/validation.xml
文件,该文件可能如下所示:
validation.xml
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"> <default-provider>org.hibernate.validator.HibernateValidator</default-provider> <message-interpolator>org.hibernate.validator.engine.ResourceBundleMessageInterpolator</message-interpolator> <traversable-resolver>org.hibernate.validator.engine.resolver.DefaultTraversableResolver</traversable-resolver> <constraint-validator-factory>org.hibernate.validator.engine.ConstraintValidatorFactoryImpl</constraint-validator-factory> <constraint-mapping>/constraints-car.xml</constraint-mapping> </validation-config>
和 constraints-car.xml
文件
constraints-car.xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd" xmlns="http://jboss.org/xml/ns/javax/validation/mapping"> <default-package>org.apache.camel.component.bean.validator</default-package> <bean class="CarWithoutAnnotations" ignore-annotations="true"> <field name="manufacturer"> <constraint annotation="javax.validation.constraints.NotNull" /> </field> <field name="licensePlate"> <constraint annotation="javax.validation.constraints.NotNull" /> <constraint annotation="javax.validation.constraints.Size"> <groups> <value>org.apache.camel.component.bean.validator.OptionalChecks</value> </groups> <element name="min">5</element> <element name="max">14</element> </constraint> </field> </bean> </constraint-mappings>
以下是 OrderedChecks 的示例路由定义的 XML 语法。
请注意,正文应包含要验证的类实例。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <to uri="bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks"/> </route> </camelContext> </beans>