当前位置: 首页>>代码示例>>Java>>正文


Java MethodArgumentNotValidException.getBindingResult方法代码示例

本文整理汇总了Java中org.springframework.web.bind.MethodArgumentNotValidException.getBindingResult方法的典型用法代码示例。如果您正苦于以下问题:Java MethodArgumentNotValidException.getBindingResult方法的具体用法?Java MethodArgumentNotValidException.getBindingResult怎么用?Java MethodArgumentNotValidException.getBindingResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.web.bind.MethodArgumentNotValidException的用法示例。


在下文中一共展示了MethodArgumentNotValidException.getBindingResult方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: processHandler

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
/**
 * Rest handler for validation errors.
 * @param ex handled exception
 * @return rest result
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<?> processHandler(MethodArgumentNotValidException ex) {

  BindingResult bindingResult = ex.getBindingResult();
  List<FieldError> fieldErrors = bindingResult.getFieldErrors();
  List<FieldErrorDto> fieldErrorDtos = fieldErrors.stream()
      .map(FieldErrorDto::new)
      .collect(Collectors.toList());

  ValidationResultDto validationResultDto = new ValidationResultDto();
  validationResultDto.setFieldErrors(fieldErrorDtos);

  return ResponseEntity.badRequest().body(validationResultDto);
}
 
开发者ID:mental-party,项目名称:meparty,代码行数:21,代码来源:TestValidationHandler.java

示例2: processHandler

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
/**
 * Rest handler for validation errors.
 * @param ex handled exception
 * @return rest result
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<?> processHandler(MethodArgumentNotValidException ex) {

  BindingResult bindingResult = ex.getBindingResult();
  List<FieldError> fieldErrors = bindingResult.getFieldErrors();
  List<FieldErrorDto> fieldErrorDtos = fieldErrors.stream()
      .map(FieldErrorDto::new)
      .collect(Collectors.toList());

  ValidationResultDto validationResultDto = new ValidationResultDto();
  validationResultDto.setFieldErrors(fieldErrorDtos);

  LOGGER.error("VALIDATION ERROR: " + ex.getMessage());

  return ResponseEntity.badRequest().body(validationResultDto);
}
 
开发者ID:mental-party,项目名称:meparty,代码行数:23,代码来源:ValidationErrorHandler.java

示例3: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ErrorVM dto = new ErrorVM(ErrorConstants.ERR_VALIDATION);
    for (FieldError fieldError : fieldErrors) {
        dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
    }
    return dto;
}
 
开发者ID:oktadeveloper,项目名称:jhipster-microservices-example,代码行数:13,代码来源:ExceptionTranslator.java

示例4: handleMethodArgumentNotValid

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream()
        .map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode()))
        .collect(Collectors.toList());

    Problem problem = Problem.builder()
        .withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE)
        .withTitle("Method argument not valid")
        .withStatus(defaultConstraintViolationStatus())
        .with("message", ErrorConstants.ERR_VALIDATION)
        .with("fieldErrors", fieldErrors)
        .build();
    return create(ex, problem, request);
}
 
开发者ID:pascalgrimaud,项目名称:qualitoast,代码行数:17,代码来源:ExceptionTranslator.java

示例5: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
/**
 * @param ex {@link MethodArgumentNotValidException}
 * @return Returns an object with a list of error fields
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ValidationErrorDTO processValidationError(final MethodArgumentNotValidException ex) {
    final BindingResult result = ex.getBindingResult();
    final List<FieldError> fieldErrors = result.getFieldErrors();

    return processFieldErrors(fieldErrors);
}
 
开发者ID:JonkiPro,项目名称:REST-Web-Services,代码行数:13,代码来源:ErrorFieldsExceptionHandler.java

示例6: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    return processFieldErrors(fieldErrors);
}
 
开发者ID:klask-io,项目名称:klask-io,代码行数:10,代码来源:ExceptionTranslator.java

示例7: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    return processFieldErrors(fieldErrors);
}
 
开发者ID:quanticc,项目名称:sentry,代码行数:10,代码来源:ExceptionTranslator.java

示例8: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public List<ErrorMessage> processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> error = result.getFieldErrors();
    return processFieldError(error);
}
 
开发者ID:rovaniemi,项目名称:remember-me-back,代码行数:9,代码来源:ValidationController.java

示例9: handleMethodArgumentNotValid

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.error("400 Status Code", ex);
    final BindingResult result = ex.getBindingResult();
    final GenericResponse bodyOfResponse = new GenericResponse(result.getAllErrors(), "Invalid" + result.getObjectName());
    return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
 
开发者ID:Baeldung,项目名称:spring-security-registration,代码行数:8,代码来源:RestResponseEntityExceptionHandler.java

示例10: processValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
  @ResponseBody
  public Errors processValidationError(MethodArgumentNotValidException ex,HttpServletRequest request,HttpServletResponse response) {
Errors errors = new Errors();

      BindingResult result = ex.getBindingResult();
      List<FieldError> fieldErrors = result.getFieldErrors();
      for(FieldError fieldError : fieldErrors){
      	Error error = new Error();
      	error.setMessage(fieldError.getDefaultMessage());
      	errors.addError(error);
      }
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return errors;
  }
 
开发者ID:hserv,项目名称:coordinated-entry,代码行数:16,代码来源:BaseController.java

示例11: handleValidationError

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException ex) {
    final List<ValidationMessage> validationErrors = new ArrayList<>();
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    for (FieldError fieldError : fieldErrors) {
        validationErrors.add(ValidationMessage.builder()
                .entity(fieldError.getField())
                .messageTemplate(fieldError.getDefaultMessage())
                .build());
    }
    return new ResponseEntity<List<ValidationMessage>>(validationErrors, HttpStatus.BAD_REQUEST);
}
 
开发者ID:holisticon,项目名称:continuous-delivery-demo,代码行数:15,代码来源:GlobalControllerExceptionHandler.java

示例12: handleException

import org.springframework.web.bind.MethodArgumentNotValidException; //导入方法依赖的package包/类
/**
 * 异常处理器
 *
 * @param ex 对应的异常
 * @return 异常提示状态值
 * @throws Exception
 * @see BindException 字段式绑定异常
 * @see ConstraintViolationException 约束违反例外
 * @see CommonException 自定义异常
 * @see HttpMessageNotReadableException Http消息不可读的例外,主要用于@RequestBody 反序列化无法转换异常
 */
@ExceptionHandler(Exception.class)
@ResponseBody
public Map<String, Object> handleException(Exception ex) throws Exception {
    Map<String, Object> data = new HashMap<>();
    // TODO 2016/11/16 10:47 author: egan  判断是否为字段式绑定异常
    if(ex instanceof MethodArgumentNotValidException){
        MethodArgumentNotValidException me = (MethodArgumentNotValidException) ex;
        BindingResult bindingResult = me.getBindingResult();
        processFieldError(data, bindingResult.getTarget().getClass(), bindingResult.getFieldErrors());
    }else if (ex instanceof BindException) {
        BindException be = (BindException) ex;
        Class<?> targetClass = be.getTarget().getClass();
        //获取所有的对象错误集
        List<FieldError> errors = be.getFieldErrors();
        processFieldError(data, targetClass, errors);
        //约束违反异常 主要用于校验方法参数
    } else if (ex instanceof ConstraintViolationException) {
        ConstraintViolationException mcve = (ConstraintViolationException) ex;
        //获取校验不通过的异常集
        Set<ConstraintViolation<?>> constraintViolations = mcve.getConstraintViolations();
        for (ConstraintViolation<?> violation : constraintViolations) {
            Object[] executableParameters = violation.getExecutableParameters();
            //violation.getMessage() 主要为code(规范书写)
            if (violation.getMessage().matches("\\d+")) {
                Integer code = Integer.valueOf(violation.getMessage());
                data.put(CODE_KEY, errorCode.getCode(code));
                String message = errorCode.getMessage(code);
                if (null != message) {
                    data.put(MESSAGE_KEY, message);
                }
            } else {
                data.put(MESSAGE_KEY, violation.getMessage());
            }
            return data;
        }
        data.put(CODE_KEY, 1000);

    } else if (ex instanceof CommonException) {
        CommonException ce = (CommonException) ex;
        data.put(CODE_KEY, errorCode.getCode(ce.getCode()));
        data.put(MESSAGE_KEY, StringUtils.isEmpty(ce.getMessage()) ? errorCode.getMessage(ce.getCode()) : ce.getMessage());
        return data;
    } else if (ex instanceof HttpMessageNotReadableException) {
        data.put(CODE_KEY, 1000);
        data.put(MESSAGE_KEY, errorCode.getMessage(1000));
    } else if (ex instanceof MethodArgumentTypeMismatchException) {
        data.put(CODE_KEY, 1000);
        data.put(MESSAGE_KEY, errorCode.getMessage(1000));
    } else {
        throw ex;
    }
    return data;
}
 
开发者ID:egzosn,项目名称:spring-jdbc-orm,代码行数:65,代码来源:BaseController.java


注:本文中的org.springframework.web.bind.MethodArgumentNotValidException.getBindingResult方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。