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
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
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());
}
▸
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