Custom Validation Annotation

In this lesson, we will create an annotation that implements some custom business rules. Spring will validate the user input against our custom validation rules and display our custom error message if the input is not in the desired format.

As an example, we will create a form to enter the record of matches between two players.

The form will prompt the user to enter two players, along with the head-to-head match score. The head-to-head score must follow a specific format. Two numbers must be separated by a hyphen. For example, 28-10 is a valid input. Characters are not allowed for this field. We will attach our custom annotation to validate the head-to-head score against the above-mentioned requirements.

First, we will create a new class called PlayerStats having three fields as follows:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.