Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Bean Validation 2.0 - Support für Java 8 und mehr
Gunnar Morling
March 28, 2017
Programming
0
150
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
Keep Your Cache Always Fresh With Debezium
gunnarmorling
0
350
Open-source Change Data Capture With Debezium
gunnarmorling
0
130
Continuous Performance Regression Testing with JfrUnit (JChampionsConf)
gunnarmorling
1
63
Continuous Performance Regression Testing with JfrUnit (P99Conf)
gunnarmorling
1
50
To the Moon and Beyond With Java 17 APIs!
gunnarmorling
0
180
Debezium: What's new (KafkaSummit 2021)
gunnarmorling
1
44
Continuous Performance Regression Testing With JfrUnit
gunnarmorling
0
160
Don’t Fear Outdated Caches - Change Data Capture to the Rescue
gunnarmorling
1
93
Change Data Capture Pipelines With Debezium and Kafka Streams (JokerConf)
gunnarmorling
0
500
Other Decks in Programming
See All in Programming
Rust、何もわからない...#3
estie
0
170
ExplainableAIの概要とAmazon SageMaker Clarifyでの実装例
hacarus
0
100
Edge Side Frontend という新領域
mizchi
24
10k
話題の AlloyDB は本当に凄いデータベースなのでプレビューを使い倒した #devio2022
maroon1st
0
13k
2022年のモダンCSS改
tonkotsuboy_com
24
17k
YATA: collaborative documents and how to make them fast
horusiath
1
170
ECサイトの脆弱性診断をいい感じにやりたい/OWASPKansaiNight_LT1_220727
owaspkansai
0
300
段階的な技術的負債の解消方法.pdf
ko2ic
2
940
Regular expressions basics/正規表現の基本
kishikawakatsumi
6
260
閱讀原始碼 - 再戰十年的 jQuery
eddie
1
300
Recap CDN, Edge, WebAssembly | ワインと鍋.js#1
sadnessojisan
2
1.2k
僕が便利だと感じる Snow Monkey の特徴/20220723_Gifu_WordPress_Meetup
oleindesign
0
110
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
63
7.6k
GitHub's CSS Performance
jonrohan
1020
420k
The Art of Programming - Codeland 2020
erikaheidi
32
11k
Visualization
eitanlees
125
12k
Automating Front-end Workflow
addyosmani
1351
200k
JazzCon 2018 Closing Keynote - Leadership for the Reluctant Leader
reverentgeek
173
8.6k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
181
15k
A better future with KSS
kneath
226
16k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
A Modern Web Designer's Workflow
chriscoyier
689
180k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1.1M
ReactJS: Keep Simple. Everything can be a component!
pedronauck
655
120k
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 gunnar@hibernate.org @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