What’s New with Bean Validation in Java EE 7

11
What’s New with Bean Validation in Java EE 7 Reza Rahman Java EE/GlassFish Evangelist [email protected] @reza_rahman

description

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.

Transcript of What’s New with Bean Validation in Java EE 7

Page 1: What’s New with Bean Validation in Java EE 7

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1

What’s New with Bean Validation in Java EE 7Reza RahmanJava EE/GlassFish [email protected]@reza_rahman

Page 2: What’s New with Bean Validation in Java EE 7

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

Page 3: What’s New with Bean Validation in Java EE 7

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

Page 4: What’s New with Bean Validation in Java EE 7

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); }}

Page 5: What’s New with Bean Validation in Java EE 7

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

Page 6: What’s New with Bean Validation in Java EE 7

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6

Bean Validation 1.1Method 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 }}

Page 7: What’s New with Bean Validation in Java EE 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}

Page 8: What’s New with Bean Validation in Java EE 7

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}

Page 9: What’s New with Bean Validation in Java EE 7

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

Page 10: What’s New with Bean Validation in Java EE 7

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/

Page 11: What’s New with Bean Validation in Java EE 7

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11