Validation
A good service always validates data before processing it. In this section, we will look at the ;Bean Validation API and use its reference implementation to implement validation in our services.
The Bean Validation API provides a number of annotations that can be used to validate beans. The ;JSR 349 ;specification defines Bean Validation API 1.1. Hibernate-validator is the reference implementation. ;Both are already defined as dependencies in the spring-boot-web-starter
project:
hibernate-validator-5.2.4.Final.jar
validation-api-1.1.0.Final.jar
We will create a simple validation for the createTodo service method.
Creating validations involves two steps:
- Enabling validation on the controller method.
- Adding validations on the bean.
Enabling validation on ;the controller method
It's very simple to enable validation on the controller method. The following snippet shows an example:
@RequestMapping(method = RequestMethod.POST, path = "/users/{name}/todos") ResponseEntity<?>...