@Override
  protected ResponseEntity<Object> handleMethodArgumentNotValid(
      MethodArgumentNotValidException ex,
      HttpHeaders headers,
      HttpStatus status,
      WebRequest request) {
    final List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    final List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();

    StringBuilder errors = new StringBuilder();

    for (FieldError fieldError : fieldErrors) {
      errors
          .append(fieldError.getField())
          .append(": ")
          .append(fieldError.getDefaultMessage())
          .append("\n");
    }

    for (ObjectError globalError : globalErrors) {
      errors
          .append(globalError.getObjectName())
          .append(": ")
          .append(globalError.getDefaultMessage())
          .append("\n");
    }

    return new ResponseEntity<>(errors.toString(), headers, status);
  }
 /** Exception to be thrown when validation on an argument annotated with @Valid fails. */
 @ExceptionHandler(MethodArgumentNotValidException.class)
 public ResponseEntity<Map<String, Object>> handleMethodArgumentNotValidException(
     MethodArgumentNotValidException errors) {
   Map<String, Object> errorMap = new HashMap<String, Object>();
   errorMap.put("hasErrors", "true");
   errorMap.put("developerMessage", "There are validation issues, please provide valid inputs");
   errorMap.put("userMessage", "Please provide valid inputs");
   errorMap.put("moreInfo", errors.getMessage());
   errorMap.put("errorCode", HttpStatus.BAD_REQUEST);
   errors.printStackTrace();
   for (FieldError error : errors.getBindingResult().getFieldErrors()) {
     errorMap.put(error.getField(), error.getDefaultMessage());
   }
   return new ResponseEntity<Map<String, Object>>(errorMap, HttpStatus.BAD_REQUEST);
 }
Exemplo n.º 3
0
  @ExceptionHandler(MethodArgumentNotValidException.class)
  @ResponseBody
  public ResponseEntity<ErrorMessage> handleValidationException(MethodArgumentNotValidException e) {
    List<ObjectError> errors = e.getBindingResult().getAllErrors();
    List<String> messages = new ArrayList<String>();

    // WebServiceError webServiceError =
    // WebServiceError.build(WebServiceError.Type.VALIDATION_ERROR, errors.get(0).getObjectName() +
    // " " + errors.get(0).getDefaultMessage());

    for (ObjectError error : errors) {

      if (error instanceof FieldError) {
        FieldError fieldError = (FieldError) error;
        messages.add(
            fieldError.getField()
                + " "
                + fieldError.getRejectedValue()
                + " "
                + error.getDefaultMessage());
      } else {

        messages.add(error.toString());
      }
    }

    return new ResponseEntity<ErrorMessage>(new ErrorMessage(messages), HttpStatus.BAD_REQUEST);
  }
 @Override
 protected ResponseEntity<Object> handleMethodArgumentNotValid(
     final MethodArgumentNotValidException ex,
     final HttpHeaders headers,
     final HttpStatus status,
     final WebRequest request) {
   return handleBindingResult(ex, ex.getBindingResult(), headers, status, request);
 }
Exemplo n.º 5
0
  @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);
  }
Exemplo n.º 6
0
 @ExceptionHandler(MethodArgumentNotValidException.class)
 @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
 @ResponseBody
 public MessageDTO processValidationError(MethodArgumentNotValidException ex) {
   LOGGER.error("MethodArgumentNotValidException occurred .", ex);
   BindingResult result = ex.getBindingResult();
   FieldError error = result.getFieldError();
   return processFieldError(error);
 }
  @ExceptionHandler(MethodArgumentNotValidException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ResponseBody
  public ErrorTester noRequestHandlerFoundExceptionHandler(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    FieldError error = result.getFieldError();

    return new ErrorTester("There is error " + error.getDefaultMessage());
  }
 @ExceptionHandler(MethodArgumentNotValidException.class)
 @ResponseStatus(BAD_REQUEST)
 public @ResponseBody ErrorMessageResponse handleMethodArgumentNotValidException(
     MethodArgumentNotValidException exception) {
   LOG.info("Invalid method arguments.", exception);
   return new ErrorMessageResponse(
       transform(
           exception.getBindingResult().getFieldErrors(),
           error -> new ErrorMessage(error.getDefaultMessage())));
 }
  @ExceptionHandler
  @ResponseStatus(
      value = HttpStatus.UNPROCESSABLE_ENTITY,
      reason = "Invalid attributes provided for Room")
  public Model handleException(MethodArgumentNotValidException ex, Model model) {
    System.out.println("In first exception handler......");
    model.addAttribute("status", HttpStatus.UNPROCESSABLE_ENTITY);
    model.addAttribute("errors", ex.getBindingResult().getFieldErrors());

    return model;
  }
  @ExceptionHandler(MethodArgumentNotValidException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  public Map<String, String> handleValidationError(MethodArgumentNotValidException ex) {
    Map<String, String> fieldErrorMap = new HashMap<>();

    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    for (FieldError fieldError : fieldErrors) {
      fieldErrorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
    }

    return fieldErrorMap;
  }
 @Override
 protected ResponseEntity<Object> handleMethodArgumentNotValid(
     final MethodArgumentNotValidException ex,
     final HttpHeaders headers,
     final HttpStatus status,
     final WebRequest request) {
   logger.error("400 Status Code", ex);
   final BindingResult result = ex.getBindingResult();
   final GenericResponse bodyOfResponse =
       new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
   return handleExceptionInternal(
       ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
 }
Exemplo n.º 12
0
 /** 处理参数校验错误 */
 @ExceptionHandler({MethodArgumentNotValidException.class})
 @ResponseStatus(value = HttpStatus.BAD_REQUEST)
 @ResponseBody
 public BaseResponseEntity<List<String>> handleException(
     MethodArgumentNotValidException exception, HttpServletRequest request) {
   BindingResult bindingResult = exception.getBindingResult();
   List<ObjectError> objectErrors = bindingResult.getAllErrors();
   List<String> errorMessages = new LinkedList<String>();
   for (ObjectError objectError : objectErrors) {
     errorMessages.add(objectError.getDefaultMessage());
   }
   return BaseResponseEntity.build(
       HttpStatus.BAD_REQUEST.value(),
       APIStatus.INVALID_PARAMETER.getStatus(),
       APIStatus.INVALID_PARAMETER.name(),
       errorMessages,
       request);
 }