How to Perform Validation in Spring
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.
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.
Creating Entity Class
Creating Controller Class
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.
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:
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.