@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);
  }
コード例 #2
0
ファイル: ResponseBuilder.java プロジェクト: sigabort/venus
 public static BaseResponse createResponse(HttpStatus status, BindingResult result) {
   BaseResponse response = new BaseResponse();
   if (result.hasErrors()) {
     response.setError(true);
     response.setHttpErrorCode(status.value());
     /* create an array of total error count */
     ArrayList<ErrorDTO> errors = new ArrayList<ErrorDTO>(result.getErrorCount());
     /* append all field errors */
     for (FieldError err : result.getFieldErrors()) {
       System.out.println(
           "I got field error for: "
               + err.getField()
               + ", message: "
               + err.getDefaultMessage()
               + ", code: "
               + err.getCode());
       ErrorDTO dto = new ErrorDTO(err.getField(), err.getCode(), err.getDefaultMessage());
       errors.add(dto);
     }
     /* append global errors now */
     for (ObjectError err : result.getGlobalErrors()) {
       System.out.println(
           "I got global error for: "
               + err.getObjectName()
               + ", message: "
               + err.getDefaultMessage()
               + ", code: "
               + err.getCode());
       ErrorDTO dto = new ErrorDTO(err.getObjectName(), err.getCode(), err.getDefaultMessage());
       errors.add(dto);
     }
     response.setErrors(errors);
   }
   return response;
 }
  protected void handleResultErrors(BindingResult result) throws EscreeningDataValidationException {
    if (result.hasErrors()) {
      ErrorBuilder<EscreeningDataValidationException> errorBuilder =
          ErrorBuilder.throwing(EscreeningDataValidationException.class);

      for (ObjectError objectError : result.getAllErrors()) {

        ErrorMessage errorMessage = new ErrorMessage();

        if (objectError instanceof FieldError) {
          errorMessage.setName(((FieldError) objectError).getField());
        } else {
          errorMessage.setName(objectError.getObjectName());
        }

        errorMessage.setDescription(objectError.getDefaultMessage());
        errorBuilder.toUser(errorMessage);
      }
      // set admin message and throw error
      errorBuilder
          .toAdmin(
              "BaseDashboardRestController.handleResultErrors:  called with "
                  + result.getAllErrors().size()
                  + " errors")
          .throwIt();
    }
  }
コード例 #4
0
ファイル: TMUtils.java プロジェクト: carriercomm/broadband
 public static void printResultErrors(BindingResult result) {
   if (result.hasErrors()) {
     List<ObjectError> errors = result.getAllErrors();
     for (ObjectError error : errors)
       System.out.println(error.getObjectName() + ", " + error.getDefaultMessage());
   }
 }
コード例 #5
0
 @RequiresPermissions(value = "cartype:save")
 @RequestMapping(value = "/save", method = RequestMethod.POST)
 public String editCarType(
     CarType carType,
     BindingResult bindingResult,
     @RequestParam(value = "file") MultipartFile file,
     HttpSession httpSession) {
   if (bindingResult.hasErrors()) {
     for (ObjectError error : bindingResult.getAllErrors()) {
       System.out.println(error.getObjectName() + " : " + error.getDefaultMessage());
     }
   }
   carTypeService.updateCarType(carType, file, httpSession);
   return "redirect:/admin/cartype/list";
 }
コード例 #6
0
 @RequestMapping(value = "admin/user/useraddaction", method = RequestMethod.POST)
 public String addContact(
     @ModelAttribute("user") @Valid User user, BindingResult result, ModelMap m) {
   System.out.println("user : "******"lasterror", "");
   if (result.hasErrors()) {
     String errorOutput = "Registration failed : </br>";
     List<ObjectError> e = result.getAllErrors();
     for (ObjectError a : e) {
       errorOutput += a.getObjectName() + " : " + a.getDefaultMessage() + "</br>";
     }
     EMF.getHttpSession().setAttribute("lasterror", errorOutput);
     return "redirect:/app/admin/user/";
   }
   try {
     EMF.save(user);
     EMF.commit();
   } catch (Exception e) {
     m.addAttribute("error", e.getMessage());
   }
   return "redirect:/app/admin/user/";
 }