org.springframework.web.bind.support.WebExchangeBindException类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(229)

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

WebExchangeBindException介绍

[英]A specialization of ServerWebInputException thrown when after data binding and validation failure. Implements BindingResult (and its super-interface Errors) to allow for direct analysis of binding and validation errors.
[中]数据绑定和验证失败后引发的ServerWebInputException专门化。实现BindingResult(及其超级接口错误),以便直接分析绑定和验证错误。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void validate(Object target, Object[] validationHints, MethodParameter param,
    BindingContext binding, ServerWebExchange exchange) {
  String name = Conventions.getVariableNameForParameter(param);
  WebExchangeDataBinder binder = binding.createDataBinder(exchange, target, name);
  binder.validate(validationHints);
  if (binder.getBindingResult().hasErrors()) {
    throw new WebExchangeBindException(param, binder.getBindingResult());
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Returns diagnostic information about the errors held in this object.
 */
@Override
public String getMessage() {
  MethodParameter parameter = getMethodParameter();
  Assert.state(parameter != null, "No MethodParameter");
  StringBuilder sb = new StringBuilder("Validation failed for argument at index ")
      .append(parameter.getParameterIndex()).append(" in method: ")
      .append(parameter.getExecutable().toGenericString())
      .append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
  for (ObjectError error : this.bindingResult.getAllErrors()) {
    sb.append("[").append(error).append("] ");
  }
  return sb.toString();
}

代码示例来源:origin: spring-projects/spring-framework

private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor)
    throws URISyntaxException {
  ServerWebExchange exchange = postForm("age=invalid");
  Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
  mono = valueMonoExtractor.apply(mono);
  StepVerifier.create(mono)
      .consumeErrorWith(ex -> {
        assertTrue(ex instanceof WebExchangeBindException);
        WebExchangeBindException bindException = (WebExchangeBindException) ex;
        assertEquals(1, bindException.getErrorCount());
        assertTrue(bindException.hasFieldErrors("age"));
      })
      .verify();
}

代码示例来源:origin: naturalprogrammer/spring-lemon

public static List<LemonFieldError> getErrors(WebExchangeBindException ex) {
  
  List<LemonFieldError> errors = ex.getFieldErrors().stream()
    .map(LemonFieldError::of).collect(Collectors.toList());
  
  errors.addAll(ex.getGlobalErrors().stream()
    .map(LemonFieldError::of).collect(Collectors.toSet()));
  
  return errors;
}

代码示例来源:origin: hantsy/angular-spring-reactive-sample

WebExchangeBindException webExchangeBindException = (WebExchangeBindException) ex;
log.debug("errors:" + webExchangeBindException.getFieldErrors());
Errors errors = new Errors("validation_failure", "Validation failed.");
webExchangeBindException.getFieldErrors().forEach(e -> errors.add(e.getField(), e.getCode(), e.getDefaultMessage()));

代码示例来源:origin: spring-cloud/spring-cloud-open-service-broker

@ExceptionHandler(WebExchangeBindException.class)
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public ErrorMessage handleException(WebExchangeBindException ex) {
  return handleBindingException(ex, ex.getBindingResult());
}

代码示例来源:origin: spring-projects/spring-framework

if (adapter != null) {
  return adapter.fromPublisher(errors.hasErrors() ?
      Mono.error(new WebExchangeBindException(parameter, errors)) :
      valueMono);
    throw new WebExchangeBindException(parameter, errors);

代码示例来源:origin: org.springframework/spring-web

/**
 * Returns diagnostic information about the errors held in this object.
 */
@Override
public String getMessage() {
  MethodParameter parameter = getMethodParameter();
  Assert.state(parameter != null, "No MethodParameter");
  StringBuilder sb = new StringBuilder("Validation failed for argument at index ")
      .append(parameter.getParameterIndex()).append(" in method: ")
      .append(parameter.getExecutable().toGenericString())
      .append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
  for (ObjectError error : this.bindingResult.getAllErrors()) {
    sb.append("[").append(error).append("] ");
  }
  return sb.toString();
}

代码示例来源:origin: org.springframework/spring-web-reactive

protected void validate(Object target, Object[] validationHints,
    MethodParameter param, BindingContext binding, ServerWebExchange exchange) {
  String name = Conventions.getVariableNameForParameter(param);
  WebExchangeDataBinder binder = binding.createDataBinder(exchange, target, name);
  binder.validate(validationHints);
  if (binder.getBindingResult().hasErrors()) {
    throw new WebExchangeBindException(param, binder.getBindingResult());
  }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Returns diagnostic information about the errors held in this object.
 */
@Override
public String getMessage() {
  MethodParameter parameter = getMethodParameter();
  Assert.state(parameter != null, "No MethodParameter");
  StringBuilder sb = new StringBuilder("Validation failed for argument at index ")
      .append(parameter.getParameterIndex()).append(" in method: ")
      .append(parameter.getExecutable().toGenericString())
      .append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
  for (ObjectError error : this.bindingResult.getAllErrors()) {
    sb.append("[").append(error).append("] ");
  }
  return sb.toString();
}

代码示例来源:origin: org.springframework/spring-web-reactive

if (adapter != null) {
  return adapter.fromPublisher(errors.hasErrors() ?
      Mono.error(new WebExchangeBindException(parameter, errors)) :
      Mono.just(value));
    throw new WebExchangeBindException(parameter, errors);

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-web

/**
 * Returns diagnostic information about the errors held in this object.
 */
@Override
public String getMessage() {
  MethodParameter parameter = getMethodParameter();
  Assert.state(parameter != null, "No MethodParameter");
  StringBuilder sb = new StringBuilder("Validation failed for argument at index ")
      .append(parameter.getParameterIndex()).append(" in method: ")
      .append(parameter.getExecutable().toGenericString())
      .append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
  for (ObjectError error : this.bindingResult.getAllErrors()) {
    sb.append("[").append(error).append("] ");
  }
  return sb.toString();
}

代码示例来源:origin: spring-cloud/spring-cloud-open-service-broker

@Test
public void webExchangeBindExceptionException() throws NoSuchMethodException {
  BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
  bindingResult.addError(new FieldError("objectName", "field1", "message"));
  bindingResult.addError(new FieldError("objectName", "field2", "message"));
  Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
  MethodParameter parameter = new MethodParameter(method, -1);
  WebExchangeBindException exception =
      new WebExchangeBindException(parameter, bindingResult);
  ErrorMessage errorMessage = exceptionHandler.handleException(exception);
  assertThat(errorMessage.getError()).isNull();
  assertThat(errorMessage.getMessage()).contains("field1");
  assertThat(errorMessage.getMessage()).contains("field2");
}

相关文章