Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Neues in Bean Validation 2.0 - Support für Java...
Search
Gunnar Morling
September 29, 2017
Programming
0
110
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
Share
More Decks by Gunnar Morling
See All by Gunnar Morling
1BRC–Nerd Sniping the Java Community
gunnarmorling
0
51
Data Contracts In Practice With Debezium and Apache Flink
gunnarmorling
1
510
1BRC – Nerd Sniping the Java Community (JavaZone)
gunnarmorling
0
43
Syncing your Database To OpenSearch In Real-Time (JCON Slovenia)
gunnarmorling
0
75
1BRC – Nerd Sniping the Java Community (Keynote JCon Slovenia)
gunnarmorling
1
320
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
450
Data Contracts In Practice With Debezium and Apache Flink (Kafka Summit London)
gunnarmorling
2
760
From Postgres to OpenSearch in No Time
gunnarmorling
1
840
Debezium Snapshots Revisited! (Current '23)
gunnarmorling
1
260
Other Decks in Programming
See All in Programming
Contemporary Test Cases
maaretp
0
140
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Jakarta EE meets AI
ivargrimstad
0
160
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
180
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
65
4.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Faster Mobile Websites
deanohume
305
30k
Typedesign – Prime Four
hannesfritz
40
2.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Rails Girls Zürich Keynote
gr2m
94
13k
Transcript
Neues in Bean Validation 2.0 Support für Java 8 und
mehr Gunnar Morling, Red Hat @gunnarmorling 1
Agenda Was ist Bean Validation? Was gibt's Neues in Bean
Validation 2.0? Fragen 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
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
Demo 5
Bean Validation 2.0 - JSR 380 Nutzung der neuen Sprachfeatures
von Java 8 Unterstützung der neuen APIs 6
Use Case: Passwortvalidierung basierend auf Nutzerrolle @Size.List({ @Size(min = 8,
group = Default.class), @Size(min = 12, group = Admin.class) }) private char[] password = ...; 7
@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
Use Case: Nicht-leere Listenelemente sicherstellen private List<String> names; 9
private List<String> names; @NotEmpty private List<String> names; Use Case: Nicht-leere
Listenelemente sicherstellen 10
private List<String> names; @OnElements(constraint=@NotEmpty) private List<String> names; Use Case: Nicht-leere
Listenelemente sicherstellen 11
private List<String> names; private List<@NotEmpty String> names; @OnElements(constraint=@NotEmpty) private List<String>
names; Use Case: Nicht-leere Listenelemente sicherstellen 12
private List<String> names; private List<@NotEmpty @Pattern(regexp="[a-zA-Z]*") String> names; @OnElements(constraint=@NotEmpty) private
List<String> names; Use Case: Nicht-leere Listenelemente sicherstellen 13
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
@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
Kaskadierende Validierung @Valid private List<Address> addresses; 16
@Valid private List<Address> addresses; private List<@Valid Address> addresses; Kaskadierende Validierung
17
@Valid private List<Address> addresses; private List<@Valid Address> addresses; private Map<@Valid
Comment, Integer> scorePerComment; Kaskadierende Validierung 18
@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
Weitere unterstützte Container Optional, OptionalInt, OptionalLong, OptionalDouble Propertytypen von JavaFX
Optional<@Email String> getEmail() { ... }; 20
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
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
Demo 23
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
@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
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
Neue Constraints @NotEmpty, @NotBlank @Email @Positive, @PositiveOrZero , @Negative, @NegativeOrZero
@PastOrPresent, @FutureOrPresent 27
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
Status Final seit August Referenzimplementierung: Hibernate Validator 6.0 29
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
Resourcen Spezifikation Referenzimplementierung API, Spezifikation, TCK, Website @gunnarmorling http://beanvalidation.org/2.0/spec/ github.com/hibernate/hibernate-validator/
github.com/beanvalidation/ 31
32
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