Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page

Was this helpful?

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.

public class Todo implements Serializable {

    @NotNull
    @NotBlank
    private String name;

    private String description;

    @PastOrPresent
    private LocalDateTime creationDate = LocalDateTime.now();

    @Future
    private LocalDateTime dueDate = LocalDateTime.now().plusDays(2);

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(LocalDateTime creationDate) {
        this.creationDate = creationDate;
    }

    public LocalDateTime getDueDate() {
        return dueDate;
    }

    public void setDueDate(LocalDateTime dueDate) {
        this.dueDate = dueDate;
    }


}

Create a test to verify it.

@RunWith(Arquillian.class)
public class TodoTest {

    @Deployment(name = "test")
    public static Archive<?> createDeployment() {

        JavaArchive archive = ShrinkWrap.create(JavaArchive.class)
                .addPackage(Todo.class.getPackage())
                //.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml")
                //.addAsResource("persons.json", "persons.json")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        // System.out.println(archive.toString(true));
        return archive;
    }

    @Inject
    Logger LOG;

    @Inject
    ValidatorFactory validatorFactory;

    @Inject
    Validator validator;

    @Test()
    public void testInvalidTodo() {
        Todo todo = new Todo();
        assertNull(todo.getName());
        Set<ConstraintViolation<Todo>> violations = validatorFactory.getValidator().validate(todo);
        LOG.log(Level.INFO, "violations.size ::" + violations.size());
        assertTrue(violations.size() > 0);
    }

    @Test()
    public void testInvalidTodo2() {
        Todo todo = new Todo();
        Set<ConstraintViolation<Todo>> violations = validator.validate(todo, Default.class);
        LOG.log(Level.INFO, "violations.size ::" + violations.size());
        assertTrue(violations.size() > 0);
    }

    @Test()
    public void testInvalidDueDate() {
        Todo todo = new Todo();
        todo.setName("test");
        todo.setDueDate(LocalDateTime.now());
        Set<ConstraintViolation<Todo>> violations = validator.validate(todo, Default.class);
        LOG.log(Level.INFO, "violations.size ::" + violations.size());
        assertTrue(violations.size() == 1);
    }

}

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.

PreviousJSON-P 1.1NextJAXRS 2.1

Last updated 4 years ago

Was this helpful?

Grab the from my GitHub account, and have a try.

source codes