What’s New with Bean Validation in Java EE 7

10,450 views
10,020 views

Published on

This fast-faced, code-centric lightning talk covers the changes in Bean Validation 1.1.

Part of Java EE 7, Bean Validation includes a number of important changes such as better alignment with CDI, integration with JAX-RS 2, method validation, custom validation messages using Expression Language (EL) 3, and much, much more.

Published in: Technology

What’s New with Bean Validation in Java EE 7

  1. 1. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1 What’s New with Bean Validation in Java EE 7 Reza Rahman Java EE/GlassFish Evangelist Reza.Rahman@Oracle.com @reza_rahman
  2. 2. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2 Bean Validation 1.1  Point release update to Java EE 6  Small but important set of changes aligned with the platform  Integration with CDI, JAX-RS, JPA, JSF, etc Overview
  3. 3. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3 Bean Validation 1.1  CDI alignment  Method validation  Integration with JAX-RS  Error messages using EL expressions Feature Summary
  4. 4. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4 Bean Validation 1.1  Delegate Bean Validation component lifecycle to CDI  Particularly useful in ConstraintValidator implementations CDI Alignment public class ZipCodeValidator implements ConstraintValidator<ZipCode, String> { @Inject @France private ZipCodeChecker checker; public void initialize(ZipCode zipCode) {} public boolean isValid(String value, ConstraintValidationContext context) { if (value==null) return true; return checker.isZipCodeValid(zipCode); } }
  5. 5. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5 Bean Validation 1.1  Validate parameters and return values upon invocation – Methods and constructors  Ideal for business methods Method Validation Overview
  6. 6. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6 Bean Validation 1.1 Method Validation Example @RequestScope public class Client { @Inject AccountService service; public void createClient() { service.createUser(...); } } @ApplicationScoped public class AccountService { @NotNull public User createUser( @NotEmpty String username, String firstname, String lastname, @Valid Address home, @NotNull @Email String email, @Past Date birthDate) { // parameters are validated and an exception // is raised upon failure // business } }
  7. 7. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7 Bean Validation 1.1  Offer resource validation – Using Bean Validation constraints  Exactly like method validation Integration with JAX-RS @Path(“/user”) public class UserResource { @Post @Consumes("application/x-www-form-urlencoded") public void register( @NotEmpty @FormParam(“firstname”) String firstname, @NotEmpty @FormParam(“lastname”) String lastname, @NotNull @Email @FormParam(“email”) String email) { ... } // Works on fields and getters too }
  8. 8. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.8 Bean Validation 1.1  Ability to use EL inside error messages  Access to constraint parameters and invalid value  Offer localized string formatting too Error messages using Expression Language javax.validation.constraints.DecimalMax.message= must be less than ${inclusive == true ? 'or equal to ' : ''}{value}
  9. 9. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.9 Summary  BV 1.1 brings important changes  Both from a Java EE 7 and ecosystem perspective  Try the APIs out, provide your feedback and get involved
  10. 10. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10 Resources  Bean Validation project page – http://beanvalidation.org  Glassfish 4 – https://glassfish.java.net/
  11. 11. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11

×