[英]How validate domain object in controller input filled by Spring Data?
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;您需要使用 javax.validation.Valid 并向您的 Person pojo 添加约束: javax.validation.constraints.Email/NotBlank/NotNull;
public class Person {
@NotNull
private Long id;
/* here getters and setters...*/
}
In your controller:在您的 controller 中:
@PostMapping("/person/{id}")
@ResponseBody
public boolean isAdmin(@Valid @RequestBody Person person) {
return person.isAdmin();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.