News Ticker

Bean Validation 2.0: 100DaysOfJavaEE8

Bean Validation Constraint Challenge

The Bean Validation 2.0 API introduces new constraint. Which of the following are among those new constraints?


The new constraints are @NotBlank, @Email, @NegativeOrZero and @PastOrPresent.

Bean Validation 2.0

Bean Validation 2.0 includes a range of new features. Among them are constraints that validate email addresses, ensure that numbers are positive or negative, test if dates are past or present, and test that fields are not empty or null. These are @Email@Positive@PositiveOrZero@Negative@NegativeOrZero@PastOrPresent@FutureOrPresent@NotEmpty, and @NotBlank.

How to Use These Constraints

Constraints act in a wider range of locations than in previous API releases. They can proceed arguments of parameterized types and support for validating container elements by type arguments has been added as in the following code snippet.

List<@Size(min = 30) String> favourites;

Other Bean Validation Enhancements

Cascaded Validation of Containers

Cascaded validation of containers has been added. If you annotate any type argument of a container with @Valid it will cause the validation of every element in that container when the parent object is validated. In the code snippet below, every Driver and Car element will be validated as shown:

Map<@Valid Driver, @Valid Car> driverAndCar;

Custom Container Types

Bean Validation also adds support for custom container types by plugging in value extractors. Built-in constraints are marked as repeatable, parameter names are retrieved using reflection, andConstraintValidator#initialize() is a default method. JavaFX also gets support for its types.

Java SE 8 Uplift

The API gets an uplift to Java SE 8’s Date and Time types and offers support for java.util.Optional.

Further Reading

For further information on how to use the new JSON Binding API take a look at my new book Java EE 8: Only What’s New.

GitHub Repository

The code from this and all other #100DaysOfJavaEE8 can be found in my GitHub repository.


1 Trackback / Pingback

  1. #100DaysOfJavaEE8: Weekly Round Up

Leave a Reply