DEV Community

Djordje Bajic
Djordje Bajic

Posted on • Updated on

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",

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.

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

 private String cardNumber;

 private String email;

 private boolean paid;

 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

   //Validate data

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.

Top comments (0)