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 路由时,只有属性制造商和 licensePlate 的 @NotNull 约束才会被验证(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>