For example I have:
@PostMapping("/person/{id}")
@ResponseBody
public boolean isAdmin(@PathVariable("id") Person person) {
return person != null && person.isAdmin();
}
How I can get same result using Validation?
This not works for me, but I look up for something like this, without manual checking in method body. Is there way for doing so?
@PostMapping("/person/{id}")
@ResponseBody
public boolean isAdmin(@PathVariable("id") @NotNull Person person) {
return person.isAdmin();
}
you need to use javax.validation.Valid and add constraints to your Person pojo: javax.validation.constraints.Email/NotBlank/NotNull;
public class Person {
@NotNull
private Long id;
/* here getters and setters...*/
}
In your controller:
@PostMapping("/person/{id}")
@ResponseBody
public boolean isAdmin(@Valid @RequestBody Person person) {
return person.isAdmin();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.