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>
<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>
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Copy to ClipboardCopied!Toggle word wrapToggle overflow
or
bean-validator://label[?options]
bean-validator://label[?options]
Copy to ClipboardCopied!Toggle word wrapToggle overflow
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&…
To use Hibernate Validator in the OSGi environment use dedicated ValidationProviderResolver implementation, just as org.apache.camel.component.bean.validator.HibernateValidationProviderResolver. The snippet below demonstrates this approach. Keep in mind that you can use HibernateValidationProviderResolver starting from the Camel 2.13.0.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If no custom ValidationProviderResolver is defined and the validator component has been deployed into the OSGi environment, the HibernateValidationProviderResolver will be automatically used.
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
}
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 5, max = 14, groups = OptionalChecks.class)
private String licensePlate;
// getter and setter
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
and an interface definition for our custom validation group
OptionalChecks.java
public interface OptionalChecks {
}
public interface OptionalChecks {
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
with the following Camel route, only the @NotNull constraints on the attributes manufacturer and licensePlate will be validated (Camel uses the default group javax.validation.groups.Default).
Copy to ClipboardCopied!Toggle word wrapToggle overflow
It’s also possible to describe your constraints as XML and not as Java annotations. In this case, you have to provide the file META-INF/validation.xml which could looks like this
We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.
Making open source more inclusive
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.
About Red Hat
We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.