Slide 1

Slide 1 text

Bean Validation 2.0 Support für Java 8 und mehr Gunnar Morling, Red Hat 1

Slide 2

Slide 2 text

Was erwartet Euch? Was ist Bean Validation? Was ist neu in Bean Validation 2.0? Feedback geben 2

Slide 3

Slide 3 text

Gunnar Morling Opensource-Softwareentwickler bei Red Hat Div. Hibernate-Projekte Spec Lead für Bean Validation 2.0 Andere Projekte: ModiTect, MapStruct [email protected] @gunnarmorling http://in.relation.to/gunnar-morling/ 3

Slide 4

Slide 4 text

Was ist Bean Validation? "Constraint once, validate everywhere" Constraints für JavaBeans Validierung per API oder automatisch ​ JPA JSF, Spring MVC, GWT JAX-RS Erweiterbar (eigene Constraints) BV 1.1: ​ Methodenvalidierung 4

Slide 5

Slide 5 text

Demo 5

Slide 6

Slide 6 text

Bean Validation 2.0 - JSR 380 Einsatz der neuen Sprachfeatures Unterstützung der API-Erweiterungen 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Wiederholbare Annotationen @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 = ...; 8

Slide 9

Slide 9 text

Validierung von Collections private List names; 9

Slide 10

Slide 10 text

Validierung von Collections private List names; @Size(max=10) private List names; 10

Slide 11

Slide 11 text

Validierung von Collections private List names; @Size(max=10) private List<@Pattern(regexp="[a-zA-Z]*") String> names; @Size(max=10) private List names; 11

Slide 12

Slide 12 text

Validierung von Collections private List names; @Size(max=10) private List<@Size(max=50) String> names; @Size(max=10) private List<@Pattern(regexp="[a-zA-Z]*") String> names; @Size(max=10) private List names; 12

Slide 13

Slide 13 text

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) @Repeatable(List.class) @Documented @Constraint(validatedBy = { }) public @interface Size { ... } Typ-Annotationen (JSR 308) Neuer ElementType TYPE_USE 13

Slide 14

Slide 14 text

Kaskadierung @Valid private List
addresses; 14

Slide 15

Slide 15 text

Kaskadierung @Valid private List
addresses; private List<@Valid Address> addresses; 15

Slide 16

Slide 16 text

Kaskadierung @Valid private List
addresses; private List<@Valid Address> addresses; private Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; 16

Slide 17

Slide 17 text

Kaskadierung @Valid private List
addresses; private List<@Valid Address> addresses; private Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; private Address @NotNull @Valid[] addresses; 17

Slide 18

Slide 18 text

java.util.Optional Neuer ElementType TYPE Auch für StringProperty et al. (JavaFX) Optional<@Email String> getEmail() { ... }; 18

Slide 19

Slide 19 text

Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon, Scala etc.) Unterstützt mittels Extractor-SPI private Table revenuePerYearAndCategory; 19

Slide 20

Slide 20 text

Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon, Scala etc.) Unterstützt mittels Extractor-SPI private Table revenuePerYearAndCategory; private Table< Year, String, @DecimalMin(value="0", inclusive=false) Integer> revenuePerYearAndCategory; 20

Slide 21

Slide 21 text

Demo 21.1

Slide 22

Slide 22 text

class TableValueExtractor implements ValueExtractor> { @Override public void extractValues(Table originalValue, ValueExtractor.ValueReceiver receiver) { for ( Cell cell : originalValue.cellSet() ) { receiver.keyedValue( "", new CellKey( cell.getRowKey(), cell.getColumnKey() ), cell.getValue() ); } } } 21.2

Slide 23

Slide 23 text

java.time API (JSR 310) @Past/@Future erlaubt für java.time.LocalDateTime, ZonedDateTime etc. @Past private Year inceptionYear = Year.of( 2017 ); 22

Slide 24

Slide 24 text

java.time API (JSR 310) @Past/@Future erlaubt für java.time.LocalDateTime, ZonedDateTime etc. @Past private Year inceptionYear = Year.of( 2017 ); @Past(orPresent=true) private Year inceptionYear = Year.of( 2017 ); 23

Slide 25

Slide 25 text

java.time API (JSR 310) ValidatorFactory vf = Validation.byDefaultProvider() .configure() .clockProvider( new FixedClockProvider( ZonedDateTime.of( 2016, 6, 15, 0, 0, 0, 0, ZoneId.of( "Europe/Paris" ) ) ) ) .buildValidatorFactory(); 24

Slide 26

Slide 26 text

Weitere Java 8 Goodies ConstraintValidator ohne initialize() public class NotNullValidator implements ConstraintValidator { public void initialize(NotNull constraintAnnotation) { } public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } public class NotNullValidator implements ConstraintValidator { public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } 25

Slide 27

Slide 27 text

Diverses Neue Constraints @NotEmpty, @NotBlank @Email @Positive, @Negative ValidatorFactory implementiert AutoCloseable try ( ValidatorFactory vf = Validation.buildDefaultValidatorFactory() ) { } 26

Slide 28

Slide 28 text

JSF 2.3 und JAX-RS 2.1 JSF: Validierung von Class-Level-Constraints JAX-RS Berücksichtigung des Request Locale Option zur Deaktivierung von Bean Validation 27

Slide 29

Slide 29 text

Status Spec: Early Draft Referenzimplementierung: Hibernate Validator 6.0 Alpha2 Feedback erwünscht :-) Public Draft: April Bestandteil von Java EE 8 (Juli 2017) 28

Slide 30

Slide 30 text

Resourcen Wie mitmachen: API, Spezifikation, TCK, Website: Referenzimplementierung: Alles ist Open Source (Apache License 2.0) beanvalidation.org/contribute/ github.com/beanvalidation/ github.com/hibernate/hibernate-validator/ 29

Slide 31

Slide 31 text

30