Java Code Examples for org.springframework.validation.BindException#getAllErrors()

The following examples show how to use org.springframework.validation.BindException#getAllErrors() . 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: ExceptionHandlerConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
	 * {@linkplain Valid} 验证异常统一处理-433
	 * 
	 * @param e 验证异常
	 * @return 结果
	 */
	@Override
    @ResponseBody
    @ExceptionHandler(BindException.class)
	public Result<?> bindExceptionHandler(BindException e) {
//		ServletUtils.getResponse().setStatus(433);
//    	String uri = ServletUtils.getRequest().getRequestURI();
//    	Console.error("uri={}", uri);
		List<ObjectError> errors = e.getAllErrors();
		JSONObject paramHint = new JSONObject();
		errors.forEach(error -> {
			String str = StrUtil.subAfter(error.getArguments()[0].toString(), "[", true);
			String key = str.substring(0, str.length() - 1);
			String msg = error.getDefaultMessage();
			paramHint.put(key, msg);
			Console.error(key + " " + msg);
		});
		
		return ResultInfo.paramCheckNotPass(paramHint.toString());
	}
 
Example 2
Source File: ExceptionHandlerConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * {@linkplain Valid} 验证异常统一处理-433
 * @param e 验证异常
 * @return 结果
 */
@Override
   @ResponseBody
   @ExceptionHandler(BindException.class)
public Result<?> bindExceptionHandler(BindException e) {
	ServletUtils.getResponse().setStatus(433);
   	String uri = ServletUtils.getRequest().getRequestURI();
   	Console.error("uri={}", uri);
	List<ObjectError> errors = e.getAllErrors();
	JSONObject paramHint = new JSONObject();
	errors.forEach(error -> {
		String str = StrUtil.subAfter(error.getArguments()[0].toString(), "[", true);
		String key = str.substring(0, str.length() - 1);
		String msg = error.getDefaultMessage();
		paramHint.put(key, msg);
		Console.error(key + " " + msg);
	});
	
	return ResultInfo.paramCheckNotPass(paramHint.toString());
}
 
Example 3
Source File: GlobalExceptionHandler.java    From goods-seckill with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler(BindException.class)
public Result<String> bindExceptionHandler(BindException e) {
	e.printStackTrace();
	List<ObjectError> allErrors = e.getAllErrors();
	ObjectError objectError = allErrors.get(0);
	String defaultMessage = objectError.getDefaultMessage();
	return Result.error(CodeMsg.BIND_ERROR.fillArgs(defaultMessage));
}
 
Example 4
Source File: ApiResponse.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
/**
 * 参数绑定错误响应
 *
 * @param exception 异常
 * @return 响应
 */
public static ApiResponse createRestResponse(BindException exception, HttpServletRequest request) {
    Builder builder = newBuilder();
    if (null != exception && exception.hasErrors()) {
        StringBuilder error = new StringBuilder("");
        for (ObjectError objectError : exception.getAllErrors()) {
            error.append(objectError.getDefaultMessage() + ";");
        }
        log.error(error.toString());
        builder.status(BaseApiStatus.SYST_SERVICE_UNAVAILABLE);
        builder.error(getMessage(String.valueOf(builder.getThis().status),request));
    }
    return builder.data(null).build();
}
 
Example 5
Source File: ParamValidateUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 从bindException中获取到参数错误类型,参数错误
 */
public static ModelAndView getParamErrors(BindException be) {

    // 构造error的映射
    Map<String, String> paramErrors = new HashMap<String, String>();
    Map<String, Object[]> paramArgusErrors = new HashMap<String, Object[]>();
    for (Object error : be.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fe = (FieldError) error;
            String field = fe.getField();

            // 默认的message
            String message = fe.getDefaultMessage();
            try {

                contextReader.getMessage(message, fe.getArguments());

            } catch (NoSuchMessageException e) {

                // 如果有code,则从前往后遍历Code(特殊到一般),修改message为code所对应
                for (int i = fe.getCodes().length - 1; i >= 0; i--) {
                    try {
                        String code = fe.getCodes()[i];
                        String info = contextReader.getMessage(code, fe.getArguments());
                        LOG.debug(code + "\t" + info);
                        message = code;
                    } catch (NoSuchMessageException e2) {
                        LOG.debug("");
                    }
                }
            }

            // 最终的消息
            paramErrors.put(field, message);
            paramArgusErrors.put(field, fe.getArguments());
        }
    }

    return ParamValidateUtils.paramError(paramErrors, paramArgusErrors, ErrorCode.FIELD_ERROR);
}
 
Example 6
Source File: ParamValidateUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 从bindException中获取到参数错误类型,参数错误
 */
public static ModelAndView getParamErrors(BindException be) {

    // 构造error的映射
    Map<String, String> paramErrors = new HashMap<String, String>();
    Map<String, Object[]> paramArgusErrors = new HashMap<String, Object[]>();
    for (Object error : be.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fe = (FieldError) error;
            String field = fe.getField();

            // 默认的message
            String message = fe.getDefaultMessage();
            try {

                contextReader.getMessage(message, fe.getArguments());

            } catch (NoSuchMessageException e) {

                // 如果有code,则从前往后遍历Code(特殊到一般),修改message为code所对应
                for (int i = fe.getCodes().length - 1; i >= 0; i--) {
                    try {
                        String code = fe.getCodes()[i];
                        String info = contextReader.getMessage(code, fe.getArguments());
                        LOG.debug(code + "\t" + info);
                        message = code;
                    } catch (NoSuchMessageException e2) {
                        LOG.debug("");
                    }
                }
            }

            // 最终的消息
            paramErrors.put(field, message);
            paramArgusErrors.put(field, fe.getArguments());
        }
    }

    return ParamValidateUtils.paramError(paramErrors, paramArgusErrors, ErrorCode.FIELD_ERROR);
}