When I send the request:
"Person": {
"name": 5
}
The request should fail (bad request) because 5 isn’t a String. It prints: Person{name='5'}
.
Similarly, there’s no error when I send null.
I have these annotations:
@JsonProperty("name")
@Valid
@NotBlank
private String name;
Controller:
public void register(@Valid @RequestBody Person p) {
...
}
How can I make it validate the name so only strings are accepted?
2
Answers
Add a
BindingResult
parameter.Use the
@Pattern
annotation.For more details check this link and this one for the regex.