Validation of data in ESB application at different layers of message processing is a requirement which is quite frequent. Often, the validation logic is implemented at different layers which is time consuming and also prone to errors. Therefore, it is necessary to separate domain model and the validation logic, further to it, it also has to be aimed for reuse with the help of framework.
This blog post focuses to bring the goodness of JSR303 Bean Validation model [Hibernate RI], Spring framework’s support for custom validation and a blend on how it will work in MuleSoft ESB.
Java Bean Validation (JSR303) is a framework as part of Java EE. The bean validation defines meta model and API for data validation. The implementation is done through annotations and is extensible, which can be reused by simply adding annotations to the domain model.
// Example – define constraints to a bean
public class Person {
@NotNull (message=’ERR-BS-001’)
public class Person {
@NotNull (message=’ERR-BS-001’)
@Size(min=1, max=16)
private String firstName;
@NotNull (message=’ERR-BS-002’)
@Size(min=1, max=16)
private String lastName;
Benefits of Spring Validators
Spring framework supports JSR303 bean validations and support for custom validation. Some of the advantages upon using Spring Validators are mentioned below:
A validation framework is necessary, which binds annotation validators, custom validators and error message handler.
To validate annotations, JSR303 RI is required in the library. Hibernate Validator or Apache’s BVal can be used to support annotation based validations.
It might be possible to use annotations in order to validate REST requests by using @Valid annotation. Mule ESB 6.1 has upgraded Jersey version 2.11 (RI) which supports validation using @Valid annotation prior to a REST method invocation (like in Spring MVC).
// update user profile after validation
@POST
@Consumes( MediaType.APPLICATION_JSON_VALUE )
public boolean updateProfile( @Valid UserProfile mProfile )
//…
}
If you would like to find out more about how APIs could help you make the most out of your current infrastructure while enabling you to open your digital horizons, do give us a call at +44 (0)203 475 7980 or email us at Salesforce@coforge.com
Other useful links:
API Recipes with MuleSoft Anypoint Platform