0
我在Spring Boot中使用Hibernate验证器。我在下面注解的类中有一个变量。访问Hibernate验证器消息
Public class User {
@Pattern(regexp="[email protected]+\\..+", message="Wrong email!")
private String userEmail;
}
我在控制器中使用验证。
@PostMapping("/users")
public ResponseEntity addUser(@Valid @RequestBody User user, BindingResult result) {
if(result.hasErrors()) {
log.info("There are errors in the input");
}
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> inputErrors = validator.validate(user);
log.info("Errors: " + inputErrors.toString());
}
是否有更容易的方式访问验证消息,然后通过工厂?喜欢的东西
result.getMessage();
在我的情况下,密钥是'userEmail'吗?有没有一种检索区域设置的好方法? – g3blv