本文整理汇总了Java中org.springframework.validation.BindingResult.getGlobalErrors方法的典型用法代码示例。如果您正苦于以下问题:Java BindingResult.getGlobalErrors方法的具体用法?Java BindingResult.getGlobalErrors怎么用?Java BindingResult.getGlobalErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.validation.BindingResult
的用法示例。
在下文中一共展示了BindingResult.getGlobalErrors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processValidationError
import org.springframework.validation.BindingResult; //导入方法依赖的package包/类
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
FieldErrorVM dto = new FieldErrorVM(ErrorConstants.ERR_VALIDATION, translate(ErrorConstants.ERR_VALIDATION));
for (FieldError fieldError : result.getFieldErrors()) {
dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
}
for (ObjectError globalError : result.getGlobalErrors()) {
dto.add(globalError.getObjectName(), globalError.getObjectName(), globalError.getCode());
}
return dto;
}
示例2: processValidationError
import org.springframework.validation.BindingResult; //导入方法依赖的package包/类
/**
* Method binding issues (raised by Spring framework) - mapped to BAD_REQUEST.
*
* @param ex
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestErrorResponse processValidationError(MethodArgumentNotValidException ex) {
final BindingResult result = ex.getBindingResult();
final Map<String, Object> globalErrorsMap = new LinkedHashMap<>();
final List<ObjectError> globalErrors = result.getGlobalErrors();
for (ObjectError objectError : globalErrors) {
globalErrorsMap.put(objectError.getObjectName(), objectError.getDefaultMessage());
}
final List<FieldError> fieldErrors = result.getFieldErrors();
final Map<String, Object> fieldErrorsMap = new LinkedHashMap<>();
for (FieldError fieldError : fieldErrors) {
fieldErrorsMap.put(fieldError.getObjectName() + '.' + fieldError.getField(),
fieldError.getDefaultMessage());
}
final Map<String, Object> additionalDetails = new LinkedHashMap<>();
if (!globalErrorsMap.isEmpty()) {
additionalDetails.put("global-errors", globalErrorsMap);
}
if (!fieldErrorsMap.isEmpty()) {
additionalDetails.put("field-errors", fieldErrorsMap);
}
final RestErrorResponse errorResponse =
new RestErrorResponse(HttpStatus.BAD_REQUEST.value(), "Failed to validate request");
if (!additionalDetails.isEmpty()) {
errorResponse.setAdditionalDetails(additionalDetails);
}
return errorResponse;
}