How to Perform Validation in Spring

3 min readAug 14, 2023

Hello there, lovely readers! Today, we’re diving deep into the world of Spring Validations and their associated annotations. As developers, we understand the significance of data validation in ensuring the integrity and reliability of our applications. Spring Framework provides strong and flexible validation mechanisms, enabling us to implement business rules effectively and gracefully handle erroneous data.

What is this Validation?

It is a library in the spring framework. While writing an application, we may want to have our private fields and to be able to restrict their features. Such as password, username, email, etc. The Spring Validation library helps us to avoid writing all the rules one by one.

Moreover, we have an assistant who makes it very easy. As I mentioned at the beginning of the article, we do it with the spring validation library, thanks to its annotations. We can just write it by prefixing it with an @ sign.

We’ll take a look at the types of validation annotations.

  • @NotNull : It can be inferred from its name that a field will not be null.
  • @NotEmpty : It specifies that a list field cannot be empty.
  • @NotBlank : Specifies that a string field cannot be empty.
  • @Max and @Min : It specifies the bottom and top values of a number field.
  • @Size : The size must be equal to the specified value is what it determines.
  • @Pattern : A string field is valid only if it matches the specified regex.
  • @Email : It indicates that a valid email is required.
We can just write @NotNull as seen in the photo, or we can also write our message in it, like the usage above.

If you want to take a look at various validations, you can click here.

How To Use?

We don’t need to create a separate class to use it. Instead, @Valid annotation and @Validation should be written in the necessary places.

It’s possible that the API controller is the only location where we can validate.

Adding Dependancy

Our first step is to add the relevant dependencies to the pom.xml file.

The project’s dependencies are generally the same.

Creating Entity Class

Creating Controller Class

As I mentioned while talking about how to use it, we don’t need to open a separate class for validation. As you can see from the code snippet above, we just need to put the @Valid annotation in front of the other annotation.

Adding Validation Annotations

Everything seems fine and easy when using Validation, but what happens if we don’t? Does it throw an error?

We already know that the code we write in the Service class includes logic operations. Spring says that thanks to the validation library, you don’t need to write the logic part, it already does it in the background.

We can also leave it unchanged in the Controller class. If we do this, we will need to add the @Valid annotation when we want to use the validation annotations. To avoid typing the same things repeatedly, we can add the @Valid annotation before @RequestBody in the Controller class to eliminate this.

The Valid annotation and RequestBody annotation are combined when performing the getUser operation. With this process, Spring will convert the JSON data that comes with the HTTP Request into a Java object and first check it. If it passes the check, no problem. However, if a method marked with Valid fails to pass the check, it will throw an HTTP status 400 (Bad Request) error.

To Sum Up

In all projects, validation of data is of great importance. Spring validations in Java make it easy for us to do this.

In this tutorial, we’ve gone through quickly skimming the basic validation features we might need when building an application with Spring Boot.

PS: You can find similar pieces of code that I mentioned briefly in the demo_project on my GitHub account.

To follow me on GitHub:

https://github.com/zgokceaynaci

To follow me on LinkedIn:

https://www.linkedin.com/in/zgokceaynaci/

Your feedback is crucial to me as it will assist me in improving. So don’t hesitate to give feedback.

Thank you for reading. See you in the next article, bye for now.

--

--

Zehra Gökçe Aynacı
Zehra Gökçe Aynacı

Written by Zehra Gökçe Aynacı

Learning, growing, and chasing excellence—one challenge at a time.

No responses yet