Java Code Examples for org.springframework.validation.ObjectError#getDefaultMessage()

The following examples show how to use org.springframework.validation.ObjectError#getDefaultMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseController.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
/**
 * Parameters check message format.
 * 
 * @param bindingResult checkResult
 * @return
 */
private String getParamValidFaildMessage(BindingResult bindingResult) {
    List<ObjectError> errorList = bindingResult.getAllErrors();
    log.info("errorList:{}", JsonUtils.toJSONString(errorList));
    if (errorList == null) {
        log.warn("onWarning:errorList is empty!");
        return null;
    }

    ObjectError objectError = errorList.get(0);
    if (objectError == null) {
        log.warn("onWarning:objectError is empty!");
        return null;
    }
    return objectError.getDefaultMessage();
}
 
Example 2
Source File: UserController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 处理修改用户资料的请求
 *
 * @param user    user
 * @param session session
 * @return JsonResult
 */
@PostMapping(value = "save")
@ResponseBody
public JsonResult saveProfile(@Valid @ModelAttribute User user, BindingResult result, HttpSession session) {
    try {
        if (result.hasErrors()) {
            for (ObjectError error : result.getAllErrors()) {
                return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
            }
        }
        userService.save(user);
        configuration.setSharedVariable("user", userService.findUser());
        session.removeAttribute(HaloConst.USER_SESSION_KEY);
    } catch (Exception e) {
        log.error("Failed to modify user profile: {}", e.getMessage());
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-failed"));
    }
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-success"));
}
 
Example 3
Source File: UserController.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 处理修改用户资料的请求
 *
 * @param user    user
 * @param session session
 * @return JsonResult
 */
@PostMapping(value = "save")
@ResponseBody
public JsonResult saveProfile(@Valid @ModelAttribute User user, BindingResult result, HttpSession session) {
    try {
        if (result.hasErrors()) {
            for (ObjectError error : result.getAllErrors()) {
                return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
            }
        }
        userService.save(user);
        configuration.setSharedVariable("user", userService.findUser());
        session.removeAttribute(HaloConst.USER_SESSION_KEY);
    } catch (Exception e) {
        log.error("Failed to modify user profile: {}", e.getMessage());
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-failed"));
    }
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-success"));
}
 
Example 4
Source File: MenuController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 新增/修改菜单
 *
 * @param menu menu
 *
 * @return 重定向到/admin/menus
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult saveMenu(@Valid Menu menu, BindingResult result) {
    if (result.hasErrors()) {
        for (ObjectError error : result.getAllErrors()) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
        }
    }
    menu = menuService.save(menu);
    if (null != menu) {
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), "菜单保存成功!");
    } else {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), "菜单保存成功!");
    }
}
 
Example 5
Source File: TagController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 新增/修改标签
 *
 * @param tag tag
 *
 * @return JsonResult
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult saveTag(@Valid Tag tag, BindingResult result) {
    if (result.hasErrors()) {
        for (ObjectError error : result.getAllErrors()) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
        }
    }
    final Tag tempTag = tagService.findByTagUrl(tag.getTagUrl());
    if (null != tag.getTagId()) {
        if (null != tempTag && !tag.getTagId().equals(tempTag.getTagId())) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
        }
    } else {
        if (null != tempTag) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
        }
    }
    tag = tagService.save(tag);
    if (null == tag) {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
}
 
Example 6
Source File: CategoryController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 新增/修改分类目录
 *
 * @param category category对象
 *
 * @return JsonResult
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult saveCategory(@Valid Category category, BindingResult result) {
    if (result.hasErrors()) {
        for (ObjectError error : result.getAllErrors()) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
        }
    }
    final Category tempCategory = categoryService.findByCateUrl(category.getCateUrl());
    if (null != category.getCateId()) {
        if (null != tempCategory && !category.getCateId().equals(tempCategory.getCateId())) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
        }
    } else {
        if (null != tempCategory) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
        }
    }
    category = categoryService.save(category);
    if (null == category) {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
}
 
Example 7
Source File: ValidatorUtils.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public static List<String> asStringList(BindingResult br, boolean appendFieldname){
		if(br==null || !br.hasErrors())
			return Collections.emptyList();
		List<String> msglist = new ArrayList<String>();
		String msg = null;
		for(ObjectError error : br.getAllErrors()){
			msg = "";
			if(appendFieldname && FieldError.class.isInstance(error)){
				FieldError fe = (FieldError) error;
				FieldName info = findValidationInfo(br.getTarget().getClass(), fe.getField());
				msg = info==null?fe.getField():info.value();
//				msg = info==null?"":info.value();
			}
			msg += error.getDefaultMessage();
			msglist.add(msg);
		}
		return msglist;
	}
 
Example 8
Source File: GlobalControllerAdvice.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorMessage> handleMethodArgumentNotValid(MethodArgumentNotValidException ex
) {
	List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
	List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
	List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
	String error;
	for (FieldError fieldError : fieldErrors) {
		error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
		errors.add(error);
	}
	for (ObjectError objectError : globalErrors) {
		error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
		errors.add(error);
	}
	ErrorMessage errorMessage = new ErrorMessage(errors);

	//Object result=ex.getBindingResult();//instead of above can allso pass the more detailed bindingResult
	return new ResponseEntity(errorMessage, HttpStatus.BAD_REQUEST);
}
 
Example 9
Source File: GlobalControllerAdvice.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorMessage> handleMethodArgumentNotValid(MethodArgumentNotValidException ex
) {
	List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
	List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
	List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
	String error;
	for (FieldError fieldError : fieldErrors) {
		error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
		errors.add(error);
	}
	for (ObjectError objectError : globalErrors) {
		error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
		errors.add(error);
	}
	ErrorMessage errorMessage = new ErrorMessage(errors);

	//Object result=ex.getBindingResult();//instead of above can allso pass the more detailed bindingResult
	return new ResponseEntity(errorMessage, HttpStatus.BAD_REQUEST);
}
 
Example 10
Source File: GlobalControllerAdvice.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorMessage> handleMethodArgumentNotValid(MethodArgumentNotValidException ex
) {
	List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
	List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
	List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
	String error;
	for (FieldError fieldError : fieldErrors) {
		error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
		errors.add(error);
	}
	for (ObjectError objectError : globalErrors) {
		error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
		errors.add(error);
	}
	ErrorMessage errorMessage = new ErrorMessage(errors);

	//Object result=ex.getBindingResult();//instead of above can allso pass the more detailed bindingResult
	return new ResponseEntity(errorMessage, HttpStatus.BAD_REQUEST);
}
 
Example 11
Source File: ErrorAdvice.java    From Microservices-with-Spring-Cloud with MIT License 5 votes vote down vote up
public ErrorMessage(ObjectError objectError) {
    message = objectError.getDefaultMessage();
    code = objectError.getCode();
    objectName = objectError.getObjectName();
    if (objectError instanceof FieldError) {
        field = ((FieldError) objectError).getField();
    }
}
 
Example 12
Source File: StackValidationExceptionHandler.java    From gaia with Mozilla Public License 2.0 5 votes vote down vote up
private String getMessage(ObjectError error){
    if(error instanceof FieldError){
        var fieldError = ((FieldError)error);
        return fieldError.getField() + " " + fieldError.getDefaultMessage();
    }
    return error.getDefaultMessage();
}
 
Example 13
Source File: ErrorAdvice.java    From Microservices-with-Spring-Cloud with MIT License 5 votes vote down vote up
public ErrorMessage(ObjectError objectError) {
    message = objectError.getDefaultMessage();
    code = objectError.getCode();
    objectName = objectError.getObjectName();
    if (objectError instanceof FieldError) {
        field = ((FieldError) objectError).getField();
    }
}
 
Example 14
Source File: ValidationBindingResult.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public String getErrorMessage(ObjectError fe, ReturnableClosure<ObjectError, String> block){
	String fmsg = "";
	if(block!=null){
		fmsg = block.execute(fe);
	}else{
		if(fe!=null)
			fmsg = fe.getDefaultMessage();
	}
	return fmsg;
}
 
Example 15
Source File: PageController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 处理添加/修改友链的请求并渲染页面
 *
 * @param link Link实体
 *
 * @return JsonResult
 */
@PostMapping(value = "/links/save")
@ResponseBody
public JsonResult saveLink(@Valid Link link, BindingResult result) {
    if (result.hasErrors()) {
        for (ObjectError error : result.getAllErrors()) {
            return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
        }
    }
    link = linkService.save(link);
    if (null == link) {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
}
 
Example 16
Source File: BaseController.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
private String getParamValidFaildMessage(BindingResult bindingResult) {
    List<ObjectError> errorList = bindingResult.getAllErrors();
    log.info("errorList:{}", JsonUtils.toJSONString(errorList));
    if (errorList == null) {
        log.warn("onWarning:errorList is empty!");
        return null;
    }

    ObjectError objectError = errorList.get(0);
    if (objectError == null) {
        log.warn("onWarning:objectError is empty!");
        return null;
    }
    return objectError.getDefaultMessage();
}
 
Example 17
Source File: UniformHandler.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 处理Bean校验异常
 * @param ex            异常
 * @param bindingResult 错误绑定数据
 * @return  ResponseEntity
 */
private ResponseEntity<Object> handleValidBeanExceptionResponse(Exception ex, BindingResult bindingResult) {
    ObjectError objectError = bindingResult.getAllErrors().get(0);
    String message = objectError.getDefaultMessage();
    if (objectError.getArguments() != null && objectError.getArguments().length > 0) {
        FieldError fieldError = (FieldError) objectError;
        message = WebContext.getRequest().getRequestURI() + " [" + fieldError.getField() + "=" + fieldError.getRejectedValue() + "] " + message;
    }
    log.warn("Hydrogen uniform valid response exception with msg: {} ", message);
    return handleExceptionResponse(ex, HttpStatus.BAD_REQUEST.value(), message);
}
 
Example 18
Source File: BaseBindingResultAdviceTrait.java    From problem-spring-web with MIT License 4 votes vote down vote up
default Violation createViolation(final ObjectError error) {
    final String fieldName = formatFieldName(error.getObjectName());
    return new Violation(fieldName, error.getDefaultMessage());
}
 
Example 19
Source File: BaseBindingResultAdviceTrait.java    From problem-spring-web with MIT License 4 votes vote down vote up
default Violation createViolation(final ObjectError error) {
    final String fieldName = formatFieldName(error.getObjectName());
    return new Violation(fieldName, error.getDefaultMessage());
}
 
Example 20
Source File: FilterToolHelp.java    From circus-train with Apache License 2.0 4 votes vote down vote up
@Override
public String apply(@Nonnull ObjectError error) {
  return TAB + error.getDefaultMessage();
}