bean validation 1 1 what s cooking
play

Bean Validation 1.1 What's cooking? 05.04.2013 Gunnar Morling - PowerPoint PPT Presentation

Bean Validation 1.1 What's cooking? 05.04.2013 Gunnar Morling JBoss, by Red Hat Bean Validation 1.1 JSR 349 Final Approval Ballot nchste Woche! Vollstndig offen Issue-Tracker Mailingliste GitHub:


  1. Bean Validation 1.1 – What's cooking? 05.04.2013 Gunnar Morling – JBoss, by Red Hat

  2. Bean Validation 1.1 • JSR 349 – Final Approval Ballot nächste Woche! • Vollständig offen – Issue-Tracker – Mailingliste – GitHub: • Spezifikation • Referenzimplemen- tierung • TCK • Website 05.04.2013 Bean Validation 1.1 – What's cooking? 2 /13

  3. Dependency Injection • Dependency Injection via CDI – ConstraintValidator-Implementierungen – public class public class PastValidator PastValidator implements implements ConstraintValidator< ConstraintValidator<Past Past, Date> { , Date> { – @Inject @Inject – private private TimeService TimeService timeService timeService; – public public void void initialize( initialize(Past Past constraintAnnotation) { constraintAnnotation) { } – public public boolean boolean isValid(Date date, ConstraintValidatorContext context) { isValid(Date date, ConstraintValidatorContext context) { – if if ( date == ( date == null null ) { ) { return return true true; – } – return return date.before( date.before( timeService timeService.getCurrentTime() ); .getCurrentTime() ); } – } – MessageInterpolator, TraversableResolver, ConstraintValidatorFactory etc. 05.04.2013 Bean Validation 1.1 – What's cooking? 3 /13

  4. Methodenvalidierung (I) • Validierung von Methodenparametern und -rückgabewerten beim Aufruf • Erfordert Interceptor, AOP, Proxy etc. 05.04.2013 Bean Validation 1.1 – What's cooking? 4 /13

  5. Methodenvalidierung (II) • Parameterprüfung – “old school” • /** /** * Places an order. * Places an order. * • * * @param @param customerCode Must not be null and between 3 and 20 characters. customerCode Must not be null and between 3 and 20 characters. * * @param @param item Must not be null. item Must not be null. • * * @param @param quantity Must be larger or equal than 1. quantity Must be larger or equal than 1. */ */ public void public void placeOrder(String customerCode, Item item, placeOrder(String customerCode, Item item, int int quantity) { quantity) { • validateCustomerCode(customerCode); validateCustomerCode(customerCode); if if(item == (item == null null) ) throw throw new new IllegalArgumentException(); IllegalArgumentException(); • if if(quantity < 1) (quantity < 1) throw throw new new IllegalArgumentException(); IllegalArgumentException(); //Actual business logic... //Actual business logic... • } • Nachteile – Manuelle Prüfung – Redundanz – Schnell uneinheitlich 05.04.2013 Bean Validation 1.1 – What's cooking? 5 /13

  6. Methodenvalidierung (III) • Parameterprüfung – “Bean Validation way” • public public void void placeOrder( placeOrder( @NotNull @NotNull @Size @Size(min=3, max=20) String customerCode, (min=3, max=20) String customerCode, @NotNull @NotNull Item item, Item item, • @Min @Min(1) (1) int int quantity) { quantity) { • //... //... } • Vorteile – Validierung einheitlich als Aspekt implementiert – Constraints Teil der JavaDoc – “Programming by Contract” • Aktiv per Default für CDI Beans und JAX-RS Resourcen 05.04.2013 Bean Validation 1.1 – What's cooking? 6 /13

  7. Methodenvalidierung (IV) • Objektgraphen • public public void void placeOrder( placeOrder( @NotNull @NotNull @Size @Size(min=3, max=20) String customerCode, (min=3, max=20) String customerCode, @NotNull @NotNull @Valid @Valid Item item, Item item, • @Min @Min(1) (1) int int quantity) { quantity) { • //... //... } 05.04.2013 Bean Validation 1.1 – What's cooking? 7 /13

  8. Methodenvalidierung (IV) • Objektgraphen • public public void void placeOrder( placeOrder( @NotNull @NotNull @Size @Size(min=3, max=20) String customerCode, (min=3, max=20) String customerCode, @NotNull @NotNull @Valid @Valid Item item, Item item, • @Min @Min(1) (1) int int quantity) { quantity) { • //... //... } • Rückgabewerte @NotNull @NotNull @RetailOrder @RetailOrder @Valid @Valid public Order placeOrder( public Order placeOrder( @NotNull @NotNull @Size @Size(min=3, max=20) String customerCode, (min=3, max=20) String customerCode, @NotNull @NotNull @Valid Item item, @Valid Item item, @Min @Min(1) (1) int int quantity) { quantity) { //... //... } 05.04.2013 Bean Validation 1.1 – What's cooking? 7 /13

  9. Methodenvalidierung (V) • Konstruktoren • @Valid @Valid public OrderService( public OrderService(@NotNull @NotNull @Valid @Valid OrderDao orderDao) { OrderDao orderDao) { //... //... • } 05.04.2013 Bean Validation 1.1 – What's cooking? 8 /13

  10. Methodenvalidierung (V) • Konstruktoren • @Valid @Valid public OrderService( public OrderService(@NotNull @NotNull @Valid @Valid OrderDao orderDao) { OrderDao orderDao) { //... //... • } • Cross-parameter Constraints @PasswordsMatch @PasswordsMatch public void public void resetPassword( resetPassword(@NotNull @NotNull password, password, @NotNull @NotNull passwordConfirmation) { passwordConfirmation) { ... ... } @SupportedValidationTarget @SupportedValidationTarget(value = ValidationTarget. (value = ValidationTarget. PARAMETERS PARAMETERS ) public public class class PasswordsMatchValidator PasswordsMatchValidator implements implements ConstraintValidator< ConstraintValidator<PasswordsMatch PasswordsMatch, Object[]> { , Object[]> { @Override @Override public public void void initialize( initialize(PasswordsMatch PasswordsMatch constraintAnnotation) { constraintAnnotation) { } @Override @Override public public boolean boolean isValid(Object[] value, ConstraintValidatorContext context) { isValid(Object[] value, ConstraintValidatorContext context) { return return value[0].equals(value[1]); value[0].equals(value[1]); } } 05.04.2013 Bean Validation 1.1 – What's cooking? 8 /13

  11. EL-Ausdrücke in Fehlermeldungen (I) • Dynamische Fehlermeldungen mittels Unified EL (JSR 341) • public public class class Foo { Foo { @DecimalMin @DecimalMin(value= (value="10.0" "10.0", inclusive= , inclusive=true true) • private private BigDecimal BigDecimal number number; } • ValidationMessages.properties: javax.validation.constraints.DecimalMax.message = javax.validation.constraints.DecimalMax.message = must must be be less less than than ${inclusive ${inclusive == == true true ? 'or 'or equal equal to to ' : ''}{value} ''}{value} 05.04.2013 Bean Validation 1.1 – What's cooking? 9 /13

  12. EL-Ausdrücke in Fehlermeldungen (II) • Ausgabe des validierten Werts • public public class class Shop { Shop { @ValidUser @ValidUser( • minAge=18, minAge=18, message= message="User must at least be ${minAge}, but is ${validatedValue.age}." "User must at least be ${minAge}, but is ${validatedValue.age}." • ) private private User User user user; • //... //... } 05.04.2013 Bean Validation 1.1 – What's cooking? 10 /13

  13. EL-Ausdrücke in Fehlermeldungen (II) • Ausgabe des validierten Werts • public public class class Shop { Shop { @ValidUser @ValidUser( • minAge=18, minAge=18, message= message="User must at least be ${minAge}, but is ${validatedValue.age}." "User must at least be ${minAge}, but is ${validatedValue.age}." • ) private private User User user user; • //... //... } • Formatierung mit Formatter-Object public public class class Bar { Bar { @Min @Min( value=100, value=100, message= message="Value ${formatter.format('%1$.2f', validatedValue)} is too small" "Value ${formatter.format('%1$.2f', validatedValue)} is too small" ) private private double double number number = 98.12345678d; = 98.12345678d; // ==> "Value 98.12 is too small" // ==> "Value 98.12 is too small" } 05.04.2013 Bean Validation 1.1 – What's cooking? 10 /13

  14. Gruppenkonvertierung (I) • @Valid-Annotation zur Validierung von Objektgraphen • public public class class Address { Address { @NotEmpty @NotEmpty(message= message="Street must not be empty "Street must not be empty") ") • private private String String street street; } • public class public class Customer { Customer { • @Valid @Valid private private final final Address Address address address = = new new Address(); Address(); • } 05.04.2013 Bean Validation 1.1 – What's cooking? 11 /13

  15. Gruppenkonvertierung (I) • @Valid-Annotation zur Validierung von Objektgraphen • public public class class Address { Address { @NotEmpty @NotEmpty(message= message="Street must not be empty "Street must not be empty") ") • private private String String street street; } • public public class class Customer { Customer { • @Valid @Valid private private final final Address Address address address = = new new Address(); Address(); • } • Fehlertext in Abhängigkeit der Rolle eines Objekts? public public class class Customer { Customer { @Valid @Valid private private final final Address Address homeAddress homeAddress = = new new Address(); Address(); @Valid @Valid private private final final Address Address officeAddress officeAddress = = new new Address(); Address(); } 05.04.2013 Bean Validation 1.1 – What's cooking? 11 /13

Recommend


More recommend