本文整理匯總了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);
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}