Generic JavaBeans Validation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:


* [[Spring_Validation_Concepts#Generic_JavaBeans_Validation|Spring Validation Concepts]]
* [[Spring_Validation_Concepts#Generic_JavaBeans_Validation|Spring Validation Concepts]]
* [[Bean_Validation#Generic_JavaBeans_Validation_Example|Bean Validation]]
* [[JavaBeans_Validation#Generic_JavaBeans_Validation_Example|JavaBeans Validation]]


=Overview=
=Overview=


This article documents a runnable Java example uses JavaBeans Validation annotations to externalize validation logic to a JavaBeans [[Bean_Validation#Validation_Provider|validation provider]]. The example depends on [[Spring Boot]], but this is only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, and get our runtime running. There is nothing Spring-specific that would prevent the same example working in a generic Java environment with JSR-303 support.
This article documents a runnable Java example uses JavaBeans Validation annotations to externalize validation logic to a JavaBeans [[JavaBeans_Validation#Validation_Provider|validation provider]]. The example depends on [[Spring Boot]], but this is only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, and get our runtime running. There is nothing Spring-specific that would prevent the same example working in a generic Java environment with JSR-303 support. JavaBeans Validation can be also used directly in Spring MVC controllers, with the [[@Valid|@Valid]] annotation. This is an example how to do it: "[[Spring_Validation_Concepts#Bean_Validation_in_Spring_MVC|Bean Validation in Spring MVC]]".


=Approach=
=Approach=
Line 41: Line 41:
A certain number of standard validation annotations are available:
A certain number of standard validation annotations are available:


{{Internal|Bean_Validation#Annotations|JavaBeans Validation Annotations}}
{{Internal|JavaBeans_Validation#Annotations|JavaBeans Validation Annotations}}


Because we are using Hibernate Validator, Hibernate-specific validation annotations are also available:
Because we are using Hibernate Validator, Hibernate-specific validation annotations are also available:


{{Internal|Bean_Validation#Hibernate_Validation_Annotations|Hibernate Validation Annotations}}
{{Internal|JavaBeans_Validation#Hibernate_Validation_Annotations|Hibernate Validation Annotations}}


==Instantiate a Validator==
==Instantiate a Validator==
Line 62: Line 62:


==Validate and Handle Constraint Violations==
==Validate and Handle Constraint Violations==
<syntaxhighlight lang='java'>
import javax.validation.ConstraintViolation;
...
CreditCard cc = new CreditCard();
Set<ConstraintViolation<CreditCard>> violations = validator.validate(cc);
for(ConstraintViolation<CreditCard> v: violations) {
System.out.println(v.getMessage());
}
...
</syntaxhighlight>
The result of validating a CardAccount with a null first name is:
<syntaxhighlight lang='java'>
The first name cannot be null
</syntaxhighlight>
at stdout.


=Playground Example=
=Playground Example=


{{External|[https://github.com/ovidiuf/playground/tree/master/spring/generic-validation GitHub Generic JavaBeans Validation Example]}}
{{External|[https://github.com/ovidiuf/playground/tree/master/spring/generic-validation GitHub Generic JavaBeans Validation Example]}}

Latest revision as of 17:36, 16 October 2018

External

Internal

Overview

This article documents a runnable Java example uses JavaBeans Validation annotations to externalize validation logic to a JavaBeans validation provider. The example depends on Spring Boot, but this is only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, and get our runtime running. There is nothing Spring-specific that would prevent the same example working in a generic Java environment with JSR-303 support. JavaBeans Validation can be also used directly in Spring MVC controllers, with the @Valid annotation. This is an example how to do it: "Bean Validation in Spring MVC".

Approach

Spring Boot Starter Dependencies

The simplest way to get the project started is to use Spring Initializr and select "Validation". The Gradle configuration will get the following dependency:

dependencies {
    ...
    implementation('org.springframework.boot:spring-boot-starter-validation')
    ...
}

Declare Validation Rules

Use JavaBean Validation annotation to declare validation logic. Example:

public class CreditCard {
    ...
    @NotNull(message = "The first name cannot be null")
    private String firstName;
    ...
}

A certain number of standard validation annotations are available:

JavaBeans Validation Annotations

Because we are using Hibernate Validator, Hibernate-specific validation annotations are also available:

Hibernate Validation Annotations

Instantiate a Validator

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

...

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
...

Validate and Handle Constraint Violations

import javax.validation.ConstraintViolation;

...

CreditCard cc = new CreditCard();

Set<ConstraintViolation<CreditCard>> violations = validator.validate(cc);

for(ConstraintViolation<CreditCard> v: violations) {
 System.out.println(v.getMessage());
}

...

The result of validating a CardAccount with a null first name is:

The first name cannot be null

at stdout.

Playground Example

GitHub Generic JavaBeans Validation Example