/**
-
全局异常处理
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public Result handleUnauthorizedException(HttpServletRequest request, UnauthorizedException e) {
// 在这里定义返回的信息或者跳转到指定的页面
log.error(“无权限访问!!! {}”, e.getMessage());
return Result.error(“无权限访问”);
}@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result handler(MethodArgumentNotValidException e) {
log.error(“参数校验失败!!!!”, e);
BindingResult bindingResult = e.getBindingResult();
ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
return Result.fail(objectError.getDefaultMessage());
}@ExceptionHandler(value = Exception.class)
public Result handler(Exception e) {
log.error(“数据服务接口异常!”, e);
return Result.error(“请求异常,请联系管理员!”);
}
}