Bean Validation 2.0
Bean Validation 2.0 added more alignments with Java 8 and CDI, and bring a series of new annotations.
  • Supports Java 8 Date time and Optional.
  • Supports annotations applied on parameterized type, such as List<@Email String> emails.
  • Add a series of new annotations, eg. @Email,@NotEmpty, @NotBlank, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent.
Let's create a simple example to experience it.
1
public class Todo implements Serializable {
2
3
@NotNull
4
@NotBlank
5
private String name;
6
7
private String description;
8
9
@PastOrPresent
10
private LocalDateTime creationDate = LocalDateTime.now();
11
12
@Future
13
private LocalDateTime dueDate = LocalDateTime.now().plusDays(2);
14
15
public String getName() {
16
return name;
17
}
18
19
public void setName(String name) {
20
this.name = name;
21
}
22
23
public String getDescription() {
24
return description;
25
}
26
27
public void setDescription(String description) {
28
this.description = description;
29
}
30
31
public LocalDateTime getCreationDate() {
32
return creationDate;
33
}
34
35
public void setCreationDate(LocalDateTime creationDate) {
36
this.creationDate = creationDate;
37
}
38
39
public LocalDateTime getDueDate() {
40
return dueDate;
41
}
42
43
public void setDueDate(LocalDateTime dueDate) {
44
this.dueDate = dueDate;
45
}
46
47
48
}
Copied!
Create a test to verify it.
1
@RunWith(Arquillian.class)
2
public class TodoTest {
3
4
@Deployment(name = "test")
5
public static Archive<?> createDeployment() {
6
7
JavaArchive archive = ShrinkWrap.create(JavaArchive.class)
8
.addPackage(Todo.class.getPackage())
9
//.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml")
10
//.addAsResource("persons.json", "persons.json")
11
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
12
// System.out.println(archive.toString(true));
13
return archive;
14
}
15
16
@Inject
17
Logger LOG;
18
19
@Inject
20
ValidatorFactory validatorFactory;
21
22
@Inject
23
Validator validator;
24
25
@Test()
26
public void testInvalidTodo() {
27
Todo todo = new Todo();
28
assertNull(todo.getName());
29
Set<ConstraintViolation<Todo>> violations = validatorFactory.getValidator().validate(todo);
30
LOG.log(Level.INFO, "violations.size ::" + violations.size());
31
assertTrue(violations.size() > 0);
32
}
33
34
@Test()
35
public void testInvalidTodo2() {
36
Todo todo = new Todo();
37
Set<ConstraintViolation<Todo>> violations = validator.validate(todo, Default.class);
38
LOG.log(Level.INFO, "violations.size ::" + violations.size());
39
assertTrue(violations.size() > 0);
40
}
41
42
@Test()
43
public void testInvalidDueDate() {
44
Todo todo = new Todo();
45
todo.setName("test");
46
todo.setDueDate(LocalDateTime.now());
47
Set<ConstraintViolation<Todo>> violations = validator.validate(todo, Default.class);
48
LOG.log(Level.INFO, "violations.size ::" + violations.size());
49
assertTrue(violations.size() == 1);
50
}
51
52
}
Copied!
Validator or ValidatorFactory is available for injection, you can inject them into your beans as other CDI beans. validate method will return a collection of ConstraintViolation.
Grab the source codes from my GitHub account, and have a try.
Last modified 8mo ago
Copy link