Your SlideShare is downloading. ×
What’s New with Bean Validation in Java EE 7
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

What’s New with Bean Validation in Java EE 7

7,212
views

Published on

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

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

0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,212
On Slideshare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
57
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11