0
나는 스프링 부트와 함께 Hibernate Validator를 사용하고있다. 아래에 주석을 달았던 클래스에 변수가 있습니다.Hibernate Validator 메시지에 접근하기
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());
}
왜 유효성 검사 메시지에 액세스하는 것이 더 쉬운가요? 당신은 당신의 컨트롤러
@Autowired
private MessageSource messageSource;
에 메시지 소스를 autowire하기 및 메시지
messageSource.getMessage(<your key from the error>, null, locale);
당신에게 NED 로케일을 얻을 수
result.getMessage();
내 경우에는 키가'userEmail'입니까? 로케일을 검색하는 좋은 방법이 있습니까? – g3blv