Java org.springframework.web.bind.MethodArgumentNotValidException 代码实例

・8 分钟阅读

以下是展示如何使用org.springframework.web.bind.MethodArgumentNotValidException的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 2


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 3


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 4


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 5


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseEntity<ErrorMessage> handleException(
                MethodArgumentNotValidException ex, 
                HttpServletResponse response) {
    BindingResult result = ex.getBindingResult();
    String message = "Missing required fields:";
    for (FieldError error: result.getFieldErrors()) {
        message += " " + error.getField();
    }
        return getErrorResponse(message, HttpStatus.UNPROCESSABLE_ENTITY);
}
 

实例 6


@ExceptionHandler(MethodArgumentNotValidException.class)
ResponseEntity<RestApiErrorDto> processValidationError(MethodArgumentNotValidException e) {
    BindingResult bindingResult = e.getBindingResult();
    List<FieldError> fieldErrors = bindingResult.getFieldErrors();
    ValidationErrorDTO validationErrorDTO = processFieldErrors(fieldErrors);
    return new ResponseEntity<>(new RestApiErrorDto("validation.error",
                "Validation of request body detected errors", validationErrorDTO), BAD_REQUEST);
}
 

实例 7


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 8


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 9


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public BddErrorMessage processValidationError(MethodArgumentNotValidException ex) {
   BindingResult result = ex.getBindingResult();
   List<FieldError> fieldErrors = result.getFieldErrors();
   BddErrorMessage validationErrMsg = new BddErrorMessage("BDD.INVALID_PARAMS", "");
   validationErrMsg.setErrors(processFieldErrors(fieldErrors).getErrors());
   return validationErrMsg;
}
 

实例 10


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 11


@Override
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    log.info("Bad Request: {}", ex.getMessage());
    log.debug("Bad Request: ", ex);
    final BindingResult result = ex.getBindingResult();
    final List<FieldError> fieldErrors = result.getFieldErrors();
    final ValidationErrorDTO dto = processFieldErrors(fieldErrors);
    return handleExceptionInternal(ex, dto, headers, HttpStatus.BAD_REQUEST, request);
}
 

实例 12


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 13


@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
 

实例 14


/**
 * Processes an error that occurs when the validation of an object fails.
 *
 * @param ex    The exception that was thrown when the validation failed.
 * @return      An error object that describes all validation errors.
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationErrorDTO handleValidationErrors(MethodArgumentNotValidException ex, Locale currentLocale) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    LOGGER.error("Found {} validation errors", fieldErrors.size());
    return constructValidationErrors(fieldErrors, currentLocale);
}
 
讨论
淘淘あ西西 profile image