2.3K Views
May 26, 22
スライド概要
GlassFish勉強会 2018 Springの資料です。
Java、Spring、IntelliJ IDEA
#glassfish_jp +BWB&&৽ػೳղઆ #FBO7BMJEBUJPOฤ גΧαϨΞϧଟాਅහ (MBTT'JTI6TFS(SPVQ+BQBOษڧձ ݄ (C) CASAREAL, Inc. All rights reserved. 1
#glassfish_jp ͜ͷηογϣϯʹ͍ͭͯ ▸ #FBO7BMJEBUJPOͷ৽ػೳΛղઆ͠·͢ ▸ ʲத͚ऀڃʳ#FBO7BMJEBUJPOΛͬͨ͜ͱ͕ ͋Δํ͕ରͰ͢ ▸ αϯϓϧϓϩάϥϜ(JU)VCʹެ։͍ͯ͠·͢ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBCFBO WBMJEBUJPOTBNQMF 2 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣗݾհ ▸ ଟాਅහʢ!TVLF@NBTBʣ ▸ ݚमτϨʔφʔ!ΧαϨΞϧ ▸ +BWB&&4QSJOH .JDSPTFSWJDFT$MPVE'PVOESZ ▸ 1JWPUBMೝఆߨࢣ ▸ (6(+ӡӦϝϯόʔɺ+46(ελοϑ 3 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣜגձࣾΧαϨΞϧ ▸ ଞࣾʹແ͍৭ʑͳϓϩάϥϛϯάޠݴͷݚम Λఏ͢·͍ͯ͠ڙʂ 4 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp <$.>+BWB&&ݚमͬͯ·͢ ᶃ +BWB&&8FCΞϓϦέʔγϣϯ։ൃ ▸ +4' $%* +1"ͷૅجΛֶश ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDFPQFOTFNJOBSKBWBQB ᶄ 3&458FCαʔϏε։ൃೖ ▸ 3&45͓Αͼ+"934ͷૅجΛֶश ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDFPQFOTFNJOBSKBWBQB ▸ $BTBSFBM💖1BZBSB 5 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 6 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 7 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp #FBO7BMJEBUJPOͱʁ ▸ +BWB&&ͷҰ෦ͱͯ͠ఆٛ͞Εͨೖྗূݕͷ༷ ▸ +BWB&&ʹݶΒͣɺ༷ʑͳϑϨʔϜϫʔΫͷೖྗ ͰূݕΘΕ͍ͯΔ ▸ 4QSJOHɺ(SBJMTɺ%SPQXJ[BSEɺʜ 8 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp #FBO7BMJEBUJPOʮ༷ʯ ▸ #FBO7BMJEBUJPOͰܾ·ͬͯ ͍Δͷ༷ॻɾΠϯλϑΣʔ εɾΞϊςʔγϣϯͳͲ ▸ ࢀর࣮ )JCFSOBUF7BMJEBUPS ༷ #FBO7BMJEBUJPO ࣮ )JCFSOBUF7BMJEBUPS ▸ Ͱ࣌ݱ།Ұͷ࣮ 9 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp +"934Ͱͷར༻ྫ public class HelloDto { @NotBlank(message = "{hello.notblank}") private String message; // getter/setter } ΞϊςʔγϣϯΛ ͚Δ͚ͩʂ @Path("/hello") public class HelloResource { @POST public Response post(@Valid HelloDto helloDto) { // Կ͔ॲཧ } } 10 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp Τϥʔϝοηʔδ ▸ TSDNBJOSFTPVSDFT 7BMJEBUJPO.FTTBHFTQSPQFSUJFTʹهड़ hello.notblank = ϝοηʔδඞਢೖྗͰ͢ ▸ ଟޠݴରԠՄೳ ▸ ͨͩ͠αʔόʔͷϩέʔϧʹͳΔ ValidationMessages_ja.properties hello.notblank = ϝοηʔδඞਢೖྗͰ͢ ValidationMessages_en.properties hello.notblank = Message is required (C) CASAREAL, Inc. All rights reserved. 11
#glassfish_jp
࣮ߦྫ
$ 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.
12
#glassfish_jp ͬͱৄ͘͠Γ͍ͨਓ ▸ ʮύʔϑΣΫτ+BWB&&ʯΛಡΈ·͠ΐ͏ʂ Bean Validationͷষ ٠ా͞Μ͕୲ʂ 13 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽͍͠ূݕΞϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 14 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp #FBO7BMJEBUJPO֓ཁ ▸ +BWB&&ͷҰ෦ͱ݄ͯ͠ϦϦʔε👏 ▸ ରԠ͢Δ࣮)JCFSOBUF7BMJEBUPS 15 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp #FBO7BMJEBUJPOͷجຊํ ᶃ ੍ΞϊςʔγϣϯͷՃ ▸ Ұ෦)JCFSOBUF7BMJEBUPSͷͷΛऔΓࠐΉ ᶄ +%,৽ػೳͷ༻׆ ▸ ܕΞϊςʔγϣϯɺ෮ՄೳΞϊςʔγϣϯ ▸ %BUFBOE5JNF"1*ɺKBWBVUJM0QUJPOBM ▸ ϦϑϨΫγϣϯʹΑΔϝιουҾ໊ͷऔಘ ▸ ΠϯλϑΣʔεͷσϑΥϧτϝιου 16 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 17 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp #7ͷશ੍Ξϊςʔγϣϯ ▸ @NotNull ▸ @Email ▸ @Negative ▸ @Null ▸ @Digits ▸ @NegativeOrZero ▸ @NotEmpty ▸ @DecimalMax ▸ @Past ▸ @NotBlank ▸ @DecimalMin ▸ @PastOrPresent ▸ @AssertTrue ▸ @Max ▸ @Future ▸ @AssertFalse ▸ @Min ▸ @Size ▸ @Positive ▸ @Pattern ▸ @PositiveOrZero ▸ @FutureOrPresent ※ଠࣈ͕৽Ξϊςʔγϣϯ 18 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ৽Ξϊςʔγϣϯղઆ ໊લ ର @NotBlank CharSequence @NotEmpty จࣈྻɺྻɺ ίϨΫγϣϯ આ໌ nullɺۭจࣈɺ ֯εϖʔε❌ nullɺ͞0❌ ϝʔϧΞυϨεࣜܗ @Email CharSequence @Positive ܕ ਖ਼ͷͰͳ͍ͱ❌ @PositiveOrZero ܕ 0Ҏ্Ͱͳ͍ͱ❌ Ͱͳ͍ͱ❌ 19 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ৽Ξϊςʔγϣϯղઆ ໊લ ର આ໌ @Negative ܕ ෛͷͰͳ͍ͱ❌ @NegativeOrZero ܕ 0ҎԼͰͳ͍ͱ❌ @PastOrPresent DateɺCalendarɺ java.time.* աͨ·ڈͱࡏݱ @FutureOrPresent DateɺCalendarɺ java.time.* ಉ࣌͡Ͱͳ͍ͱ❌ ະདྷ·ͨͱࡏݱ ಉ࣌͡Ͱͳ͍ͱ❌ 20 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ػ৽͍͠خೳ ▸ @NotBlankɺ@NotEmptyɺ@Email͕ඪ४ʹ👏 ▸ ݩʑ)JCFSOBUF7BMJEBUPSͷಠࣗΞϊςʔγϣϯ ʢ͜ΕΒඇਪʹͳͬͨʣ ▸ %BUFBOE5JNF"1*ʹࢦఆՄೳʹ👏 ▸ @Pastɺ@PastOrPresentɺ@Futureɺ @FutureOrPresent 21 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp <ࢀߟ>)JCFSOBUF7BMJEBUPSಠࣗػೳ ▸ @PastͳͲͷ੍࣌ ˠ+PEB5JNFͷΫϥεʹద༻Մೳ👏 ▸ @Max/@PositiveͳͲͷ੍ ˠจࣈྻjavax.money.MonetaryAmountʹద༻ Մೳ👏 ▸ )JCFSOBUF7BMJEBUPS͕།Ұͷ࣮ͳͷͰɺ ಠࣗػೳͷ༻׆ʢʹϩοΫΠϯʣΞϦʁ 22 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 23 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
ܕҾͷ੍ࢦఆ
▸ δΣωϦΫεͷܕҾʹ੍ࢦఆ͕Մೳʹ👏
▸ ίϨΫγϣϯͷதͳͲͷ͕ূݕՄೳʂ
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.
24
#glassfish_jp ೖΕΫϥεͷ੍ࢦఆ ▸ OptionalXxxʢjava.util.OptionalIntʣɺ +BWB'9ͷXxxPropertyʢIntegerPropertyʣ ʹ੍ࢦఆ͕Մೳʹ👏 public class Sample { @Max(10) private OptionalInt optionalInt; } // omitted 25 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
࣮ߦ
@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());
}
26
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
ίϨΫγϣϯʹෳͷҧ͕͋ͬͨΒʁ
▸ ҧͨ͠ཁૉͷ੍͚ͩҧ͕ग़Δ
@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());
}
27
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 28 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ͦͷଞ͍Ζ͍Ζ ▸ ੍Ξϊςʔγϣϯ͕෮Մೳʹͳͬͨ ▸ @Xxx.Listͷࢦఆ͕ෆཁʹ👏 ʙ#7Y @Max.List({ @Max(value = 100, groups = Group1.class), @Max(value = 200, groups = Group2.class) }) int someValue; #7ʙ @Max(value = 100, groups = Group1.class) @Max(value = 200, groups = Group2.class) int someValue; 29 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ͦͷଞ͍Ζ͍Ζ ▸ ϝιουίϯετϥΫλͷҾʹରͯ͠όϦ σʔγϣϯͨ͠ࡍɺParameterNameProviderͰ Ҿ໊ΛऔಘͰ͖ΔΑ͏ʹͳͬͨ👏 ▸ ίϯύΠϧ࣌ʹ-parametersΦϓγϣϯ͕ඞཁ 30 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ͦͷଞ͍Ζ͍Ζ ▸ ConstraintValidatorΠϯλϑΣʔεͷ initialize()ϝιου͕ۭͷσϑΥϧτϝιο υʹͳͬͨ ▸ .&5"*/'WBMJEBUJPOYNMͷ9.-໊લۭ͕ؒม ߋ͞Εͨ 31 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࣍ ᶃ #FBO7BMJEBUJPOͱʁ ᶄ #FBO7BMJEBUJPO֓ཁ ᶅ #7৽੍͍͠Ξϊςʔγϣϯ ᶆ #7ܕҾͷ੍ࢦఆ ᶇ #7ͦͷଞ͍Ζ͍Ζ ᶈ +4'+4'Ͱͷ૬ؔόϦσʔγϣϯ 32 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp +4'ͷ૬ؔόϦσʔγϣϯ ▸ ෳ߲Λൺֱͨ͠όϦσʔγϣϯ͕ग़དྷΔΑ ͏ʹͳͬͨ👏 33 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp खॱᶃXFCYNMͷઃఆ <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> 34 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
खॱᶄ੍Ξϊςʔγϣϯͷ࡞
@Constraint(validatedBy = EqualsValidator.class)
public @interface Equals {
String property1(); // ൺֱରͷ1ͭͷϓϩύςΟ໊
String property2(); // ൺֱରͷ2ͭͷϓϩύςΟ໊
public class EqualsValidator
implements ConstraintValidator<Equals, Object> {
@Override
public boolean isValid(Object obj,
ConstraintValidatorContext context) {
// ࢦఆ͞Εͨ2ͭͷϓϩύςΟͷΛऔಘͯ͠ɺ
// ಉͩͬͨ͡ΒtrueΛฦ͢ॲཧΛॻ͘
}
35
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp खॱᶅ.BOBHFE#FBOʹΞϊςʔτ @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 36 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
खॱᶆ'BDFMFUTʹλάΛهड़
<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ଐੑͰόϦσʔγϣϯάϧʔϓΛࢦఆՄೳ
37
(C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ҙ ▸ ݸผϓϩύςΟͷূݕ ˠΫϥεϨϕϧόϦσʔγϣϯ ͷॱʹ࣮ߦ͞ΕΔ ▸ ݸผͷϓϩύςΟͰ੍ҧ͕͋ͬͨΒɺ ΫϥεϨϕϧόϦσʔγϣϯ࣮ߦ͞Εͳ͍ʂ 38 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp
ҙ
▸ <f:validateWholeBean>ɺ
ඞͣGPSNͷҰ൪Լʹॻ͘ʂ
▸ ͦ͏Ͱͳ͍ͱྫ֎ൃੜ
Ұ൪Լʂʂ
<h:form id="email-form">
ϝʔϧΞυϨεɿ<h:inputText value=“…”/><br/>
ϝʔϧΞυϨεʢ֬ೝʣɿ<h:inputText value=“…”/><br/>
<h:commandButton value=“ૹ৴" action=“…”/>
<f:validateWholeBean value="#{compareBean}"/>
</h:form>
※ਖ਼֬ʹͱ͏ݴʮίϯϙʔωϯτπϦʔͷҰ൪ԼʯͳͷͰɺh:commandButtonΑΓ্Ͱྑ͍
(C) CASAREAL, Inc. All rights reserved.
39
#glassfish_jp ·ͱΊ (C) CASAREAL, Inc. All rights reserved. 40
#glassfish_jp ·ͱΊ ▸ ศརͳ੍Ξϊςʔγϣϯ͕ಋೖ͞Εͨʂ ▸ @NotBlankɺ@NotEmptyɺ@Emailɺɾɾɾ ▸ ίϨΫγϣϯOptionalͳͲͷཁૉͷ੍ࢦ ఆ͕Մೳʹʂ ▸ ੍ͷܕΞϊςʔγϣϯԽ 41 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ·ͱΊ ▸ +%,ʹରԠʂ ▸ %BUFBOE5JNF"1*ɺ෮ՄೳΞϊςʔγϣϯɾɾɾ ▸ +4'Ͱ૬ؔόϦσʔγϣϯ͕ग़དྷΔΑ͏ʹʂ ▸ ΫϥεϨϕϧόϦσʔγϣϯ 42 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ݸਓతͳײ ▸ ৭ʑͱศརͳػೳ͕ೖͬͯ͏Ε͍͠😆 ▸ ໘ͩͬͨ+4'ͷ૬ؔόϦσʔγϣϯ͕ग़དྷΔͷ ͏Ε͍͠😆 ▸ +"934ɺόϦσʔγϣϯάϧʔϓ·ͩαϙʔ τ֎Β͍͠😭 43 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp ࢀߟࢿྉ ▸ #FBO7BMJEBUJPOTQFDJpDBUJPO +43 ▸ IUUQCFBOWBMJEBUJPOPSHTQFD ▸ )JCFSOBUF7BMJEBUPS3FGFSFODF(VJEF ▸ IUUQTEPDTKCPTTPSHIJCFSOBUFTUBCMFWBMJEBUPSSFGFSFODFFO64 IUNM@TJOHMF ▸ +BWB4FSWFS'BDFT4QFDJpDBUJPOWFSTJPO +43 ▸ IUUQTKDQPSHBCPVU+BWBDPNNVOJUZQSPDFTTpOBMKTSJOEFYIUNM ▸ "OHIFM-FPOBSE͞Μͷ+4'αϯϓϧίʔυ ▸ IUUQTHJUIVCDPN"OHIFM-FPOBSE+4'USFFNBTUFS +4'7BMJEBUF8IPMF#FBO&YBNQMF 44 (C) CASAREAL, Inc. All rights reserved.
#glassfish_jp &OKPZ:PVS7BMJEBUJPO-JGF ▸ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 45 (C) CASAREAL, Inc. All rights reserved.