org.springframework.validation.BindException.getMessage()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中org.springframework.validation.BindException.getMessage()方法的一些代码示例,展示了BindException.getMessage()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BindException.getMessage()方法的具体详情如下:
包路径:org.springframework.validation.BindException
类名称:BindException
方法名:getMessage

BindException.getMessage介绍

[英]Returns diagnostic information about the errors held in this object.
[中]返回有关此对象中保留的错误的诊断信息。

代码示例

代码示例来源:origin: hs-web/hsweb-framework

@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseMessage handleException(BindException e) {
  SimpleValidateResults results = new SimpleValidateResults();
  e.getBindingResult().getAllErrors()
      .stream()
      .filter(FieldError.class::isInstance)
      .map(FieldError.class::cast)
      .forEach(fieldError -> results.addResult(fieldError.getField(), fieldError.getDefaultMessage()));
  return ResponseMessage.error(400, results.getResults().isEmpty() ? e.getMessage() : results.getResults().get(0).getMessage()).result(results.getResults());
}

代码示例来源:origin: tumao2/hdw-dubbo

/**
 * 数据绑定异常
 * @param e
 * @return
 */
@ExceptionHandler(BindException.class)
public ResultMap bindExceptionHandler(BindException e) {
  logger.error(e.getMessage(), e);
  return ResultMap.error(500, "数据绑定异常");
}

代码示例来源:origin: codeabovelab/haven-platform

@ExceptionHandler(BindException.class)
@ResponseStatus(BAD_REQUEST)
@ResponseBody
public UiError bindErrorHandler(BindException bindException) {
  log.error("Can't process request", bindException);
  return createResponse(bindException.getMessage(), bindException.getAllErrors().toString(), BAD_REQUEST);
}

代码示例来源:origin: TomChen001/xmanager

@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Result processException(BindException ex) {
  log.error(ex.getMessage(), ex);
  FieldError error = ex.getFieldError();
  return getFieldErrorResult(error);
}

代码示例来源:origin: chillzhuang/blade-tool

@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R handleError(BindException e) {
  log.warn("参数绑定失败", e.getMessage());
  return handleError(e.getBindingResult());
}

代码示例来源:origin: leecho/cola-cloud

@ResponseBody
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.OK)
public Result handleBindException(BindException e) {
  log.error(ErrorStatus.ILLEGAL_DATA.getMessage() + ":" + e.getMessage());
  List<Map<String, Object>> fields = new ArrayList<>();
  for (FieldError error : e.getFieldErrors()) {
    Map<String, Object> field = new HashMap<>();
    field.put("field", error.getField());
    field.put("message", error.getDefaultMessage());
    fields.add(field);
  }
  return failure(ErrorStatus.ILLEGAL_DATA, fields);
}

代码示例来源:origin: pengziliu/spring-boot-2.0-leaning

@ExceptionHandler(value = BindException.class)
@ResponseBody
public Response<String> bindExceptionErrorHandler(BindException ex) throws Exception {
  logger.error("bindExceptionErrorHandler info:{}",ex.getMessage());
  Response<String> r = new Response<>();
  StringBuilder sb = new StringBuilder();
  FieldError fieldError = ex.getFieldError();
  sb.append(fieldError.getDefaultMessage());
  r.setMsg(sb.toString());
  r.setCode(Code.FAILED);
  return r;
}

代码示例来源:origin: com.soento/soento-web

/**
 * 参数绑定异常处理
 *
 * @param cause 异常
 * @return 响应结果
 */
@ExceptionHandler(BindException.class)
@ResponseBody
public RespData handleBindException(BindException cause, HttpServletRequest request) {
  // 输出错误信息
  log.info("== 请求参数异常 ==");
  List<ObjectError> errors = cause.getAllErrors();
  for (ObjectError error : errors) {
    log.info(error.toString());
  }
  log.info(cause.getMessage(), cause);
  return validator(errors, request);
}

代码示例来源:origin: wyh-spring-ecosystem-student/spring-boot-student

/**
 * 统一处理 BindException 错误
 *
 * @param ex 参数验证失败错误
 * @return 参数验证失败响应
 */
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper<?> processBindException(BindException ex, HttpSession httpSession, HttpServletRequest request) {
  // 获取错误信息
  List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
  // 目前消息只返回一条错误信息,所以只需要取第一条错误信息即可
  Map<String, String> errorMap = null;
  if (fieldErrors.size() > 0) {
    errorMap = new HashMap<>(fieldErrors.size());
    for (FieldError fieldError : fieldErrors) {
      errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
    }
  }
  // 请求路径
  String url = request.getRequestURI();
  logger.warn("请求sessionId:{},请求接口:{},请求参数:{}, 异常信息: {}", httpSession.getId(), url, errorMap, ex.getMessage(), ex);
  return WrapMapper.wrap(10000, "参数校验异常", errorMap);
}

代码示例来源:origin: org.hswebframework.web/hsweb-spring-boot-starter

@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseMessage handleException(BindException e) {
  SimpleValidateResults results = new SimpleValidateResults();
  e.getBindingResult().getAllErrors()
      .stream()
      .filter(FieldError.class::isInstance)
      .map(FieldError.class::cast)
      .forEach(fieldError -> results.addResult(fieldError.getField(), fieldError.getDefaultMessage()));
  return ResponseMessage.error(400, results.getResults().isEmpty() ? e.getMessage() : results.getResults().get(0).getMessage()).result(results.getResults());
}

代码示例来源:origin: jkazama/sample-boot-micro

/** Controllerへのリクエスト紐付け例外 */
@ExceptionHandler(BindException.class)
public ResponseEntity<Map<String, String[]>> handleBind(BindException e) {
  log.warn(e.getMessage());
  return new ErrorHolder(msg, locale(), convert(e.getAllErrors()).list()).result(HttpStatus.BAD_REQUEST);
}

代码示例来源:origin: com.intoverflow.booster/booster-core

public static <T> void bindProperties(ApplicationContext context, String prefix, T bean) {
  PropertiesConfigurationFactory<T> factory = new PropertiesConfigurationFactory<>(bean);
  factory.setApplicationContext(context);
  factory.setConversionService(getConversionService(context));
  factory.setPropertySources(propertySources(context.getEnvironment()));
  factory.setTargetName(prefix);
  try {
    factory.bindPropertiesToTarget();
  } catch (BindException e) {
    log.error(e.getMessage(), e);
  }
}

相关文章