Creating Custom Annotations for Validation in Spring Boot

Creating Custom Annotations for Validation in Spring Boot

1. Overview

While Spring standard annotations (@NotBlank, @NotNull, @Min, @Size, etc.) cover many use cases when validating user input, there are times when we need to create custom validation logic for a more specific type of input. In this article, I will demonstrate how to create custom annotations for validation.

Article content
Creating Custom Annotations for Validation in Spring Boot

2. Setup

We need to add the spring-boot-starter-validation dependency to our pom.xml file.

Article content
pom.xml

3. Custom Field Level Validation

3.1 Creating the Annotation

Let’s create custom annotations to to validate file attributes, such as file extension, file size, and MIME type.

Article content
ValidFileExtension
Article content
ValidFileMaxSize
Article content
FileMimeTypeValidator

Let's break down these annotations' components:

  • @Constraint: Specifies the validator class responsible for the validation logic.
  • @Target({ElementType.FIELD}): Indicates that this annotation can only be applied to fields.
  • message(): The default error message if the validation fails.

3.2. Creating the Validator

Article content
FileExtensionValidator
Article content
FileMaxSizeValidator
Article content
FileMimeTypeValidator

These classes are implementations of the ConstraintValidator interface and contain the actual validation logic.

For FileMimeTypeValidator, we will use Apache Tika (a toolkit designed to extract metadata and content from numerous types of documents).

3.3 Applying the Annotation

Let's create a TestUploadRequest class intended for handling file uploads, specifically for a PDF file.

Article content
TestUploadRequest
Article content
TestController

4. Custom Class Level Validation

A custom validation annotation can also be defined at the class level to validate a combination of fields within a class.

4.1 Creating the Annotation

Let’s create @PasswordMatches annotation to ensure that two password fields match in a class.

Article content
PasswordMatches

  • @Target({ElementType.TYPE}): Indicates that this annotation targets a type declaration.

4.2. Creating the Validator

Article content
PasswordDto
Article content
PasswordMatchesValidator

The PasswordDto interface is an interface for objects that contain a password and a confirm password field.

The PasswordMatchesValidator class implements the ConstraintValidator interface and contains the logic for validating that the password and confirm password fields match.

4.3 Applying the Annotation

Let's create a RegisterAccountRequest class intended for handling user registration data.

Article content
RegisterAccountRequest
Article content
AuthController

5. Summary

In this short article, we discoverd how easy it is to to create custom annotations to verify a field or class. The code from this article is available over on my Github.

6. References

To view or add a comment, sign in

More articles by Nguyen Hai Dang (Eric)

Others also viewed

Explore content categories