DEV Community

Djordje Bajic
Djordje Bajic

Posted on • Edited on

2

Apache Camel #4 - Bean Validation

There is a lot of cases where we needed to write validation for data input. This "problem" can be easily solved in Camel by using Bean Validation component.

Imagine we have a JSON input in our route and we want to validate payload.

{
     "name":"Jane Doe",
     "cardNumber":"377198642272436",
     "email":"jane@doe.com",
     "paid":true,
     "price":1.25
}

To do that, we first we must unmarshall that payload to POJO.
But first, we must create a class with the same parameters as JSON and add Hibernate Validation annotations.

 @NotNull
 @Length(min = 1, max = 60)
 private String name;

 @CreditCardNumber
 private String cardNumber;

 @Email
 private String email;

 @NotNull
 private boolean paid;

 @Min(0.1)
 private int price;
Enter fullscreen mode Exit fullscreen mode

And then we add this to our Camel route.

   //define a datasource for the class you want to validate.
   GsonDataSource dataSource = new GsonDatasource(JsonDto.class);

   //unmarshal JSON to POJO
   .unmarshall(dataSource)

   //Validate data
   .to("bean-validator:someLabelHere)

When that unmarshalled data is sent to bean validator, that objects will be validated by annotations we set for JsonDto fields.

Thank you for your attention.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up