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
Bean Validation 2.0 - Support für Java 8 und mehr
Search
Gunnar Morling
March 28, 2017
Programming
0
200
Bean Validation 2.0 - Support für Java 8 und mehr
Learn what's coming in Bean Validation 2.0 (JSR 380); Slides from JavaLand 2017 (German)
Gunnar Morling
March 28, 2017
Tweet
Share
More Decks by Gunnar Morling
See All by Gunnar Morling
1BRC–Nerd Sniping the Java Community
gunnarmorling
0
72
Data Contracts In Practice With Debezium and Apache Flink
gunnarmorling
1
580
1BRC – Nerd Sniping the Java Community (JavaZone)
gunnarmorling
0
110
Syncing your Database To OpenSearch In Real-Time (JCON Slovenia)
gunnarmorling
0
92
1BRC – Nerd Sniping the Java Community (Keynote JCon Slovenia)
gunnarmorling
1
340
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
470
Data Contracts In Practice With Debezium and Apache Flink (Kafka Summit London)
gunnarmorling
2
810
From Postgres to OpenSearch in No Time
gunnarmorling
1
950
Debezium Snapshots Revisited! (Current '23)
gunnarmorling
1
290
Other Decks in Programming
See All in Programming
Androidアプリの One Experience リリース
nein37
0
1.1k
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
240
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.3k
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
良いユニットテストを書こう
mototakatsu
11
3.5k
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1k
『改訂新版 良いコード/悪いコードで学ぶ設計入門』活用方法−爆速でスキルアップする!効果的な学習アプローチ / effective-learning-of-good-code
minodriven
28
3.9k
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
9
2.3k
Beyond ORM
77web
11
1.6k
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.3k
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
Featured
See All Featured
Side Projects
sachag
452
42k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
KATA
mclloyd
29
14k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
860
The Pragmatic Product Professional
lauravandoore
32
6.4k
Facilitating Awesome Meetings
lara
51
6.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
Become a Pro
speakerdeck
PRO
26
5.1k
RailsConf 2023
tenderlove
29
970
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Transcript
Bean Validation 2.0 Support für Java 8 und mehr Gunnar
Morling, Red Hat 1
Was erwartet Euch? Was ist Bean Validation? Was ist neu
in Bean Validation 2.0? Feedback geben 2
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
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
Demo 5
Bean Validation 2.0 - JSR 380 Einsatz der neuen Sprachfeatures
Unterstützung der API-Erweiterungen 6
Wiederholbare Annotationen @Size.List({ @Size(min = 8, group = Default.class), @Size(min
= 12, group = Admin.class) }) private char[] password = ...; 7
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
Validierung von Collections private List<String> names; 9
Validierung von Collections private List<String> names; @Size(max=10) private List<String> names;
10
Validierung von Collections private List<String> names; @Size(max=10) private List<@Pattern(regexp="[a-zA-Z]*") String>
names; @Size(max=10) private List<String> names; 11
Validierung von Collections private List<String> 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<String> names; 12
@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
Kaskadierung @Valid private List<Address> addresses; 14
Kaskadierung @Valid private List<Address> addresses; private List<@Valid Address> addresses; 15
Kaskadierung @Valid private List<Address> addresses; private List<@Valid Address> addresses; private
Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; 16
Kaskadierung @Valid private List<Address> addresses; private List<@Valid Address> addresses; private
Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; private Address @NotNull @Valid[] addresses; 17
java.util.Optional Neuer ElementType TYPE Auch für StringProperty et al. (JavaFX)
Optional<@Email String> getEmail() { ... }; 18
Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon,
Scala etc.) Unterstützt mittels Extractor-SPI private Table<Year, String, Integer> revenuePerYearAndCategory; 19
Eigene Container Spezifische Kollektionstypen (z.B. Google Guava) Andere JVM-Sprachen (Ceylon,
Scala etc.) Unterstützt mittels Extractor-SPI private Table<Year, String, Integer> revenuePerYearAndCategory; private Table< Year, String, @DecimalMin(value="0", inclusive=false) Integer> revenuePerYearAndCategory; 20
Demo 21.1
class TableValueExtractor implements ValueExtractor<Table<?, ?, @ExtractedValue ?>> { @Override public
void extractValues(Table<?, ?, ?> originalValue, ValueExtractor.ValueReceiver receiver) { for ( Cell<?, ?, ?> cell : originalValue.cellSet() ) { receiver.keyedValue( "<table cell>", new CellKey( cell.getRowKey(), cell.getColumnKey() ), cell.getValue() ); } } } 21.2
java.time API (JSR 310) @Past/@Future erlaubt für java.time.LocalDateTime, ZonedDateTime etc.
@Past private Year inceptionYear = Year.of( 2017 ); 22
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
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
Weitere Java 8 Goodies 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; } } 25
Diverses Neue Constraints @NotEmpty, @NotBlank @Email @Positive, @Negative ValidatorFactory implementiert
AutoCloseable try ( ValidatorFactory vf = Validation.buildDefaultValidatorFactory() ) { } 26
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
Status Spec: Early Draft Referenzimplementierung: Hibernate Validator 6.0 Alpha2 Feedback
erwünscht :-) Public Draft: April Bestandteil von Java EE 8 (Juli 2017) 28
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
30