Bean Validator
Since Camel 2.3
Only producer is supported
The Validator component performs bean validation of the message body using the Java Bean Validation API (JSR 303). Camel uses the reference implementation, which is Hibernate Validator.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean-validator</artifactId>
<version>x.y.z</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
bean-validator:label[?options]
Where label is an arbitrary text value describing the endpoint. You can append query options to the URI in the following format: ?option=value&option=value&…
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Bean Validator component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether to ignore the META-INF/validation.xml file when the ValidatorFactory is built, so the provider, MessageInterpolator, TraversableResolver, ConstraintValidatorFactory, constraint mapping files and properties it configures are not used. Not used when a validatorFactory is set. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The ConstraintValidatorFactory that creates the ConstraintValidator instances of the constraints, for example to inject dependencies into custom validators. When not set, the one of META-INF/validation.xml or the default of Hibernate Validator is used, which calls the no-argument constructor. Not used when a validatorFactory is set. | ConstraintValidatorFactory | ||
The MessageInterpolator that turns the message templates of the violated constraints, such as \{jakarta.validation.constraints.NotNull.message}, into the messages of the BeanValidationException. When not set, the one of META-INF/validation.xml or the ResourceBundleMessageInterpolator of Hibernate Validator is used. Not used when a validatorFactory is set. | MessageInterpolator | ||
The TraversableResolver that decides whether the validation may reach a property, and whether it may cascade into it. When not set, the one of META-INF/validation.xml or the default of Hibernate Validator is used: it reaches every property, but skips the properties that Jakarta Persistence has not loaded yet when Jakarta Persistence is on the classpath. Not used when a validatorFactory is set. | TraversableResolver | ||
The ValidationProviderResolver that returns the Jakarta Bean Validation providers. The first provider (or the default-provider of META-INF/validation.xml) builds the ValidatorFactory. When not set, the single ValidationProviderResolverFactory in the registry creates it, if there is one; otherwise the default resolver finds the providers with the Java ServiceLoader. Not used when a validatorFactory is set. | ValidationProviderResolver | ||
Autowired The ValidatorFactory that gives the Validator of all endpoints; a single ValidatorFactory in the registry is used automatically. When set, the ignoreXmlConfiguration, validationProviderResolver, messageInterpolator, traversableResolver and constraintValidatorFactory options of the component and of its endpoints are not used. When not set, each endpoint builds its own ValidatorFactory from these options. | ValidatorFactory |
Endpoint Options
The Bean Validator endpoint is configured using URI syntax:
bean-validator:label
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The fully qualified class name of the validation group to validate, which can also be a group sequence. Only the constraints of this group (or of the groups in the sequence) are checked. When not set, the Default group (jakarta.validation.groups.Default) is validated. | jakarta.validation.groups.Default | String | |
Whether to ignore the META-INF/validation.xml file when the ValidatorFactory is built, so the provider, MessageInterpolator, TraversableResolver, ConstraintValidatorFactory, constraint mapping files and properties it configures are not used. Not used when a validatorFactory is set. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The ConstraintValidatorFactory that creates the ConstraintValidator instances of the constraints, for example to inject dependencies into custom validators. When not set, the one of META-INF/validation.xml or the default of Hibernate Validator is used, which calls the no-argument constructor. Not used when a validatorFactory is set. | ConstraintValidatorFactory | ||
The MessageInterpolator that turns the message templates of the violated constraints, such as \{jakarta.validation.constraints.NotNull.message}, into the messages of the BeanValidationException. When not set, the one of META-INF/validation.xml or the ResourceBundleMessageInterpolator of Hibernate Validator is used. Not used when a validatorFactory is set. | MessageInterpolator | ||
The TraversableResolver that decides whether the validation may reach a property, and whether it may cascade into it. When not set, the one of META-INF/validation.xml or the default of Hibernate Validator is used: it reaches every property, but skips the properties that Jakarta Persistence has not loaded yet when Jakarta Persistence is on the classpath. Not used when a validatorFactory is set. | TraversableResolver | ||
The ValidationProviderResolver that returns the Jakarta Bean Validation providers. The first provider (or the default-provider of META-INF/validation.xml) builds the ValidatorFactory. When not set, the single ValidationProviderResolverFactory in the registry creates it, if there is one; otherwise the default resolver finds the providers with the Java ServiceLoader. Not used when a validatorFactory is set. | ValidationProviderResolver | ||
The ValidatorFactory that gives the Validator that validates the message body. When set, ignoreXmlConfiguration, validationProviderResolver, messageInterpolator, traversableResolver and constraintValidatorFactory are not used. When not set, a new ValidatorFactory is built from these options when the producer is created. | ValidatorFactory |
Message Body
The message body at each side of the Bean Validator component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| A POJO with Jakarta Bean Validation constraints, or a List or another Iterable of them, each one validated. Text, a Map or an array has no constraints of its own and passes unchecked; no body fails. |
Producer output |
| Unchanged: a body that breaks a constraint fails with a BeanValidationException. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Using HibernateValidationProviderResolver
-
Java
-
XML
from("direct:test").
to("bean-validator://ValidationProviderResolverTest?validationProviderResolver=#myValidationProviderResolver"); <bean id="myValidationProviderResolver" class="org.apache.camel.component.bean.validator.HibernateValidationProviderResolver"/> Example
Assumed we have a java bean with the following annotations
Car.java
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 5, max = 14, groups = OptionalChecks.class)
private String licensePlate;
// getter and setter
} and an interface definition for our custom validation group
OptionalChecks.java
public interface OptionalChecks {
} with the following Camel route, only the @NotNull constraints on the attributes manufacturer and licensePlate will be validated (Camel uses the default group jakarta.validation.groups.Default).
-
Java
-
XML
-
YAML
from("direct:start")
.to("bean-validator://x")
.to("mock:end"); <route>
<from uri="direct:start"/>
<to uri="bean-validator://x"/>
<to uri="mock:end"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: bean-validator://x
- to:
uri: mock:end If you want to check the constraints from the group OptionalChecks, you have to define the route like this
-
Java
-
XML
-
YAML
from("direct:start")
.to("bean-validator://x?group=OptionalChecks")
.to("mock:end"); <route>
<from uri="direct:start"/>
<to uri="bean-validator://x?group=OptionalChecks"/>
<to uri="mock:end"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: bean-validator://x
parameters:
group: OptionalChecks
- to:
uri: mock:end If you want to check the constraints from both groups, you have to define a new interface first:
AllChecks.java
@GroupSequence({Default.class, OptionalChecks.class})
public interface AllChecks {
} And then your route definition should look like this:
-
Java
-
XML
-
YAML
from("direct:start")
.to("bean-validator://x?group=AllChecks")
.to("mock:end"); <route>
<from uri="direct:start"/>
<to uri="bean-validator://x?group=AllChecks"/>
<to uri="mock:end"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: bean-validator://x
parameters:
group: AllChecks
- to:
uri: mock:end And if you have to provide your own message interpolator, traversable resolver and constraint validator factory, you have to write a route like this:
-
Java
-
XML
from("direct:start")
.to("bean-validator://x?group=AllChecks&messageInterpolator=#myMessageInterpolator"
+ "&traversableResolver=#myTraversableResolver&constraintValidatorFactory=#myConstraintValidatorFactory")
.to("mock:end"); <bean id="myMessageInterpolator" class="my.MessageInterpolator" />
<bean id="myTraversableResolver" class="my.TraversableResolver" />
<bean id="myConstraintValidatorFactory" class="my.ConstraintValidatorFactory" /> It’s also possible to describe your constraints as XML and not as Java annotations. In this case, you have to provide the files META-INF/validation.xml and constraints-car.xml which could look like this:
-
validation.xml
-
constraints-car.xml
<validation-config
xmlns="https://jakarta.ee/xml/ns/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/configuration https://jakarta.ee/xml/ns/validation/validation-configuration-3.0.xsd"
version="3.0">
<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> <constraint-mappings
xmlns="https://jakarta.ee/xml/ns/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/mapping
https://jakarta.ee/xml/ns/validation/validation-mapping-3.0.xsd"
version="3.0">
<default-package>org.apache.camel.component.bean.validator</default-package>
<bean class="CarWithoutAnnotations" ignore-annotations="true">
<field name="manufacturer">
<constraint annotation="jakarta.validation.constraints.NotNull" />
</field>
<field name="licensePlate">
<constraint annotation="jakarta.validation.constraints.NotNull" />
<constraint annotation="jakarta.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> Here is the XML syntax for the example route definition where OrderedChecks can be https://github.com/apache/camel/blob/main/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/OrderedChecks.java
Note that the body should include an instance of a class to validate.
-
Java
-
XML
-
YAML
from("direct:start")
.to("bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks"); <route>
<from uri="direct:start"/>
<to uri="bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: bean-validator://x
parameters:
group: org.apache.camel.component.bean.validator.OrderedChecks