Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Validation vs Hibernate Validation

I am confused between the two now. I know Hibernate Validator 6 is the reference implementation for Bean Validation 2.0 specs. It supports Grouping, Internationalization of error messages, Custom Method Validations etc. Question is does Spring 5 support these features or am I only left with Hibernate Validator 6?

Why should I used Hibernate Validator along with my Spring Boot project instead of the provided Spring Validation? My company needs a justification to use any third party library.

All the reference example online suggest to use Hibernate Validator and nothing much found on Spring validation, please advise or point to other links.

like image 347
Nova Guy Avatar asked Jan 11 '18 05:01

Nova Guy


2 Answers

according to Spring Validation documentation

With Bean Validation, a single javax.validation.Validator instance typically validates all model objects that declare validation constraints. To configure such a JSR-303 backed Validator with Spring MVC, simply add a Bean Validation provider, such as Hibernate Validator, to your classpath

you can verify that by creating sample spring-boot project with spring-boot-starter-web dependency. It actually adds hibernate-validator dependency into your classpath

Here are couple of links to jsr-303 in spring tutorials:

  1. https://howtodoinjava.com/spring/spring-mvc/spring-bean-validation-example-with-jsr-303-annotations/

  2. https://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

like image 72
Alex M981 Avatar answered Sep 18 '22 13:09

Alex M981


Hibernate Validation is implementation of JSR 303: Bean Validation API. Spring has its Validation package (it supports JSR 303: Bean Validation API but not proper implimentation).

You could note that org.springframework.validation.Validator is different from javax.validation.Validator.

You can perform Spring Validation just by creating a class implementing org.springframework.validation.Validator as simple as here But in case you need to follow the specifications of JSR 303: Bean Validation API you do it through Hibernate Validator.

Okay to put in some more details.

1) If you want to perform (some) validation, this can be done using spring. (below is some snippet):

import org.springframework.validation.Validator;

class MyService{
     Validator validator =  new MyValidator();
     //perform validation
    }
class MyValidator implements Validator{
    // Your own validation logic. You may use ValidationUtils to help. 

}

2) If you want to perform (JSR 303 specification) validation you need to have its provider like Hibernate.

import javax.validation.Validator;
class MyService{
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); //Bootstraping
        Validator validator = factory.getValidator(); 
       //perform validation
}

The Bootstraping process above,is supported by Spring Framework. All you need to do is let spring create the bean for LocalValidatorFactoryBean

<bean id="validator"   
 class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

and inject this bean.

import javax.validation.Validator;

@Service
public class MyService {

    @Autowired
    private Validator validator;
}
like image 31
sql_dummy Avatar answered Sep 18 '22 13:09

sql_dummy