How to add validation to your Spring Entities

A Short tutorial on how to add data validation to your Spring Entities.

A student had a question about validating data at the domain level and so I thought it would share it with you. 

Spring Data Validation

Hi Dan, 

First of all thanks a lot for this great course. It really helped me to get into Spring. But now I’m facing a problem and didn’t find a solution yet. I want to use for my DTO classes annotations like @NotNull (javax.validation.constraints) or custom annotations. But both don’t work within spring boot. Do you know a good way or practice to solve this? Or is it too expensive to make these annotations work? If yes, is there a spring alternative for such annotations that execute a custom validation like a license-plate for instance? 

I hope this question isn’t too off-topic to this course and perhaps also interesting for another member of this course.

Best wishes,
Daniel  

Before we get started I just want to thank Daniel for the question. If you want to follow along with this project you can grab the source code here. We are going to start a new project and select the Web, JPA & H2 dependencies. 

Validation

I am gong to create an entity called the city and the key here is to look at the state property. We are using an annotation on the state @NotNull. This says that we create a new city and try to save it that the state can’t be null. 

I then create a Command Line Runner to insert a new record. I am intentionally not adding the state to this object. 

When we try and run this application you will see the following error. 

This was so easy to do and the great thing is it doesn’t stop there. If you want to add all kinds of validation to different properties you can. Check out the documentation to find a list of annotations you can add for validation. 

Custom Validation

Most of the time the annotations provided will get the job the done. There are times when you need some type of custom validation done. In these cases, we can create our own custom validator and it’s really easy to do. 

Say on our City object we wanted an annotation where we can make sure the state was equal to “OHIO”. I know this is a silly example but I want to keep it simple. This is what our domain object would look like now. 

Now we create our own annotation & StateValidatorCheck constraint. 

Now if we try and create a city object with a state other than OHIO we will get an error. 

Conclusion 

As you can see its pretty easy to sprinkle in some validation in your Spring Boot applications. 

Question: What are the challenges you face in validating data?

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.