SlideShare a Scribd company logo
1 of 45
Download to read offline
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
1
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
▸
2
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸ 

▸
▸
3
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
4
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
▸
▸ 💖
5
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
6
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
7
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
8
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸ 

▸
9
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
10
public class HelloDto {
@NotBlank(message = "{hello.notblank}")
private String message;
// getter/setter
}
@Path("/hello")
public class HelloResource {
@POST
public Response post(@Valid HelloDto helloDto) {
//
}
}


(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
11
hello.notblank	=	
▸
▸
hello.notblank = Message is required
hello.notblank =
ValidationMessages_ja.properties
ValidationMessages_en.properties
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
12
$	curl	-v	-X	POST		
		-H	"Content-Type:	application/json”		
		-d	‘{"message":""}'		
		http://localhost:8080/api/hello			
>	POST	/api/hello	HTTP/1.1	
>	…	
>	
<	HTTP/1.1	400	Bad	Request	
<	…	
<	
{	
		"errorType":" ",	
		"messages":[" "]	
}
※JSON ExceptionMapper
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
13
Bean Validation 

(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
14
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ 👏
▸
15
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
▸
▸
16
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
17
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ @NotNull	
▸ @Null	
▸ @NotEmpty	
▸ @NotBlank	
▸ @AssertTrue	
▸ @AssertFalse	
▸ @Size	
▸ @Pattern
18
▸ @Email	
▸ @Digits	
▸ @DecimalMax	
▸ @DecimalMin	
▸ @Max	
▸ @Min	
▸ @Positive	
▸ @PositiveOrZero
▸ @Negative	
▸ @NegativeOrZero	
▸ @Past	
▸ @PastOrPresent	
▸ @Future	
▸ @FutureOrPresent
※
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
19
@NotBlank CharSequence
null 	
❌
@NotEmpty


null 0 ❌
@Email CharSequence
❌
@Positive ❌
@PositiveOrZero 0 ❌
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
20
@Negative ❌
@NegativeOrZero 0 ❌
@PastOrPresent
Date Calendar 	
java.time.*
	
❌
@FutureOrPresent
Date Calendar 	
java.time.*
	
❌
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ @NotBlank @NotEmpty @Email 👏
▸ 

▸ 👏
▸ @Past @PastOrPresent @Future
@FutureOrPresent
21
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ @Past 

👏
▸ @Max/@Positive 

javax.money.MonetaryAmount
👏
▸ 

22
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
23
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
24
public class Sample {
private Optional<@Size(min = 3) String> optionalString;
private List<@NotNull String> list = new ArrayList<>();
		//	omitted	
}
▸ 👏
▸
※JSR Container element constraints
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ OptionalXxx java.util.OptionalInt 

XxxProperty IntegerProperty 

👏
25
public class Sample {
@Max(10)
private OptionalInt optionalInt;
		//	omitted	
}
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
26
@Test
public void test() {
Sample sample = new Sample();
sample.addToOptionalString(“aa"); // 3
sample.addToList(null); // not null
sample.addToOptionalInt(20); // 10
//
Set<ConstraintViolation<Sample>> violations =
validator.validate(sample);
//
assertEquals(3, violations.size());
}
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
27
@Test
public void test2() {
Sample sample = new Sample();
sample.addToList(null); // not null 1
sample.addToList(null); // not null 2
//
Set<ConstraintViolation<Sample>> violations =
validator.validate(sample);
//
assertEquals(2, violations.size());
}
▸
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
28
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
@Max(value = 100, groups = Group1.class)
@Max(value = 200, groups = Group2.class)
int	someValue;
▸
▸ @Xxx.List 👏
29
@Max.List({
@Max(value = 100, groups = Group1.class),
@Max(value = 200, groups = Group2.class)
})
int	someValue;
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
ParameterNameProvider
👏
▸ -parameters
30
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ ConstraintValidator
initialize()
▸
31
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
32
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
👏
33
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
34
<web-app …>
<context-param>
<param-name>
javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN
</param-name>
<param-value>true</param-value>
</context-param>
<!-- omitted -->
</web-app>




(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
35
public class EqualsValidator
implements ConstraintValidator<Equals, Object> {
@Override
public boolean isValid(Object obj,
ConstraintValidatorContext context) {
// 2
// true
}
@Constraint(validatedBy = EqualsValidator.class)
public @interface Equals {
String property1(); // 1
String property2(); // 2
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
36
@Named
@ViewScoped
@Equals(property1 = "email1", property2 = "email2",
message = “…”)
public class CompareBean implements Serializable {
@NotBlank(message = “…”)
@Size(min = 3, message = “…”)
private String email1;
@NotBlank(message = “…”)
@Size(min = 3, message = “…”)
private String email2;
				//	omitted




(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
37
<h:form id="email-form">
<h:inputText value="#{compareBean.email1}"/><br/>
<h:inputText value="#{compareBean.email2}"/><br/>
<h:commandButton value=“ "
action="#{compareBean.submit()}"/>
<f:validateWholeBean value="#{compareBean}"/>
</h:form>
Managed Bean
※<f:validateBean/> validationGroups
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ 



▸ 

38
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ <f:validateWholeBean> 

▸
39※ h:commandButton
<h:form id="email-form">
<h:inputText value=“…”/><br/>
<h:inputText value=“…”/><br/>
<h:commandButton value=“ " action=“…”/>
<f:validateWholeBean value="#{compareBean}"/>
</h:form>
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
40
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸ @NotBlank @NotEmpty @Email
▸ Optional
▸
41
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
▸
42
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸ 😆
▸
😆
▸
😭
43
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
▸
▸
▸
▸
▸
▸
▸
44
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
▸
45

More Related Content

What's hot

金魚本に載ってないJpqlの話 #glassfishjp
金魚本に載ってないJpqlの話 #glassfishjp金魚本に載ってないJpqlの話 #glassfishjp
金魚本に載ってないJpqlの話 #glassfishjp
Satoshi Kubo
 
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
y torazuka
 

What's hot (20)

Spring Data JPAによるデータアクセス徹底入門 #jsug
Spring Data JPAによるデータアクセス徹底入門 #jsugSpring Data JPAによるデータアクセス徹底入門 #jsug
Spring Data JPAによるデータアクセス徹底入門 #jsug
 
オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)
オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)
オススメのJavaログ管理手法 ~コンテナ編~(Open Source Conference 2022 Online/Spring 発表資料)
 
単なるキャッシュじゃないよ!?infinispanの紹介
単なるキャッシュじゃないよ!?infinispanの紹介単なるキャッシュじゃないよ!?infinispanの紹介
単なるキャッシュじゃないよ!?infinispanの紹介
 
金魚本に載ってないJpqlの話 #glassfishjp
金魚本に載ってないJpqlの話 #glassfishjp金魚本に載ってないJpqlの話 #glassfishjp
金魚本に載ってないJpqlの話 #glassfishjp
 
Pivotal認定講師によるSpring Framework 5.1 & Spring Boot 2.1ハンズオン! #jjug_ccc
Pivotal認定講師によるSpring Framework 5.1 & Spring Boot 2.1ハンズオン! #jjug_cccPivotal認定講師によるSpring Framework 5.1 & Spring Boot 2.1ハンズオン! #jjug_ccc
Pivotal認定講師によるSpring Framework 5.1 & Spring Boot 2.1ハンズオン! #jjug_ccc
 
Spring Bootの本当の理解ポイント #jjug
Spring Bootの本当の理解ポイント #jjugSpring Bootの本当の理解ポイント #jjug
Spring Bootの本当の理解ポイント #jjug
 
はまる!JPA(初学者向けライト版)
はまる!JPA(初学者向けライト版)はまる!JPA(初学者向けライト版)
はまる!JPA(初学者向けライト版)
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
 
決済サービスのSpring Bootのバージョンを2系に上げた話
決済サービスのSpring Bootのバージョンを2系に上げた話決済サービスのSpring Bootのバージョンを2系に上げた話
決済サービスのSpring Bootのバージョンを2系に上げた話
 
Form認証で学ぶSpring Security入門
Form認証で学ぶSpring Security入門Form認証で学ぶSpring Security入門
Form認証で学ぶSpring Security入門
 
OAuth 2.0 Web Messaging Response Mode - OpenID Summit Tokyo 2015
OAuth 2.0 Web Messaging Response Mode - OpenID Summit Tokyo 2015OAuth 2.0 Web Messaging Response Mode - OpenID Summit Tokyo 2015
OAuth 2.0 Web Messaging Response Mode - OpenID Summit Tokyo 2015
 
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
 
OSS+AWSでここまでできるDevSecOps (Security-JAWS第24回)
OSS+AWSでここまでできるDevSecOps (Security-JAWS第24回)OSS+AWSでここまでできるDevSecOps (Security-JAWS第24回)
OSS+AWSでここまでできるDevSecOps (Security-JAWS第24回)
 
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
JJUG CCC 2013 Fall「JVMコードリーディング入門-JVMのOS抽象化レイヤーについて-」
 
Java仮想マシンの実装技術
Java仮想マシンの実装技術Java仮想マシンの実装技術
Java仮想マシンの実装技術
 
JVMのGCアルゴリズムとチューニング
JVMのGCアルゴリズムとチューニングJVMのGCアルゴリズムとチューニング
JVMのGCアルゴリズムとチューニング
 
Spring Bootをはじめる時にやるべき10のこと
Spring Bootをはじめる時にやるべき10のことSpring Bootをはじめる時にやるべき10のこと
Spring Bootをはじめる時にやるべき10のこと
 
SpringBootTest入門
SpringBootTest入門SpringBootTest入門
SpringBootTest入門
 
Java 17直前!オレ流OpenJDK「の」開発環境(Open Source Conference 2021 Online/Kyoto 発表資料)
Java 17直前!オレ流OpenJDK「の」開発環境(Open Source Conference 2021 Online/Kyoto 発表資料)Java 17直前!オレ流OpenJDK「の」開発環境(Open Source Conference 2021 Online/Kyoto 発表資料)
Java 17直前!オレ流OpenJDK「の」開発環境(Open Source Conference 2021 Online/Kyoto 発表資料)
 
Open Liberty: オープンソースになったWebSphere Liberty
Open Liberty: オープンソースになったWebSphere LibertyOpen Liberty: オープンソースになったWebSphere Liberty
Open Liberty: オープンソースになったWebSphere Liberty
 

More from Masatoshi Tada

ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
Masatoshi Tada
 
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_cccJPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
Masatoshi Tada
 

More from Masatoshi Tada (13)

これで怖くない!?コードリーディングで学ぶSpring Security #中央線Meetup
これで怖くない!?コードリーディングで学ぶSpring Security #中央線Meetupこれで怖くない!?コードリーディングで学ぶSpring Security #中央線Meetup
これで怖くない!?コードリーディングで学ぶSpring Security #中央線Meetup
 
基礎からのOAuth 2.0とSpring Security 5.1による実装
基礎からのOAuth 2.0とSpring Security 5.1による実装基礎からのOAuth 2.0とSpring Security 5.1による実装
基礎からのOAuth 2.0とSpring Security 5.1による実装
 
初めてでも30分で分かるSpring 5 & Spring Boot 2オーバービュー
初めてでも30分で分かるSpring 5 & Spring Boot 2オーバービュー初めてでも30分で分かるSpring 5 & Spring Boot 2オーバービュー
初めてでも30分で分かるSpring 5 & Spring Boot 2オーバービュー
 
ReactiveだけじゃないSpring 5 & Spring Boot 2新機能解説
ReactiveだけじゃないSpring 5 & Spring Boot 2新機能解説ReactiveだけじゃないSpring 5 & Spring Boot 2新機能解説
ReactiveだけじゃないSpring 5 & Spring Boot 2新機能解説
 
JSUG SpringOne 2017報告会
JSUG SpringOne 2017報告会JSUG SpringOne 2017報告会
JSUG SpringOne 2017報告会
 
とにかく分かりづらいTwelve-Factor Appの解説を試みる
とにかく分かりづらいTwelve-Factor Appの解説を試みるとにかく分かりづらいTwelve-Factor Appの解説を試みる
とにかく分かりづらいTwelve-Factor Appの解説を試みる
 
Java EEでもOAuth 2.0!~そしてPayara Micro on Cloud Foundryで遊ぶ~
Java EEでもOAuth 2.0!~そしてPayara Micro on Cloud Foundryで遊ぶ~Java EEでもOAuth 2.0!~そしてPayara Micro on Cloud Foundryで遊ぶ~
Java EEでもOAuth 2.0!~そしてPayara Micro on Cloud Foundryで遊ぶ~
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with Thymeleaf
 
Java EEハンズオン資料 JJUG CCC 2015 Fall
Java EEハンズオン資料 JJUG CCC 2015 FallJava EEハンズオン資料 JJUG CCC 2015 Fall
Java EEハンズオン資料 JJUG CCC 2015 Fall
 
Java EE 8先取り!MVC 1.0入門 [EDR2対応版] 2015-10-10更新
Java EE 8先取り!MVC 1.0入門 [EDR2対応版] 2015-10-10更新Java EE 8先取り!MVC 1.0入門 [EDR2対応版] 2015-10-10更新
Java EE 8先取り!MVC 1.0入門 [EDR2対応版] 2015-10-10更新
 
NetBeansでかんたんJava EE ○分間クッキング! #kuwaccho lt
NetBeansでかんたんJava EE ○分間クッキング! #kuwaccho ltNetBeansでかんたんJava EE ○分間クッキング! #kuwaccho lt
NetBeansでかんたんJava EE ○分間クッキング! #kuwaccho lt
 
ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
ステップ・バイ・ステップで学ぶラムダ式・Stream api入門 #jjug ccc #ccc h2
 
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_cccJPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
JPAの同時実行制御とロック20140518 #ccc_r15 #jjug_ccc
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Java EE 8新機能解説 -Bean Validation 2.0編-

  • 1. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 1
  • 2. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ 2
  • 3. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ 
 ▸ ▸ 3
  • 4. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 4
  • 5. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ 💖 5
  • 6. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 6
  • 7. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 7
  • 8. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ 8
  • 9. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ 
 ▸ 9
  • 10. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 10 public class HelloDto { @NotBlank(message = "{hello.notblank}") private String message; // getter/setter } @Path("/hello") public class HelloResource { @POST public Response post(@Valid HelloDto helloDto) { // } } 

  • 11. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 11 hello.notblank = ▸ ▸ hello.notblank = Message is required hello.notblank = ValidationMessages_ja.properties ValidationMessages_en.properties
  • 12. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 12 $ curl -v -X POST -H "Content-Type: application/json” -d ‘{"message":""}' http://localhost:8080/api/hello > POST /api/hello HTTP/1.1 > … > < HTTP/1.1 400 Bad Request < … < { "errorType":" ", "messages":[" "] } ※JSON ExceptionMapper
  • 13. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 13 Bean Validation 

  • 14. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 14
  • 15. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 👏 ▸ 15
  • 16. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ 16
  • 17. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 17
  • 18. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ @NotNull ▸ @Null ▸ @NotEmpty ▸ @NotBlank ▸ @AssertTrue ▸ @AssertFalse ▸ @Size ▸ @Pattern 18 ▸ @Email ▸ @Digits ▸ @DecimalMax ▸ @DecimalMin ▸ @Max ▸ @Min ▸ @Positive ▸ @PositiveOrZero ▸ @Negative ▸ @NegativeOrZero ▸ @Past ▸ @PastOrPresent ▸ @Future ▸ @FutureOrPresent ※
  • 19. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 19 @NotBlank CharSequence null ❌ @NotEmpty 
 null 0 ❌ @Email CharSequence ❌ @Positive ❌ @PositiveOrZero 0 ❌
  • 20. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 20 @Negative ❌ @NegativeOrZero 0 ❌ @PastOrPresent Date Calendar java.time.* ❌ @FutureOrPresent Date Calendar java.time.* ❌
  • 21. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ @NotBlank @NotEmpty @Email 👏 ▸ 
 ▸ 👏 ▸ @Past @PastOrPresent @Future @FutureOrPresent 21
  • 22. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ @Past 
 👏 ▸ @Max/@Positive 
 javax.money.MonetaryAmount 👏 ▸ 
 22
  • 23. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 23
  • 24. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 24 public class Sample { private Optional<@Size(min = 3) String> optionalString; private List<@NotNull String> list = new ArrayList<>(); // omitted } ▸ 👏 ▸ ※JSR Container element constraints
  • 25. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ OptionalXxx java.util.OptionalInt 
 XxxProperty IntegerProperty 
 👏 25 public class Sample { @Max(10) private OptionalInt optionalInt; // omitted }
  • 26. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 26 @Test public void test() { Sample sample = new Sample(); sample.addToOptionalString(“aa"); // 3 sample.addToList(null); // not null sample.addToOptionalInt(20); // 10 // Set<ConstraintViolation<Sample>> violations = validator.validate(sample); // assertEquals(3, violations.size()); }
  • 27. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 27 @Test public void test2() { Sample sample = new Sample(); sample.addToList(null); // not null 1 sample.addToList(null); // not null 2 // Set<ConstraintViolation<Sample>> violations = validator.validate(sample); // assertEquals(2, violations.size()); } ▸
  • 28. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 28
  • 29. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp @Max(value = 100, groups = Group1.class) @Max(value = 200, groups = Group2.class) int someValue; ▸ ▸ @Xxx.List 👏 29 @Max.List({ @Max(value = 100, groups = Group1.class), @Max(value = 200, groups = Group2.class) }) int someValue;
  • 30. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ParameterNameProvider 👏 ▸ -parameters 30
  • 31. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ConstraintValidator initialize() ▸ 31
  • 32. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 32
  • 33. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 👏 33
  • 34. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 34 <web-app …> <context-param> <param-name> javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN </param-name> <param-value>true</param-value> </context-param> <!-- omitted --> </web-app> 
 

  • 35. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 35 public class EqualsValidator implements ConstraintValidator<Equals, Object> { @Override public boolean isValid(Object obj, ConstraintValidatorContext context) { // 2 // true } @Constraint(validatedBy = EqualsValidator.class) public @interface Equals { String property1(); // 1 String property2(); // 2
  • 36. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 36 @Named @ViewScoped @Equals(property1 = "email1", property2 = "email2", message = “…”) public class CompareBean implements Serializable { @NotBlank(message = “…”) @Size(min = 3, message = “…”) private String email1; @NotBlank(message = “…”) @Size(min = 3, message = “…”) private String email2; // omitted 
 

  • 37. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 37 <h:form id="email-form"> <h:inputText value="#{compareBean.email1}"/><br/> <h:inputText value="#{compareBean.email2}"/><br/> <h:commandButton value=“ " action="#{compareBean.submit()}"/> <f:validateWholeBean value="#{compareBean}"/> </h:form> Managed Bean ※<f:validateBean/> validationGroups
  • 38. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 
 
 ▸ 
 38
  • 39. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ <f:validateWholeBean> 
 ▸ 39※ h:commandButton <h:form id="email-form"> <h:inputText value=“…”/><br/> <h:inputText value=“…”/><br/> <h:commandButton value=“ " action=“…”/> <f:validateWholeBean value="#{compareBean}"/> </h:form>
  • 40. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 40
  • 41. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ @NotBlank @NotEmpty @Email ▸ Optional ▸ 41
  • 42. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ 42
  • 43. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 😆 ▸ 😆 ▸ 😭 43
  • 44. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ ▸ ▸ ▸ 44
  • 45. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 45