Commit 5f9e01e9 by 丁伟峰

for test

1 parent a5b007ca
...@@ -20,30 +20,34 @@ import java.util.stream.Collectors; ...@@ -20,30 +20,34 @@ import java.util.stream.Collectors;
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
private static final String BAD_REQUEST_MSG = "参数检验不通过"; private static final String BAD_REQUEST_MSG = "参数检验不通过";
//处理 form data方式调用接口校验失败抛出的异常 //处理 form data方式调用接口校验失败抛出的异常
@ExceptionHandler(BindException.class) @ExceptionHandler(BindException.class)
public Result<?> bindExceptionHandler(BindException e) { public Result<?> bindExceptionHandler(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList()); 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 请求体调用接口校验失败抛出的异常 // 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
List<String> collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList()); 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) @ExceptionHandler(ConstraintViolationException.class)
public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) { public Result<?> constraintViolationExceptionHandler(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations(); Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
List<String> collect = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList()); 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) @ExceptionHandler(Exception.class)
public Result<?> exceptionHandler(Exception e) { 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!