@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); }
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(); } }
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()); } }
@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"; }
@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/"; }