项目
博客
文档
归档
资源链接
关于我
项目
博客
文档
归档
资源链接
关于我
05| 统一接口响应协议和响应工具类封装
2024-07-30
·
·
原创
·
·
本文共 654个字,预计阅读需要 3分钟。
#### 设置自定义的错误响应BizCodeEnum或者ErrorEnum 路径 `nla-common\src\main\java\cn\nla\common\enums\BizCodeEnum.java` ```java @Getter @AllArgsConstructor public enum BizCodeEnum { /** * 通用操作码 */ OPS_REPEAT(110001,"重复操作"), /** * 系统相关 */ SYS_LOGIN(210001,"登录过期,请重新登录"), SYS_NO_TOKEN(210002,"token不存在,请重新登录"), SYS_NO_USER(210003,"loginUser对象为空"), /** * 购物车 */ CART_FAIL(220001,"添加购物车失败"), /** *验证码 */ CODE_TO_ERROR(240001,"接收号码不合规"), CODE_LIMITED(240002,"验证码发送过快"), CODE_ERROR(240003,"验证码错误"), CODE_CAPTCHA_ERROR(240101,"图形验证码错误"), /** * 账号 */ ACCOUNT_REPEAT(250001,"账号已经存在"), ACCOUNT_UNREGISTER(250002,"账号不存在"), ACCOUNT_PWD_ERROR(250003,"账号或者密码错误"), ACCOUNT_UNLOGIN(250004,"账号未登录"), /** * 优惠券 */ COUPON_CONDITION_ERROR(270001,"优惠券条件错误"), COUPON_UNAVAILABLE(270002,"没有可用的优惠券"), COUPON_NO_EXITS(270003,"优惠券不存在"), COUPON_NO_STOCK(270005,"优惠券库存不足"), COUPON_OUT_OF_LIMIT(270006,"优惠券领取超过限制次数"), COUPON_OUT_OF_TIME(270407,"优惠券不在领取时间范围"), COUPON_GET_FAIL(270407,"优惠券领取失败"), COUPON_RECORD_LOCK_FAIL(270409,"优惠券锁定失败"), /** * 订单 */ ORDER_CONFIRM_COUPON_FAIL(280001,"创建订单-优惠券使用失败,不满足价格条件"), ORDER_CONFIRM_PRICE_FAIL(280002,"创建订单-验价失败"), ORDER_CONFIRM_LOCK_PRODUCT_FAIL(280003,"创建订单-商品库存不足锁定失败"), ORDER_CONFIRM_ADD_STOCK_TASK_FAIL(280004,"创建订单-新增商品库存锁定任务"), ORDER_CONFIRM_TOKEN_NOT_EXIST(280008,"订单令牌缺少"), ORDER_CONFIRM_TOKEN_EQUAL_FAIL(280009,"订单令牌不正确"), ORDER_CONFIRM_NOT_EXIST(280010,"订单不存在"), ORDER_CONFIRM_CART_ITEM_NOT_EXIST(280011,"购物车商品项不存在"), /** * 收货地址 */ ADDRESS_ADD_FAIL(290001,"新增收货地址失败"), ADDRESS_DEL_FAIL(290002,"删除收货地址失败"), ADDRESS_NO_EXITS(290003,"地址不存在"), /** * 支付 */ PAY_ORDER_FAIL(300001,"创建支付订单失败"), PAY_ORDER_CALLBACK_SIGN_FAIL(300002,"支付订单回调验证签失败"), PAY_ORDER_CALLBACK_NOT_SUCCESS(300003,"创建支付订单失败"), PAY_ORDER_NOT_EXIST(300005,"订单不存在"), PAY_ORDER_STATE_ERROR(300006,"订单状态不正常"), PAY_ORDER_PAY_TIMEOUT(300007,"订单支付超时"), /** * 流控操作 */ CONTROL_FLOW(500101,"限流控制"), CONTROL_DEGRADE(500201,"降级控制"), CONTROL_AUTH(500301,"认证控制"), /** * 文件相关 */ FILE_UPLOAD_USER_IMG_FAIL(600101,"用户头像文件上传失败"); private int code; private String message; } ``` #### 设置统一的响应体JsonData或者R 路径: `nla-common\src\main\java\cn\nla\common\util\JsonData.java` ```java @Data @AllArgsConstructor @NoArgsConstructor public class JsonData { public static final Integer SUCCESS = 0; public static final Integer FAIL = -1; /** * 状态码 0 表示成功 */ private Integer code; /** * 数据 */ private Object data; /** * 描述 */ private String msg; /** * 成功,不传入数据 */ public static JsonData buildSuccess() { return new JsonData(SUCCESS, null, null); } /** * 成功,传入数据 */ public static JsonData buildSuccess(Object data) { return new JsonData(SUCCESS, data, null); } /** * 失败,传入描述信息 */ public static JsonData buildError(String msg) { return new JsonData(FAIL, null, msg); } /** * 自定义状态码和错误信息 */ public static JsonData buildCodeAndMsg(int code, String msg) { return new JsonData(code, null, msg); } /** * 传入枚举,返回信息 */ public static JsonData buildResult(BizCodeEnum codeEnum) { return JsonData.buildCodeAndMsg(codeEnum.getCode(), codeEnum.getMessage()); } } ``` 更规范的顶用全局响应对象 ```java @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(value = "【Response】统一响应对象", description = "统一响应的数据结构") public class Result
{ public static final String SUCCESS_MSG = "成功"; public static final String FAIL_MSG = "失败"; public static final Integer SUCCESS = 200; public static final Integer FAIL = 500; /** * 状态码 200 表示成功 */ @ApiModelProperty(value = "状态码: 200-成功;其他-失败", required = true) private Integer code; /** * 数据 */ @ApiModelProperty(value = "响应数据", required = true) private T data; /** * 描述 */ @ApiModelProperty(value = "响应描述", required = true) private String msg; public static
Result
buildSuccess() { return Result.buildSuccess(null); } public static
Result
buildSuccess(T data) { return new Result<>(SUCCESS, data, SUCCESS_MSG); } public static
Result
buildError() { return Result.buildError(FAIL_MSG); } public static
Result
buildError(String msg) { return new Result<>(FAIL, null, msg); } public static
Result
build(int code, String msg) { return new Result<>(code, null, msg); } } ``` ### 自定义全局异常+处理器handler开发 #### 自定义全局异常类 路径:`nla-common\src\main\java\cn\nla\common\exception\BizException.java` ```java @Getter @Setter public class BizException extends RuntimeException { private int code; private String msg; public BizException(int code, String msg){ super(msg); this.code = code; this.msg = msg; } public BizException(BizCodeEnum bizCodeEnum){ super(bizCodeEnum.getMessage()); this.code = bizCodeEnum.getCode(); this.msg = bizCodeEnum.getMessage(); } } ``` #### 异常处理器handler 路径:`nla-common\src\main\java\cn\nla\common\exception\CustomExceptionHandler.java` ```java @ControllerAdvice @Slf4j public class CustomExceptionHandler { @ExceptionHandler(value = Exception.class) @ResponseBody public JsonData handle(Exception e){ //是不是自定义异常 if(e instanceof BizException){ BizException bizException = (BizException) e; log.error("[业务异常]",e); return JsonData.buildCodeAndMsg(bizException.getCode(),bizException.getMsg()); }else{ log.error("[系统异常]",e); return JsonData.buildError("全局异常,未知错误"); } } } ```