JSRs 303 and 330 in Action

11
GuiceCon, March 19th, 2011 Simone Tripodi [email protected] http://people.apache.org/~simonetripodi JSRs 303 and 330 in Action Apache Bean Validation & Google Guice

description

Apache Bean Validation & Google Guice

Transcript of JSRs 303 and 330 in Action

Page 1: JSRs 303 and 330 in Action

GuiceCon, March 19th, 2011

Simone [email protected]

http://people.apache.org/~simonetripodi

JSRs 303 and 330 in ActionApache Bean Validation & Google Guice

Page 2: JSRs 303 and 330 in Action

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.

Page 3: JSRs 303 and 330 in Action

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.

Page 4: JSRs 303 and 330 in Action

Bootstrapping

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

Simple enough?

Page 5: JSRs 303 and 330 in Action

obtain javax.validation.ConstraintValidator

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

}

Page 6: JSRs 303 and 330 in Action

Require for javax.validation.*components injection

class MyValidatorClient {

@javax.inject.Inject private ValidatorFactory validatorFactory;

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

...

}

Page 7: JSRs 303 and 330 in Action

Require for javax.validation.*components injection

class MyValidatorClient {

@javax.inject.Inject private Validator validator;

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

...

}

Page 8: JSRs 303 and 330 in Action

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.

Page 9: JSRs 303 and 330 in Action

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

Page 10: JSRs 303 and 330 in Action

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

Page 11: JSRs 303 and 330 in Action

G R A Z I E :)