Commit dde19761 by 丁伟峰

Merge branch 'feat-dingwf-mvp616' into develop

2 parents 3104b30e 5f9e01e9
......@@ -20,30 +20,34 @@ import java.util.stream.Collectors;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final String BAD_REQUEST_MSG = "参数检验不通过";
//处理 form data方式调用接口校验失败抛出的异常
@ExceptionHandler(BindException.class)
public Result<?> bindExceptionHandler(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(400, BAD_REQUEST_MSG, collect);
return Result.failed(String.join(";", collect));
}
// 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
return Result.instance(400, BAD_REQUEST_MSG, collect);
return Result.failed(String.join(";", collect));
}
// 处理单个参数校验失败抛出的异常
@ExceptionHandler(ConstraintViolationException.class)
public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
List<String> collect = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
return Result.instance(400, BAD_REQUEST_MSG, collect);
return Result.failed(String.join(";", collect));
}
// 处理以上处理不了的其他异常
@ExceptionHandler(Exception.class)
public Result<?> exceptionHandler(Exception e) {
return Result.instance(400, BAD_REQUEST_MSG, e.getMessage());
return Result.failed(String.join(";", collect));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!