Spring Validation vs Hibernate Validation

Nova Guy picture Nova Guy · Jan 11, 2018 · Viewed 11.6k times · Source

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.

Answer

Alex M981 picture Alex M981 · Jan 11, 2018

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/