Este conteúdo não está disponível no idioma selecionado.
Chapter 46. Bean Validator Component
Available as of Camel version 2.3
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
Copiar o linkLink copiado para a área de transferência!
bean-validator:label[?options]
bean-validator:label[?options]
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&…
Copiar o linkLink copiado para a área de transferência!
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.
Copiar o linkLink copiado para a área de transferência!
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
Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.
Tornando o open source mais inclusivo
A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.
Sobre a Red Hat
Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.