banner



How To Create Custom Annotation In Spring Boot

Spring Boot - Creating a Custom Annotation for Validation

Validation using Java Custom Annotations

Rushikesh Patil

Spring boot Annotations

Spring Boot Annotations

While I was working on a Spring Boot Project, I found that custom annotations were being used for validation. The best way to understand how it works as well as how we can use custom annotations for validation is to create simple use cases and try to understand how it works.

What is an Annotation?

"An annotation is nothing but metadata about a field, parameter, method, constructor, type (class / interface / enum), package and annotation type".

Annotations don't do anything on their own. The JVM operates using annotations and metadata.

To know more about annotation feel free to read this article here .

We will learn how to create our own custom validator annotation using a simple example in spring boot. My setup for the demonstration project will look as follows:

  1. Add the spring-boot-starter-validation dependency

2. Create a Custom Annotation

Here, we will add a constraint to validate colorName field. Constraints are useful when you need to validate multiple fields on an object.

            @Constraint(validatedBy = ColorValidator.class)          

When validation fails, we return the error message mentioned for @ColorValidation annotation.

            public String message() default "Invalid color: must be RED, GREEN or BLUE";          

3. Add validation logic for colorName field from Color.java.

4. Add @ColorValidation annotation to Color.java model class

5. Create a Controller

The purpose of GET /color is to return color values for provided colorName from the request body, nothing too fancy here.

Maybe you're wondering here, what if we pass colors other than RED, GREEN, and BLUE.

Well, that's where our custom @ColorValidation annotation will come into play, and based on logic added into ColorValidator.java it will validate the field colorName and will throw an error if we pass the colorName other than RED, GREEN, and BLUE.

Adding to this, when Spring Boot encounters an argument annotated with @Valid, it bootstraps the default JSR 380 implementation, which is Hibernate Validator, and validates the argument.

When the target argument fails to pass the validation, in our case @ColorValidation, Spring Boot throws a MethodArgumentNotValidException exception.

6. The final step in the process is to add exception handlers to extract a default message from the custom validator annotation exception — MethodArgumentNotValidException.

Let's, test this…

  • When we pass valid color code RED, GREEN or BLUE :

  • If we pass an input that is not allowed, we see the following error message:

In this way, we can create different custom annotations for validation purposes.

You can find the full source code here.

Conclusion

It is easy to create and use custom annotations in Java. Java developers will be relieved of redundant code by using custom annotations. Additionally, annotations provide static type checking which is not possible with XML at compile time. There is no tool support required for annotations like XML-based validations and configurations since XML requires third-party tools and libraries to process validations and configurations. By considering the requirement and use cases, we can make good use of custom annotations.

How To Create Custom Annotation In Spring Boot

Source: https://blog.clairvoyantsoft.com/spring-boot-creating-a-custom-annotation-for-validation-edafbf9a97a4

Posted by: cumminstric1997.blogspot.com

0 Response to "How To Create Custom Annotation In Spring Boot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel