Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

243 views

Published on

GlassFish勉強会 2018 Springの資料です。

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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

  1. 1. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 1
  2. 2. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ 2
  3. 3. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ 
 ▸ ▸ 3
  4. 4. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 4
  5. 5. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ 💖 5
  6. 6. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 6
  7. 7. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 7
  8. 8. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ 8
  9. 9. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ 
 ▸ 9
  10. 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. 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. 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. 13. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 13 Bean Validation 

  14. 14. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 14
  15. 15. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 👏 ▸ 15
  16. 16. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ 16
  17. 17. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 17
  18. 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. 19. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 19 @NotBlank CharSequence null ❌ @NotEmpty 
 null 0 ❌ @Email CharSequence ❌ @Positive ❌ @PositiveOrZero 0 ❌
  20. 20. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 20 @Negative ❌ @NegativeOrZero 0 ❌ @PastOrPresent Date Calendar java.time.* ❌ @FutureOrPresent Date Calendar java.time.* ❌
  21. 21. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ @NotBlank @NotEmpty @Email 👏 ▸ 
 ▸ 👏 ▸ @Past @PastOrPresent @Future @FutureOrPresent 21
  22. 22. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ @Past 
 👏 ▸ @Max/@Positive 
 javax.money.MonetaryAmount 👏 ▸ 
 22
  23. 23. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 23
  24. 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. 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. 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. 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. 28. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 28
  29. 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. 30. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ParameterNameProvider 👏 ▸ -parameters 30
  31. 31. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ConstraintValidator initialize() ▸ 31
  32. 32. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 32
  33. 33. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 👏 33
  34. 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. 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. 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. 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. 38. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 
 
 ▸ 
 38
  39. 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. 40. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp 40
  41. 41. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ @NotBlank @NotEmpty @Email ▸ Optional ▸ 41
  42. 42. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ 42
  43. 43. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 😆 ▸ 😆 ▸ 😭 43
  44. 44. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ ▸ ▸ ▸ ▸ ▸ ▸ ▸ 44
  45. 45. (C) CASAREAL, Inc. All rights reserved. #glassfish_jp ▸ 45

×
Save this presentationTap To Close