Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Hibernate Validator is the reference implementation of the Jakarta Bean Validation specification (formerly Java Bean Validation), which allows developers to express and validate application-level constraints on Java beans. With Spring Boot, integrating Hibernate Validator is straightforward, enabling automatic validation and easy handling of validation errors.
Here's how you can set up validation using Hibernate Validator in a Spring Boot application:
If you have spring-boot-starter-web
in your project, Hibernate Validator is already included. Otherwise, you can add it directly:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- OR if you just need Hibernate Validator --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>your_version_here</version> </dependency>
Annotate fields in your model or DTO with validation constraints:
public class UserDTO { @NotBlank(message = "Username is mandatory") private String username; @Email(message = "Email should be valid") private String email; @Size(min = 8, message = "Password should have at least 8 characters") private String password; // getters and setters }
Your annotated beans will automatically be validated when used in a controller method with the @Valid
annotation:
@RestController @RequestMapping("/users") public class UserController { @PostMapping("/register") public ResponseEntity<?> registerUser(@Valid @RequestBody UserDTO userDTO) { // Handle the registration logic return ResponseEntity.ok("User registered successfully"); } }
You can handle validation errors using Spring's exception handling mechanisms. A common way is to use @ExceptionHandler
:
@RestControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<List<String>> handleValidationExceptions(MethodArgumentNotValidException ex) { List<String> errors = ex.getBindingResult() .getAllErrors().stream() .map(DefaultMessageSourceResolvable::getDefaultMessage) .collect(Collectors.toList()); return ResponseEntity.badRequest().body(errors); } }
When a validation error occurs, the custom exception handler will capture it and return a list of error messages.
You can customize error messages in multiple ways:
Directly in the annotation: As shown in the UserDTO
example above.
Using a message properties file: Create a messages.properties
file in the src/main/resources
directory and use keys for messages:
user.email.invalid=Provided email is invalid!
Then, in the model, use the key:
@Email(message = "{user.email.invalid}") private String email;
Hibernate Validator provides a rich set of built-in constraints, and you can also define custom constraints if needed. This way, you can create complex validation logic tailored to your application's requirements.
With these steps, you can effectively set up validation in your Spring Boot application, ensuring that the data your application processes are both consistent and valid.
Configuring Hibernate Validator in Spring Boot:
application.properties
or application.yml
.# application.yml spring: jpa: hibernate: ddl-auto: validate messages: basename: validationMessages
Using annotations for field-level validation in Spring Boot:
javax.validation.constraints
package for field-level validation, such as @NotNull
, @Size
, and @Email
.public class User { @NotNull @Size(min = 2, max = 50) private String username; @Email private String email; // Other fields and methods }
Custom constraints and validation groups with Hibernate Validator in Spring Boot:
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = CustomValidator.class) public @interface CustomConstraint { String message() default "Custom validation failed"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
Cross-field validation in Spring Boot with Hibernate Validator:
@ScriptAssert
or by creating custom cross-field validators.public class User { @NotBlank private String password; @NotBlank private String confirmPassword; // Other fields and methods }
Validating method parameters and return values in Spring Boot:
@Validated
on the method or by using parameter-level annotations.@Service @Validated public class UserService { public User createUser(@NotNull @Valid User user) { // Business logic } }
Integration of Hibernate Validator with Spring Boot controllers:
@Valid
on method parameters to trigger validation.@RestController public class UserController { @PostMapping("/users") public ResponseEntity<User> createUser(@Valid @RequestBody User user) { // Controller logic } }
Error handling and displaying validation messages in Spring Boot:
@ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<Object> handleValidationErrors(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { // Handle and customize validation errors } }
Testing validation in Spring Boot applications with Hibernate Validator:
@RunWith(SpringRunner.class) @SpringBootTest public class UserValidationTest { @Autowired private Validator validator; @Test public void testUsernameNotBlank() { User user = new User(); user.setUsername(""); // Blank username Set<ConstraintViolation<User>> violations = validator.validate(user); // Assert validation results } }