当前位置: 首页 > news >正文

智能云图库-1-项目初始化

项目中的异常处理

自定义异常

在exception包下新建错误码枚举类:

@Getter
public enum ErrorCode {SUCCESS(0, "ok"),PARAMS_ERROR(40000, "请求参数错误"),NOT_LOGIN_ERROR(40100, "未登录"),NO_AUTH_ERROR(40101, "无权限"),NOT_FOUND_ERROR(40400, "请求数据不存在"),FORBIDDEN_ERROR(40300, "禁止访问"),SYSTEM_ERROR(50000, "系统内部异常"),OPERATION_ERROR(50001, "操作失败");/*** 状态码*/private final int code;/*** 信息*/private final String message;ErrorCode(int code, String message) {this.code = code;this.message = message;}}

一般不建议直接抛出Java 内置的 RuntimeException,而是自定义一个业务异常,和内置的异常类区分开,便于定制化输出错误信息:知道是业务异常还是系统异常

@Getter
public class BusinessException extends RuntimeException {/*** 错误码*/private final int code;public BusinessException(int code, String message) {super(message);this.code = code;}public BusinessException(ErrorCode errorCode) {super(errorCode.getMessage());this.code = errorCode.getCode();}public BusinessException(ErrorCode errorCode, String message) {super(message);this.code = errorCode.getCode();}}

为了更方便地根据情况抛出异常,可以封装一个ThrowUtils,类似断言类,简化抛异常的代码:

public class ThrowUtils {/*** 条件成立则抛异常** @param condition        条件* @param runtimeException 异常*/public static void throwIf(boolean condition, RuntimeException runtimeException) {if (condition) {throw runtimeException;}}/*** 条件成立则抛异常** @param condition 条件* @param errorCode 错误码*/public static void throwIf(boolean condition, ErrorCode errorCode) {throwIf(condition, new BusinessException(errorCode));}/*** 条件成立则抛异常** @param condition 条件* @param errorCode 错误码* @param message   错误信息*/public static void throwIf(boolean condition, ErrorCode errorCode, String message) {throwIf(condition, new BusinessException(errorCode, message));}
}

比如:

// 校验参数String searchText = pictureUploadByBatchRequest.getSearchText();Integer count = pictureUploadByBatchRequest.getCount();ThrowUtils.throwIf(count > 30, ErrorCode.PARAMS_ERROR, "最多 30 条");

响应包装类​

一般情况下,每个后端接口都要返回调用码、数据、调用信息等,前端可以根据这些信息进行相应的处理。

我们可以封装统一的响应结果类,便于前端统一获取这些信息。

通用响应类:

@Data
public class BaseResponse<T> implements Serializable {private int code;private T data;private String message;public BaseResponse(int code, T data, String message) {this.code = code;this.data = data;this.message = message;}public BaseResponse(int code, T data) {this(code, data, "");}public BaseResponse(ErrorCode errorCode) {this(errorCode.getCode(), null, errorCode.getMessage());}
}

但之后每次接口返回值时,都要手动 new一个BaseResponse 对象并传入参数,比较麻烦,我们可以新建一个工具类,提供成功调用和失败调用的方法,支持灵活地传参,简化调用。

public class ResultUtils {/*** 成功** @param data 数据* @param <T>  数据类型* @return 响应*/public static <T> BaseResponse<T> success(T data) {return new BaseResponse<>(0, data, "ok");}/*** 失败** @param errorCode 错误码* @return 响应*/public static BaseResponse<?> error(ErrorCode errorCode) {return new BaseResponse<>(errorCode);}/*** 失败** @param code    错误码* @param message 错误信息* @return 响应*/public static BaseResponse<?> error(int code, String message) {return new BaseResponse<>(code, null, message);}/*** 失败** @param errorCode 错误码* @return 响应*/public static BaseResponse<?> error(ErrorCode errorCode, String message) {return new BaseResponse<>(errorCode.getCode(), null, message);}
}

全局异常处理器​

为了防止意料之外的异常,利用AOP 切面全局对业务异常和 RuntimeException进行捕获:

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {@ExceptionHandler(BusinessException.class)public BaseResponse<?> businessExceptionHandler(BusinessException e) {log.error("BusinessException", e);return ResultUtils.error(e.getCode(), e.getMessage());}@ExceptionHandler(RuntimeException.class)public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {log.error("RuntimeException", e);return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");}
}

全局跨域配置​

跨域是指浏览器访问的URL(前端地址)和后端接口地址的域名(或端口号)不一致导致的,浏览器为了安全,默认禁止跨域请求访问。

为了开发调试方便,我们可以通过全局跨域配置,让整个项目所有的接口支持跨域,解决跨域报错。

新建config 包,用于存放所有的配置相关代码。全局跨域配置代码如下:

@Configuration
public class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {// 覆盖所有请求registry.addMapping("/**")// 允许发送 Cookie.allowCredentials(true)// 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突).allowedOriginPatterns("*").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("*").exposedHeaders("*");}
}

相关文章:

  • 医学成像中的对比语言-图像预训练模型(CLIP):一项综述|文献速递-深度学习医疗AI最新文献
  • Multisim使用教程详尽版--(2025最新版)
  • 数据库索引深度解析:原理、类型与高效使用实践
  • 图像处理有哪些核心技术?技术发展现状如何?
  • 【信息安全】黑芝麻A1000芯片安全启动方案
  • Android Studio 日志系统详解
  • [OS] vDSO + vvar(频繁调用的处理) | 存储:寄存器(高效)和栈(空间大)| ELF标准包装规范(加速程序加载)
  • springboot调用python文件,在ubuntu上部署,踩坑之旅(已部署成功)
  • Redis的Key的过期策略
  • 【笔试强训day19】
  • Spring 事件机制与观察者模式的深度解析
  • 《 C++ 点滴漫谈: 三十三 》当函数成为参数:解密 C++ 回调函数的全部姿势
  • vue2实现在屏幕中有一个小机器人可以随意移动
  • 数字化引擎再升级:小匠物联十周年庆典与全链路创新实践
  • Ubuntu 22.04安装MySQL : Qwen2.5 模型对话数据收集与微调教程
  • 探索Linux/Unix 系统中进程与文件的深层关系
  • 在 Ubuntu 22.04.1 LTS上搭建FTP服务的详细指南
  • 【从零实现高并发内存池】内存池整体框架设计 及 thread cache实现
  • Leetcode 去除重复字母
  • LeetCode算法题(Go语言实现)_45
  • 理财经理泄露客户信息案进展:湖南省检受理申诉,证监会交由地方监管局办理
  • 竞彩湃|霍芬海姆看到保级曙光,AC米兰专注于意大利杯
  • 98年服装“厂二代”:关税压力下,我仍相信中国供应链|湃客Talk
  • 华为鸿蒙电脑正式亮相,应用生态系统能否挑战Windows?
  • 如此城市|上海老邬:《爱情神话》就是我生活的一部分
  • 长安汽车辟谣作为二级企业并入东风集团:将追究相关方责任