This tutorial explains how to create, implement and apply custom annotations for form validation using Spring framework. Custom annotations are helpful to define and set up custom validation rules based on business requirements. Spring framework provides a quick, easy and efficient way of defining validation rules based on your business logic.
Following tech stack was used for this tutorial:
Java (JDK 1.8)
Spring framework (4.3.10)
Java Server Pages (JSP)
Eclipse Spring Tool Suite (3.8.2)
Requirement: As part of a simple MVC web application, we would like to create a custom annotation to add a validation rule for one of the input fields on the form.
In our example below, we add a validation rule to check if the course code entered by the user on a form begins with a certain prefix.
Let’s get started.
First, we create a custom annotation named @CourseCode. This can be done by going to File->New->Annotation in Eclipse. You will get the following window where you can specify the annotation name to be “CourseCode”:
You then need to add the following annotations: @Constraint, @Target, @Retention.
The @Constraint annotation specifies which class will hold the business logic for validation. In our case, we create a new class called “CourseCodeConstraintValidator” which will have the validation implementation. The @Target annotation specifies where this annotation can be applied. In our case, we will use this for fields and methods. The @Retention annotation specifies how long do we need this annotation. In our case, we would like this annotation to be part of the .class files (bytecode) and be processed at runtime.
Now, we add methods to this interface depending on what attributes we want the @CourseCode annotation to accept.
For this tutorial, we add two attributes: “value” which indicates what should the prefix be for the course code (our custom business logic), “message” which indicates the error message that needs to be displayed to the user if the input does not start with the right prefix.
Second, we implement the validation in “CourseCodeConstraintValidator” class:
Here, we check whether the user input starts with the right prefix. For our tutorial, we want the user input to start with prefix “TOPS”. Finally, we add this annotation to the field in the Customer model class.
In the controller, Spring calls the above isValid() method while performing validation:
On the front-end, when the user enters an invalid course code, our custom error message is displayed:
That’s it. We now have a custom annotation based on our custom business logic.
Credits: The example in this article was inspired from a Spring & Hibernate For Beginners course by Chad Darby on Udemy.
Disclaimer: The aim of this article is to explain a complex topic in a short time for the reader and is in no way, shape or form intended towards discrediting the instructor’s work.
For the complete source code, please take a look at my GitHub repository:
If you enjoyed reading this article, please leave a comment or hit the clap icon below to show your appreciation. Feel free to recommend and share this article. Thanks for your time. Happy coding!
Do you wish to improve your problem-solving skills for coding interviews? Do you struggle to apply data structures and algorithms while solving a problem? Sign up for AlgoExpert.io. I can personally attest that having gone through the questions and video explanations, they are the best resource for coding interview preparation on a tight time frame.
Use promo code: nwpxi-35 to get a 30% discount on AlgoExpert 1-year subscription.
Amrut is a Full Stack Software Engineer who is passionate about tech and software development in Web and Mobile. He likes to write about tech, coding and software development. He also loves to watch and discuss about American Football.