Generic JavaBeans Validation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 10: Line 10:
=Overview=
=Overview=


This article documents an executable Java example that shows how to use JavaBeans Validation annotations to externalize validation logic to a JavaBeans [[Bean_Validation#Validation_Provider|validation provider]]. This example depends on Spring Boot only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, which will be used as  [[Bean_Validation#Validation_Provider|validation provider]], and get our runtime running. However, 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 that shows how to use JavaBeans Validation annotations to externalize validation logic to a JavaBeans [[Bean_Validation#Validation_Provider|validation provider]]. This example depends on Spring Boot only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, which will be used as  [[Bean_Validation#Validation_Provider|validation provider]], and get our runtime running. However, there is nothing Spring-specific that would prevent the same example working in a generic Java environment with JSR-303 support.


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>

Revision as of 01:44, 16 October 2018

External

Internal

Overview

This article documents a runnable Java example that shows how to use JavaBeans Validation annotations to externalize validation logic to a JavaBeans validation provider. This example depends on Spring Boot only because we use Spring Boot's machinery to pull starter dependencies, including Hibernate Validator, which will be used as validation provider, and get our runtime running. However, there is nothing Spring-specific that would prevent the same example working in a generic Java environment with JSR-303 support.

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

Approach

Declare Validation Rules

Instantiate a Validator

Validate and Handle Constraint Violations

Playground Example

GitHub Generic JavaBeans Validation Example