SlideShare a Scribd company logo
1 of 11
Download to read offline
JSRs 303 and 330 in Action
Apache Bean Validation & Google Guice


        GuiceCon, March 19th, 2011
                  Simone Tripodi
             simonetripodi@apache.org
     http://people.apache.org/~simonetripodi
Apache Bean Validation

✗ Bean Validation Specifcation (JSR303)
implementation;
✗ Efort undergoing incubation at the ASF;


✗ TCK compliant;


✗ Works on JavaSE 5 or later;


✗ Donated to the ASF from Agimatec GmbH;


✗ Released under ASL 2.0 License.
Apache Bean Validation
                  &
             Google Guice
✗ Bootstrap Apache Bean Validation using Google
Guice;
✗ Obtain javax.validation.ConstraintValidator

instances using the Google Guice Injector, to easily
support the DI;
✗ Require for javax.validation.* components

injection;
✗ Intercept methods and validate method arguments.
Bootstrapping



Guice.createInjector([...],
new org.apache.bval.guice.ValidationModule(),
[...]);

               Simple enough?
obtain
    javax.validation.ConstraintValidator
                      instances
class MyCustomValidator
        Implements ConstraintValidator<MyAssert, MyType> {

    private MyExternalService service;

    @javax.inject.Inject
    public setService(MyExternalService service) {
        this.service = service;
    }

    public void initialize(MyAssert annotation) {
        // do something
    }

    public boolean isValid(MyType value,
            ConstraintValidatorContext context) {
        return his.service.doSomething(value);
    }

}
Require for javax.validation.*
           components injection

class MyValidatorClient {

    @javax.inject.Inject
    private ValidatorFactory validatorFactory;

    public void setValidatorFactory(ValidatorFactory
           validatorFactory) {
        this.validator = validator;
    }

    ...

}
Require for javax.validation.*
           components injection

class MyValidatorClient {

    @javax.inject.Inject
    private Validator validator;

    public void setValidator(Validator validator) {
        this.validator = validator;
    }

    ...

}
Methods arguments validation
●the org.apache.bval.guice.ValidationModule comes with
an AOP interceptor automatically initialized;
●Based on @org.apache.bval.guice.Validate method
annotation
    ●   Class<?>[] groups(): the groups have to be validated,
        empty by default;
    ●   boolean validateReturnedValue(): the returned object of
        intercepted method has to be validated, false by default;
    ●   Class<? Extends Throwable> rethrowExceptionAs(): the
        exception re-thrown if a validation occurs,
        javax.validation.ConstraintViolationException by
        default;
    ●   String exceptionMessage(): a custom error message
        when throwing a custom exception.
Method arguments validation

@Validate(
    groups = { Insert.class },
    validateReturnedValue = true,
    rethrowExceptionsAs = DummyException.class,
    exceptionMessage = "Impossible adding Country{Name=%s, ISO2 Code=%s, ISO3 Code=%s}"
)
public Country insertCountry(@NotNull(groups = { Insert.class }) String name,

       @NotNull(groups = { Insert.class })
       @Size(max = 2, groups = { Insert.class, Update.class })
       String iso2Code,

       @NotNull(groups = { Insert.class })
       @Size(max = 3, groups = { Insert.class, Update.class })
       String iso3Code) {

    Country country = new Country();
    country.setName(name);
    country.setIso2Code(iso2Code);
    country.setIso3Code(iso3Code);
    ...
    return country;
}
References


●   Apache Bean Validation: http://incubator.apache.org/bval/
●   Guice integration short guide:
http://incubator.apache.org/bval/cwiki/obtaining-a-validator.html

●   Apache Bean Validation SVN:
https://svn.apache.org/repos/asf/incubator/bval/trunk
G R A Z I E :)

More Related Content

What's hot

Devoxx 2012 hibernate envers
Devoxx 2012   hibernate enversDevoxx 2012   hibernate envers
Devoxx 2012 hibernate enversRomain Linsolas
 
Learning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APILearning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APIcaswenson
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJohn Ferguson Smart Limited
 
Wicket Security Presentation
Wicket Security PresentationWicket Security Presentation
Wicket Security Presentationmrmean
 
JSR-303 Bean Validation API
JSR-303 Bean Validation APIJSR-303 Bean Validation API
JSR-303 Bean Validation APIHeiko Scherrer
 
Integration test framework
Integration test frameworkIntegration test framework
Integration test frameworkIlya Medvedev
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Taking a Test Drive
Taking a Test DriveTaking a Test Drive
Taking a Test DriveGraham Lee
 
Creating Gradle Plugins - GR8Conf US
Creating Gradle Plugins - GR8Conf USCreating Gradle Plugins - GR8Conf US
Creating Gradle Plugins - GR8Conf USAnnyce Davis
 
How to execute Automation Testing using Selenium
How to execute Automation Testing using SeleniumHow to execute Automation Testing using Selenium
How to execute Automation Testing using Seleniumvaluebound
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questionsarchana singh
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Zianed Hou
 
Unit Testing with Jest
Unit Testing with JestUnit Testing with Jest
Unit Testing with JestMaayan Glikser
 
OSGi and Eclipse RCP
OSGi and Eclipse RCPOSGi and Eclipse RCP
OSGi and Eclipse RCPEric Jain
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialAnup Singh
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Ryosuke Uchitate
 

What's hot (20)

Devoxx 2012 hibernate envers
Devoxx 2012   hibernate enversDevoxx 2012   hibernate envers
Devoxx 2012 hibernate envers
 
Learning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APILearning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security API
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
 
Wicket Security Presentation
Wicket Security PresentationWicket Security Presentation
Wicket Security Presentation
 
JSR-303 Bean Validation API
JSR-303 Bean Validation APIJSR-303 Bean Validation API
JSR-303 Bean Validation API
 
Agile Swift
Agile SwiftAgile Swift
Agile Swift
 
Agile Android
Agile AndroidAgile Android
Agile Android
 
Integration test framework
Integration test frameworkIntegration test framework
Integration test framework
 
Testing in-groovy
Testing in-groovyTesting in-groovy
Testing in-groovy
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Java Quiz - Meetup
Java Quiz - MeetupJava Quiz - Meetup
Java Quiz - Meetup
 
Taking a Test Drive
Taking a Test DriveTaking a Test Drive
Taking a Test Drive
 
Creating Gradle Plugins - GR8Conf US
Creating Gradle Plugins - GR8Conf USCreating Gradle Plugins - GR8Conf US
Creating Gradle Plugins - GR8Conf US
 
How to execute Automation Testing using Selenium
How to execute Automation Testing using SeleniumHow to execute Automation Testing using Selenium
How to execute Automation Testing using Selenium
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questions
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
 
Unit Testing with Jest
Unit Testing with JestUnit Testing with Jest
Unit Testing with Jest
 
OSGi and Eclipse RCP
OSGi and Eclipse RCPOSGi and Eclipse RCP
OSGi and Eclipse RCP
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
 

Similar to JSRs 303 and 330 in Action

Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice PresentationDmitry Buzdin
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's NewTed Pennings
 
Java EE 8 security and JSON binding API
Java EE 8 security and JSON binding APIJava EE 8 security and JSON binding API
Java EE 8 security and JSON binding APIAlex Theedom
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejugrobbiev
 
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...Java Web Application Security with Java EE, Spring Security and Apache Shiro ...
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...Matt Raible
 
Integrating Wicket with Java EE 6
Integrating Wicket with Java EE 6Integrating Wicket with Java EE 6
Integrating Wicket with Java EE 6Michael Plöd
 
Securing your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris KelloggSecuring your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris KelloggStreamNative
 
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...JAXLondon2014
 
Testing the Enterprise layers, with Arquillian
Testing the Enterprise layers, with ArquillianTesting the Enterprise layers, with Arquillian
Testing the Enterprise layers, with ArquillianVirtual JBoss User Group
 
Flexible validation with Hibernate Validator 5.x.
Flexible validation with Hibernate Validator 5.x.Flexible validation with Hibernate Validator 5.x.
Flexible validation with Hibernate Validator 5.x.IT Weekend
 
Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSJim Lynch
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotationjavatwo2011
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best PracticesJitendra Zaa
 
ASP.NET MVC 3.0 Validation
ASP.NET MVC 3.0 ValidationASP.NET MVC 3.0 Validation
ASP.NET MVC 3.0 ValidationEyal Vardi
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code EffectivelyAndres Almiray
 

Similar to JSRs 303 and 330 in Action (20)

Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice Presentation
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's New
 
Java EE 8 security and JSON binding API
Java EE 8 security and JSON binding APIJava EE 8 security and JSON binding API
Java EE 8 security and JSON binding API
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
 
Deployment
DeploymentDeployment
Deployment
 
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...Java Web Application Security with Java EE, Spring Security and Apache Shiro ...
Java Web Application Security with Java EE, Spring Security and Apache Shiro ...
 
Integrating Wicket with Java EE 6
Integrating Wicket with Java EE 6Integrating Wicket with Java EE 6
Integrating Wicket with Java EE 6
 
Securing your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris KelloggSecuring your Pulsar Cluster with Vault_Chris Kellogg
Securing your Pulsar Cluster with Vault_Chris Kellogg
 
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
 
Testing the Enterprise layers, with Arquillian
Testing the Enterprise layers, with ArquillianTesting the Enterprise layers, with Arquillian
Testing the Enterprise layers, with Arquillian
 
Javascript Unit Testing
Javascript Unit TestingJavascript Unit Testing
Javascript Unit Testing
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
Flexible validation with Hibernate Validator 5.x.
Flexible validation with Hibernate Validator 5.x.Flexible validation with Hibernate Validator 5.x.
Flexible validation with Hibernate Validator 5.x.
 
Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best Practices
 
ASP.NET MVC 3.0 Validation
ASP.NET MVC 3.0 ValidationASP.NET MVC 3.0 Validation
ASP.NET MVC 3.0 Validation
 
Guice2.0
Guice2.0Guice2.0
Guice2.0
 
Unit testing with mock libs
Unit testing with mock libsUnit testing with mock libs
Unit testing with mock libs
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
 

JSRs 303 and 330 in Action

  • 1. JSRs 303 and 330 in Action Apache Bean Validation & Google Guice GuiceCon, March 19th, 2011 Simone Tripodi simonetripodi@apache.org http://people.apache.org/~simonetripodi
  • 2. Apache Bean Validation ✗ Bean Validation Specifcation (JSR303) implementation; ✗ Efort undergoing incubation at the ASF; ✗ TCK compliant; ✗ Works on JavaSE 5 or later; ✗ Donated to the ASF from Agimatec GmbH; ✗ Released under ASL 2.0 License.
  • 3. Apache Bean Validation & Google Guice ✗ Bootstrap Apache Bean Validation using Google Guice; ✗ Obtain javax.validation.ConstraintValidator instances using the Google Guice Injector, to easily support the DI; ✗ Require for javax.validation.* components injection; ✗ Intercept methods and validate method arguments.
  • 5. obtain javax.validation.ConstraintValidator instances class MyCustomValidator Implements ConstraintValidator<MyAssert, MyType> { private MyExternalService service; @javax.inject.Inject public setService(MyExternalService service) { this.service = service; } public void initialize(MyAssert annotation) { // do something } public boolean isValid(MyType value, ConstraintValidatorContext context) { return his.service.doSomething(value); } }
  • 6. Require for javax.validation.* components injection class MyValidatorClient { @javax.inject.Inject private ValidatorFactory validatorFactory; public void setValidatorFactory(ValidatorFactory validatorFactory) { this.validator = validator; } ... }
  • 7. Require for javax.validation.* components injection class MyValidatorClient { @javax.inject.Inject private Validator validator; public void setValidator(Validator validator) { this.validator = validator; } ... }
  • 8. Methods arguments validation ●the org.apache.bval.guice.ValidationModule comes with an AOP interceptor automatically initialized; ●Based on @org.apache.bval.guice.Validate method annotation ● Class<?>[] groups(): the groups have to be validated, empty by default; ● boolean validateReturnedValue(): the returned object of intercepted method has to be validated, false by default; ● Class<? Extends Throwable> rethrowExceptionAs(): the exception re-thrown if a validation occurs, javax.validation.ConstraintViolationException by default; ● String exceptionMessage(): a custom error message when throwing a custom exception.
  • 9. Method arguments validation @Validate( groups = { Insert.class }, validateReturnedValue = true, rethrowExceptionsAs = DummyException.class, exceptionMessage = "Impossible adding Country{Name=%s, ISO2 Code=%s, ISO3 Code=%s}" ) public Country insertCountry(@NotNull(groups = { Insert.class }) String name, @NotNull(groups = { Insert.class }) @Size(max = 2, groups = { Insert.class, Update.class }) String iso2Code, @NotNull(groups = { Insert.class }) @Size(max = 3, groups = { Insert.class, Update.class }) String iso3Code) { Country country = new Country(); country.setName(name); country.setIso2Code(iso2Code); country.setIso3Code(iso3Code); ... return country; }
  • 10. References ● Apache Bean Validation: http://incubator.apache.org/bval/ ● Guice integration short guide: http://incubator.apache.org/bval/cwiki/obtaining-a-validator.html ● Apache Bean Validation SVN: https://svn.apache.org/repos/asf/incubator/bval/trunk
  • 11. G R A Z I E :)