November 1, 2009

The Promising Standardized JSR 303 Bean Validation and Example of Usage

For too long have the Java world lived without a standardized bean validation specification and soon will the final approval hopefully be approved (2 Nov 2009). What does this imply?
  1. Better Domain Driven Design, by putting the shallow domain validation in the POJO where it belongs.
  2. Hopefully we can finally get rid of Value Objects Pattern which is a dark heritage from the EJB 2.1 age and instead use domain POJO beans through out our architecture. There is of course exception to this rule, e.g. in service layer where the exposed model is totally different than the domain model or in a UI layer with heavy usage of line charts, there an OO model does not fit into a point orientation representation.
What is still to be proven is how the specification will fit into other framework, such as JPA, Apache Wicket or Spring Remoting. But one can rest ashore if one is using the standard validation model, the odds increase dramatically. And presumably will big framework such as Spring Framework and Apache Wicket adjust there framework to the standard.

Here follows a simple example of the usage of Bean Validation.

<project xmlns=""

<name>Validation :: Domain</name>



<!-- common logging library -->
<!-- version is depended on hibernate-annotations v3.4.0.GA -->
<!-- sun bean validation api -->
<!-- hibernate bean validation impl -->
<!-- test support -->



package se.msc.examples.validation.domain;


import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private Long personId;
private String firstName;
private String surname;
private String mail;

public Long getPersonId() {
return personId;

protected void setPersonId(Long personId) {
this.personId = personId;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getSurname() {
return surname;

public void setSurname(String surname) {
this.surname = surname;

public String getMail() {
return mail;

public void setMail(String mail) {
this.mail = mail;


package se.msc.examples.validation.domain;

import static org.junit.Assert.*;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class PersonTest {
private Validator validator;
private Person person;

public static void oneTimeSetUp() throws Exception {  

public static void oneTimeTearDown() throws Exception {

public void setUp() throws Exception {
validator = Validation.buildDefaultValidatorFactory().getValidator();
person = new Person();
person.setFirstName("Magnus K");

public void tearDown() throws Exception {

private  void debugPrint(Set> violations) {
for (ConstraintViolation violation : violations) {
String propertyPath = violation.getPropertyPath().toString();
String message = violation.getMessage();
System.out.println("invalid value for: '" + propertyPath + "': " + message);

public void testValidate_OK() throws Exception {
Set> violations = validator.validate(person);
assertTrue(violations.size() == 0);

public void testValidate_FAIL_MAIL() throws Exception {
Set> violations = validator.validate(person);
assertTrue(violations.size() == 1);

public void testValidate_FAIL_SURNAME() throws Exception {
Set> violations1 = validator.validate(person);
assertTrue(violations1.size() == 1);

Set> violations2 = validator.validate(person);
assertTrue(violations2.size() == 1);


The resource bundle message
javax.validation.constraints.Null.message=must be null
javax.validation.constraints.NotNull.message=must not be null
javax.validation.constraints.AssertTrue.message=must be true
javax.validation.constraints.AssertFalse.message=must be false
javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.Max.message=must be less than or equal to {value}
javax.validation.constraints.Size.message=size must be between {min} and {max}
javax.validation.constraints.Digits.message=numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)
javax.validation.constraints.Past.message=must be a past date
javax.validation.constraints.Future.message=must be a future date
javax.validation.constraints.Pattern.message=must match the following regular expression: {regexp}

JSR 303: Bean Validation

Article comparing JSR 303 Reference Implementation And Spring 2.5 Validation

JSR 303 Reference Material

JSR 303 Specification Leads Emmanuel Bernard Blog


w3c said...

Nice information, I really appreciate the way you presented.Thanks for sharing..

Anonymous said...

Thanx that helped a lot
regards Pascal

Rajesh Kumar said...

which jar file contains this validation NotNull?

Magnus K Karlsson said...

The validation-api-1.0.0.GA.jar that is added with Maven dependency.