实现默认大小验证的验证消息

Implement validation message for default size validation

我有 DTO,我想限制其大小:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class CreateUserDTO {

    @NotNull
    @NotEmpty
    @Size(min=1, max=20, message = "'Login' value '${validatedValue}' must be between {min} and {max} characters long.")
    private String login;

    @NotNull
    @NotEmpty
    @Size(min=1, max=45, message = "'Email' value '${validatedValue}' must be between {min} and {max} characters long.")
    private String email;

    @NotNull
    @NotEmpty
    @Size(min=1, max=20, message = "'First name' value '${validatedValue}' must be between {min} and {max} characters long.")
    private String firstName;

    @NotNull
    @NotEmpty
    @Size(min=1, max=20, message = "'Last name' value '${validatedValue}' must be between {min} and {max} characters long.")
    private String lastName;
}

休息控制器:

    @PostMapping("/create")
    public ResponseEntity<?> create(@Valid @RequestBody CreateUserDTO dto) {

        .....

        return ok().build();
    }

知道为什么我会收到这条巨大的错误消息:

{
    "errors": [
        {
            "status": 404,
            "code": "1001",
            "title": "Not found",
            "detail": "Validation failed for argument [0] in public org.springframework.http.ResponseEntity<?> org.engine.rest.UsersController.create(org.engine.dto.CreateUserDTO): [Field error in object 'createUserDTO' on field 'login': rejected value [56dtttttttttt444444444465st]; codes [Size.createUserDTO.login,Size.login,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createUserDTO.login,login]; arguments []; default message [login],20,1]; default message ['Login' value '56dtttttttttt444444444465st' must be between 1 and 20 characters long.]] ",
            "extra": {
                "detail": "Validation failed for argument [0] in public org.springframework.http.ResponseEntity<?> org.engine.rest.UsersController.create(org.engine.dto.CreateUserDTO): [Field error in object 'createUserDTO' on field 'login': rejected value [56dtttttttttt444444444465st]; codes [Size.createUserDTO.login,Size.login,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createUserDTO.login,login]; arguments []; default message [login],20,1]; default message ['Login' value '56dtttttttttt444444444465st' must be between 1 and 20 characters long.]] "
            }
        }
    ]
}

只是错误消息说明了问题出在哪里

default message ['Login' value '56dtttttttttt444444444465st' must be between 1 and 20 characters long.]

由于该值,您在 CreateUserDTO dto 中传递给了 login,请求中的字符数超过了您定义的最大值 (20)。由于验证失败它抛出这个错误。

您需要处理MethodArgumentNotValidException

快速实施就像

@ExceptionHandler({ MethodArgumentNotValidException.class })
public ResponseEntity<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
    BindingResult bindingResult = ex.getBindingResult();
    for (FieldError fieldError : bindingResult.getFieldErrors()) {
        // Do something with fieldError.getDefaultMessage();
        // Which is equivalent to 'Login' value '${validatedValue}' must be between {min} and {max} characters long.
    }

    for (ObjectError objectError : bindingResult.getGlobalErrors()) {
        // Do something with objectError.getDefaultMessage();
    }

    return ResponseEntity.ok().build();
}

查看您评论中的链接存储库,您可以添加一个新的异常处理程序来自定义与约束相关的验证错误。

现在所有未定义的异常都按定义路由到 here

您可以在 EngineExceptionhandler class.

中为与约束相关的错误定义异常处理程序

类似

@ExceptionHandler(ConstraintViolationException.class)
ResponseEntity<ErrorResponseDTO> constraintValidationException(final ConstraintViolationException e) {
    List<ErrorRerponse> errors = new ArrayList<>();
    for (ConstraintViolation violation : e.getConstraintViolations()) {
        ErrorResponse error = new ErrorResponse();
        error.setTitle(violation.getPropertyPath().toString());
        error.setDetail(violation.getMessage());
        errors.add(error);
    }
    ErrorResponseDTO errorResponse = new ErrorResponseDTO();
    errorResponse.setErrors(errors);
    return new ResponseEntity<ErrorResponseDTO>(errorResponse, HttpStatus.BAD_REQUEST);
}

您可以根据需要添加更多自定义设置。

我正在使用以下 ResponseEntityExceptionHandler 对我来说效果很好。

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {


  @Override
  protected ResponseEntity<Object> handleMethodArgumentNotValid(
      MethodArgumentNotValidException ex,
      HttpHeaders headers,
      HttpStatus status,
      WebRequest request) {
    Map<String, Map<String, List<Map<String, String>>>> errors = new HashMap<>();
    ex.getBindingResult()
        .getAllErrors()
        .forEach(
            error -> {
              String fieldName = ((FieldError) error).getField();
              String errorMessage = error.getDefaultMessage();
              Map<String, String> errorFields = new HashMap<>();
              errorFields.put("field", fieldName);
              errorFields.put("message", errorMessage);
              Map<String, List<Map<String, String>>> errorMap = new HashMap<>();
              errorMap.put("errors", List.of(errorFields));
              errors.put(fieldName, errorMap);
            });

    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
  }
}

您需要处理根异常并在向最终用户显示或发送给客户端之前对其进行美化。正如上面提到的答案,你可以从 ResponseEntityExceptionHandler 覆盖 handleMethodArgumentNotValid 方法,就像我在下面所做的那样:

@RestControllerAdvice
public class ServiceExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                                                                  HttpHeaders headers, HttpStatus status, WebRequest request) {

        StringBuilder sb = new StringBuilder();
        ex.getBindingResult().getAllErrors().forEach((error) -> {
            sb.append("[");
            sb.append(((FieldError) error).getField());
            sb.append(" ");
            sb.append(error.getDefaultMessage());
            sb.append("] ");
        });

    log.error("Method arg is invalid. " + sb.toString(), ex);

    return new ResponseEntity<>(sb.toString(), HttpStatus.BAD_REQUEST);
    }
}

log.error() 打印以下消息:

// here is the message I show to the end user
10:27:02 [ERROR] [ServiceExceptionHandler] 141: Method arg is invalid. [shareList size must be between '1' and '10'] 
// here is the stack trace you encountered
org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public
...
[Size.eyatTransferDTO.shareList,Size.shareList,Size.java.util.List,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [eyatTransferDTO.shareList,shareList]; arguments []; default message [shareList],10,1]; default message [size must be between '1' and '10']]
...