How does JAVA bin validation work?

Let’s first take a look into JAVA bin validation basics all right in java we validate JAVA bin with a standard framework that is just 380 also known as bin validation 2.0 validating user input is a super common requirement in most applications and the java validation framework has become the defect standard for handling this kind of logic GSR-380 is a specification of the Java API for bin validation and this ensures that properties of bin meet specific criteria using annotations such as at Norton admin at max etc.

Job invalidation is just a specification or an API and we have some of the popular repress implementation for java bin validation API so hibernate validator is one of the popular reference implementations of validation API all right, so this is the basics of java bin validation just remember in java we have java bin validation framework which we can use to validate java bins and hibernate validator is one of the reference implementations of validation API.

Let’s take a look into the important bin validation annotations at not null annotation validates that the annotated property value is not null and add size annotation validates that the annotated property value has a size between the attributes min and max and this annotation can be applied to string collection map and array properties at min annotation validates that the annotated property has a value not smaller than the value attribute.

At max validates that the annotated property has a value no larger than a value attribute and at email, annotation validates that the annotated property is a valid email address at not empty annotation validates that the property value is not null or empty and this annotation can be applied to string collection map and array values at not blank annotation can be applied only to text values and validates that the property is not null or white space.

These are very important rotation we use while validating java bin properties now let’s take a look into how to validate java bins in spring boot wellspring boot provides good integration support with hibernate validator and hibernate validator is one of the most popular reference implementations of bin validation API and starting with spring boot 3.2.3 we need to explicitly add spring boot starter validation dependency before spring boot 2.3 hibernate validator is automatically available whenever we add spring boot web starter dependency but spring boot c 2.3 onwards we need to explicitly add this springboard starter validation dependency so remember this.

Let’s quickly create a springboard project in the spring tool suite I’d go to the file new and then choose spring starter project this is spring initializer which is indicated in spring tools root ID so let’s quickly use spring initializer to create a springboard project and let’s use springboard project name as spring boot validation and choose project type as marine 9 packaging a jar java gen 8 java language or java and just view group ID as net dot java guides and artifact ID is same as project name and just change the description like demo project for springboard and hibernate validator and change the package net dot java guides dot spring boot go to the next and keep spring boot version as it is that is 2.4.3.

This is a stable and latest release of spring boot and it is recommended by the spring boot team so let’s keep the default release of the springboard version as it is and let’s go ahead and let’s quickly select spring web starter dependency so we are going to develop a simple rest API to you know do validation in spring boot project all right and let’s go and let’s also choose a validation dependency.

Validation dependency provides a hibernated validator internally okay so before spring boot 2.3 whenever you add spring web starter dependency then hibernate validator was available automatically but spring boot 2.3 onwards we need to explicitly add this validates and dependency to our springboard project so just remember this point springboard 2.3 onwards we are going to add validation dependency to integrate hibernate validator in our springboard project all right great let’s go ahead and select h2 database dependency and also select spring data GPA dependency all right now let’s go ahead and let’s hit finish.


This will create a brand-new spring boot project in the spring tool suite ID all right our spring boot application is created successfully and now what we do will go to the build path and I’m going going to change the default java Sc that is 1.8 to JDK just edit the JRE system library and select JDK 1.8 and hit finish apply clothes now let’s go and let’s quickly create few packages so let’s say, controller.

Let’s create one more package and let’s name it as a model and let’s go and let’s create one more package and let’s name it as repository all right and let’s go ahead and let’s create one more package let’s name it as service all right now what we’re going to do is we’re going to create a simple entity that is simple user entity right-click new one choose the class and let’s give class name as a user and let’s quickly create private fields in this user class private long id private string name private stream email and then private string password.

Now quickly create data setter methods right click source and generate header status select all and generate and also create a parameterized constructor right click source and generate constructor using fields and deselect I’d generate and also create a default constructor so make sure that whenever you create a parameters’ constructor you need to also create you know default constructor because once we make this class as a JP entity then hibernate internally uses proxies or to create an object using default constructor so make sure that whenever you create a parameterized constructor you have to also create a default constructor otherwise you will get exception now let’s make this class as a JP entity by adding entity annotation and let’s add table annotation to provided table information like name.

Leave a Comment