use of org.springframework.validation.FieldError in project tutorials by eugenp.
the class ExceptionTranslator method processValidationError.
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
use of org.springframework.validation.FieldError in project tutorials by eugenp.
the class CustomRestExceptionHandler method handleBindException.
@Override
protected ResponseEntity<Object> handleBindException(final BindException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
logger.info(ex.getClass().getName());
//
final List<String> errors = new ArrayList<String>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() + ": " + error.getDefaultMessage());
}
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
}
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
use of org.springframework.validation.FieldError in project spring-boot by Linda-Tan.
the class GlobalExceptionHandler method MethodArgumentNotValidHandler.
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public BaseResponse MethodArgumentNotValidHandler(MethodArgumentNotValidException exception) {
List invalidArguments = new ArrayList<>();
// 解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
for (FieldError error : exception.getBindingResult().getFieldErrors()) {
Set invalidArgument = new HashSet(3);
invalidArgument.add(error.getDefaultMessage());
invalidArgument.add(error.getField());
invalidArgument.add(error.getRejectedValue());
invalidArguments.add(invalidArgument);
}
return new ObjectRestResponse(StatusCodeEnum.EX_PRAM_ERROR, invalidArguments);
}
use of org.springframework.validation.FieldError in project arch-playground by BeneStem.
the class BadRequestException method badRequest.
public static BadRequestException badRequest(final Object target, final String targetName, final String fieldName, final String message) {
final var error = new BeanPropertyBindingResult(target, targetName);
error.addError(new FieldError(targetName, fieldName, message));
return badRequest(error);
}
use of org.springframework.validation.FieldError in project jprime by bgjug.
the class CfpController method validateEmail.
private String validateEmail(BindingResult bindingResult, Submission submission, Model model, String email, String role) {
EmailValidator emailValidator = new EmailValidator();
if (!StringUtils.isEmpty(email) && emailValidator.isValid(email, null)) {
// Email is valid
return null;
}
bindingResult.addError(new FieldError("submission", role + ".email", "Invalid Email!"));
return goToCFP(submission, model);
}
Aggregations