Introducing validations in JAX-RS applications
Introducing validations in JAX-RS applications
Validation is the process of ensuring the completeness and sanity of business data before posting it to the underlying data source. Validating the client input is very important for any REST API that you build. In this section, we will see the offering in JAX-RS for validating client input.
The JAX-RS 2.x release allows you to validate the resource class and methods via Java Bean Validation. This framework is a Java specification, which lets you specify the validation rules on the objects or hierarchy of objects via the built-in annotations; it also lets you write the reusable validation rules for handling various use cases. Bean Validation is integrated into the Java EE platform, allowing developers to easily define and enforce the validation rules across various components that come as a part of Java EE. The following is a list of Java EE components that support Bean Validation: JAX-RS, JAXB, JPA, EJB, JSF, and CDI.
The latest release of Bean Validation is Version 2.0, which is based on JSR 380 and formerly JSR 349. You can learn more about JSR 380 at http://beanvalidation.org/2.0/spec/#introduction.
A brief introduction to Bean Validation
The Bean Validation framework allows you to specify the validation rules in the form of annotations on a field, method, or a Java class. You can either use built-in constraints offered by Bean Validation or build custom constraints by using the extension mechanisms offered by the framework. The following table summarizes the built-in constraints offered by Bean Validation that are applicable to all data types:
Validation constraints | Supported Java types | Details of the imposed constraints |
@NotNull | Applicable to all Java types | The annotated variable must not be null. |
@Null | Applicable to all Java types | The annotated variable must be null. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the Boolean data types:
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the number data types:
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the date data types:
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the string data types:
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the collection and string data types:
The following code snippet illustrates the use of built-in validation constraints provided by Bean Validation for validating an input to a JAX-RS method call. This example validates the input parameter for a positive number: Building custom validation constraintsIn the previous section, we had a quick look at the built-in validation constraints offered by the Bean Validation framework. There may be cases where you may want to build your own validation constraints. The Bean Validation framework supports that as well. The core contracts that you should implement for a custom validation constraint are as follows:
Let's build a custom validation constraint for checking whether a department with the same name already exists for a given location. This constraint avoids the duplicate department entities. The first step is to build an annotation for the custom constraint that you are going to construct. The Bean Validation framework specification demands the following attribute definitions in a constraint annotation:
Let's define an annotation for the custom validation constraint, ValidDepartmentValidator, which we are going to build next: As the next step, we need to provide the implementation for the custom validation constraint called ValidDepartmentValidator. Here is the code snippet: The last step is to define the error message in a resource bundle in order to localize the validation error messages. This file should be added to the root folder with the following name: ValidationMessages.properties. The file contents may look like the following code: This feature lets you localize the validation error messages. For example, ValidationMessages_fr.properties contains messages for the French locale. The custom validation constraint for checking a duplicate department entity is ready for use now! You can apply this constraint on the method parameters or class fields, as shown in the following code snippet: What happens when Bean Validation fails in a JAX-RS application?The framework throws javax.validation.ValidationException or its subclass exceptions, such as javax.validation.ConstraintViolationException, when the Bean Validation rules that you set on the model class fails at runtime. The JAX-RS runtime will report the validation error back to the client with a 500 (internal server error) HTTP status code. You can override the default validation exception handling by providing a custom javax.ws.rs.ext.ExceptionMapper class. We discussed ExceptionMapper a while ago in the Mapping exceptions to the response message using ExceptionMapper section. |
Comments
Post a Comment