英文:
How to receive validation message on frontend in Spring Framework?
问题
我有验证约束:
@Size(min = 3, message = "First name 'validatedValue' must be minimum 3 characters long")
@Pattern(regexp = "^A(\\w)+$", message = "First name must start with capital letter")
private String firstName;
在控制器中,我对请求体参数使用了@Valid
注解。
如果验证失败,Spring会处理此问题,但我没有收到消息。
为什么会发生这种情况?
我尝试添加了以下处理程序:
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<String> handle(ConstraintViolationException constraintViolationException) {
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
String errorMessage = "";
if (!violations.isEmpty()) {
...
} else {
errorMessage = "ConstraintViolationException occurred.";
}
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}
英文:
I have validation constraints:
@Size(min = 3, message = "First name '${validatedValue} must be minimum 3 characters long'")
@Pattern(regexp = "^A(\\w)+$", message = "First name must start with capital letter")
private String firstName;
In controller I have @Valid
annotation for the request body argument.
If validation is failed - Spring resolves this, but I do not receive message.
Why does it happen?
I have tried to add this handler:
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<String> handle(ConstraintViolationException constraintViolationException) {
Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations();
String errorMessage = "";
if (!violations.isEmpty()) {
...
} else {
errorMessage = "ConstraintViolationException occured.";
}
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}
答案1
得分: 0
我猜,通过获取字段错误,可能是可以的。
希望下面的示例能够起作用:
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ResponseBuilder> handleMethodArgumentNotValidException(HttpServletRequest request,
MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<org.springframework.validation.FieldError> fieldErrors = result.getFieldErrors();
String errorMessage = messageSource.getMessage(ApiResponseCode.ERR_COMMON_REQ_BODY.getCode(),
new Object[] {
fieldErrors.stream().map(field -> field.getDefaultMessage()).collect(Collectors.joining(",")) },
Locale.US);
LOGGER.error(errorMessage, ex);
return new ResponseEntity<ResponseBuilder>(
new ResponseBuilder.Builder(ApiResponseCode.ERR_COMMON_REQ_BODY.getCode(), errorMessage).build(),
HttpStatus.OK);
}
英文:
I guess, It is possible by getting field error.
Hope below sample works out
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ResponseBuilder> handleMethodArgumentNotValidException(HttpServletRequest request,
MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<org.springframework.validation.FieldError> fieldErrors = result.getFieldErrors();
String errorMessage = messageSource.getMessage(ApiResponseCode.ERR_COMMON_REQ_BODY.getCode(),
new Object[] {
fieldErrors.stream().map(field -> field.getDefaultMessage()).collect(Collectors.joining(",")) },
Locale.US);
LOGGER.error(errorMessage, ex);
return new ResponseEntity<ResponseBuilder>(
new ResponseBuilder.Builder(ApiResponseCode.ERR_COMMON_REQ_BODY.getCode(), errorMessage).build(),
HttpStatus.OK);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论