Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Neues in Bean Validation 2.0 - Support für Java...

Neues in Bean Validation 2.0 - Support für Java 8 und mehr

Slides from a talk on the new features in Bean Validation 2.0, given at code.talks 2017 (German).

Gunnar Morling

September 29, 2017
Tweet

More Decks by Gunnar Morling

Other Decks in Programming

Transcript

  1. Neues in Bean Validation 2.0 Support für Java 8 und

    mehr Gunnar Morling, Red Hat @gunnarmorling 1
  2. Gunnar Morling Opensource-Softwareentwickler bei Red Hat Debezium Hibernate Spec Lead

    für Bean Validation 2.0 Andere Projekte: ModiTect, MapStruct [email protected] @gunnarmorling http://in.relation.to/gunnar-morling/ 3
  3. Was ist Bean Validation? "Constrain once, validate everywhere" Constraints für

    JavaBeans Validierung per API oder automatisch ​ JPA JSF, Spring MVC, GWT JAX-RS Erweiterbar BV 1.1: ​ Methodenvalidierung 4
  4. Bean Validation 2.0 - JSR 380 Nutzung der neuen Sprachfeatures

    von Java 8 Unterstützung der neuen APIs 6
  5. Use Case: Passwortvalidierung basierend auf Nutzerrolle @Size.List({ @Size(min = 8,

    group = Default.class), @Size(min = 12, group = Admin.class) }) private char[] password = ...; 7
  6. @Size.List({ @Size(min = 8, group = Default.class), @Size(min = 12,

    group = Admin.class) }) private char[] password = ...; @Size(min = 8, group = Default.class) @Size(min = 12, group = Admin.class) private char[] password = ...; Use Case: Passwortvalidierung basierend auf Nutzerrolle 8
  7. private List<@NotEmpty @Pattern(regexp="[a-zA-Z]*") String> names; private List<String> names; @NotEmpty private

    List<@NotEmpty String> names; @OnElements(constraint=@NotEmpty) private List<String> names; Use Case: Nicht-leere Listenelemente sicherstellen 14
  8. @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) @Repeatable(List.class)

    @Documented @Constraint(validatedBy = { }) public @interface Size { ... } Typannotationen (JSR 308) Neuer Elementtyp TYPE_USE 15
  9. @Valid private List<Address> addresses; private List<@Valid Address> addresses; private Map<@Valid

    Comment, Integer> scorePerComment; Kaskadierende Validierung 18
  10. @Valid private List<Address> addresses; private List<@Valid Address> addresses; private Map<@Valid

    Comment, Integer> scorePerComment; private Map<@Valid AddressType, List<@Valid Address>> addressesByType; Kaskadierende Validierung 19
  11. Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon,

    Scala etc.) Unterstützt per Extractor SPI private Table<Year, String, Integer> revenuePerYearAndCategory; 21
  12. private Table<Year, String, Integer> revenuePerYearAndCategory; private Table<Year, String, @Positive Integer>

    revenuePerYearAndCategory; Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon, Scala etc.) Unterstützt per Extractor SPI 22
  13. Use Case: Lieferdatum in der Zukunft @Past/@Future unterstützen JSR-310-Typen: java.time.LocalDateTime,

    ZonedDateTime etc. @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); 24
  14. @FutureOrPresent private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 );

    @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); Use Case: Lieferdatum in der Zukunft @Past/@Future unterstützen JSR-310-Typen: java.time.LocalDateTime, ZonedDateTime etc. 25
  15. Use Case: Testen ValidatorFactory vf = Validation.byDefaultProvider() .configure() .clockProvider( new

    FixedClockProvider( ZonedDateTime.of( 2016, 6, 15, 0, 0, 0, 0, ZoneId.of( "Europe/Paris" ) ) ) ) .buildValidatorFactory(); 26
  16. Andere Java 8 Goodies Parameternamen in Fehlermeldungen ConstraintValidator ohne initialize()

    public class NotNullValidator implements ConstraintValidator<NotNull, Object> { public void initialize(NotNull constraintAnnotation) {} public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } public class NotNullValidator implements ConstraintValidator<NotNull, Object> { public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } 28
  17. Finale Version Teil von Java EE 8 Enthalten in GlassFish

    5; Patch für WildFly Unterstützt durch Spring 5 Alles ist Open Source: Spezifikation, API, TCK und RI 30
  18. 32

  19. Ausblick Bean Validation 2.1 Constraint-API mapping.constraintDefinition( FileExists.class ) .validateType( File.class

    ) .with( f -> f.exists() ); Constraint-API: Lambda ConstraintMapping mapping = ...; mapping.type( Marathon.class ) .property( "numberOfHelpers", FIELD ) .constraint( new MinDef().value( 1 ) ) .property( "runners", METHOD ) .valid(); 33