Spring boot request validation

theoneamin
2 min readMay 23, 2022

In this tutorial, we’ll take a look at how to validate spring boot requests. This is going to be a quick and easy way to have validation for your request parameters.

First, we need to make sure we have the required dependencies for this to work.

Then we’ll create a class for our request. We’ll add the not-empty annotation. This will make sure that empty strings are not sent.

In the message, we can put whatever we want the user to see if the validation fails. We can make that even neater and create a custom message source.

Then in the controller, we need to add the “@Valid” annotation before the request body.

Then finally, add “@Validated” to the controller class.

Now if I send a request where there’s no password, I get the following response.

Thanks for reading. I hope you found this helpful.

--

--

theoneamin
theoneamin

Responses (1)