From 40b2b1121fbcce2692cda44b6b2ce3723a80e55a Mon Sep 17 00:00:00 2001 From: zhangli <123879394@qq.com> Date: Wed, 9 Jul 2025 23:34:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E5=92=8C=E8=BF=94=E5=9B=9E=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sczx/store/common/IApiCode.java | 12 ++ .../java/com/sczx/store/common/Result.java | 122 ++++++++++++++++++ .../sczx/store/common/enums/ApiErrorCode.java | 55 ++++++++ .../controller/StoreClientController.java | 8 +- .../sczx/store/exception/BizException.java | 85 ++++++++++++ .../exception/GlobalExceptionHandler.java | 38 +++--- .../sczx/store/exception/InnerException.java | 86 ++++++++++++ 7 files changed, 383 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/sczx/store/common/IApiCode.java create mode 100644 src/main/java/com/sczx/store/common/Result.java create mode 100644 src/main/java/com/sczx/store/common/enums/ApiErrorCode.java create mode 100644 src/main/java/com/sczx/store/exception/BizException.java create mode 100644 src/main/java/com/sczx/store/exception/InnerException.java diff --git a/src/main/java/com/sczx/store/common/IApiCode.java b/src/main/java/com/sczx/store/common/IApiCode.java new file mode 100644 index 0000000..90009ee --- /dev/null +++ b/src/main/java/com/sczx/store/common/IApiCode.java @@ -0,0 +1,12 @@ +package com.sczx.store.common; + +/** + * @Author: 张黎 + * @Date: 2025/07/09/20:20 + * @Description: + */ +public interface IApiCode { + String getCode(); + + String getMsg(); +} diff --git a/src/main/java/com/sczx/store/common/Result.java b/src/main/java/com/sczx/store/common/Result.java new file mode 100644 index 0000000..cc936a1 --- /dev/null +++ b/src/main/java/com/sczx/store/common/Result.java @@ -0,0 +1,122 @@ +package com.sczx.store.common; + + + +import com.sczx.store.common.enums.ApiErrorCode; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Builder; +import lombok.Data; +import org.apache.commons.lang.StringUtils; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * @Author: 张黎 + * @Date: 2025/07/09/20:25 + * @Description: + */ +@ApiModel(value = "公共返回结果") +@Builder +@Data +public class Result implements Serializable { + private static final long serialVersionUID = 1331134667810352183L; + @ApiModelProperty(value = "成功标识") + private boolean success; + @ApiModelProperty(value = "返回编码") + private String code; + @ApiModelProperty(value = "返回信息说明") + private String msg; + private String issue; + @ApiModelProperty(value = "返回数据") + private T data; + + public Result(boolean success, String code, String msg, String issue, T data) { + this.success = success; + this.code = code; + this.msg = msg; + this.issue = issue; + this.data = data; + } + + public static Result result(IApiCode apiCode) { + return result(apiCode, (Object)null); + } + + public static Result result(IApiCode apiCode, Object data) { + return result(apiCode, apiCode.getMsg(), data); + } + + public static Result result(IApiCode apiCode, String issue, Object data) { + return result((IApiCode)apiCode, (String)null, issue, data); + } + + public static Result result(IApiCode apiCode, String msg, String issue, Object data) { + String message = apiCode.getMsg(); + if (StringUtils.isNotBlank(msg)) { + message = msg; + } + + return result(apiCode.getCode(), message, issue, data); + } + + public static Result result(String code, String msg, String issue, Object data) { + return builder().code(code).success("0".equals(code)).msg(msg).issue(issue).data(data).build(); + } + + public static Result ok() { + return ok((Object)null); + } + + public static Result ok(Object data) { + return result(ApiErrorCode.SUCCESS, data); + } + + public static Result ok(String key, Object value) { + Map map = new HashMap(1); + map.put(key, value); + return ok(map); + } + + public static Result fail(String msg) { + return result((IApiCode)ApiErrorCode.FAIL, msg, msg, (Object)null); + } + + public static Result fail(IApiCode apiCode) { + return result(apiCode, (Object)null); + } + + public static Result fail(IApiCode apiCode, String issue) { + return result(apiCode, issue, (Object)null); + } + + public static Result fail(String code, String msg) { + return result((String)code, msg, msg, (Object)null); + } + + public static Result fail(String code, String msg, String issue) { + return result((String)code, msg, issue, (Object)null); + } + + public static Result fail(String code, String msg, String issue, Object data) { + return result(code, msg, issue, data); + } + + public static Result fail(IApiCode apiCode, String issue, Object data) { + if (ApiErrorCode.SUCCESS == apiCode) { + throw new RuntimeException("失败结果状态码不能为" + ApiErrorCode.SUCCESS.getCode()); + } else { + return result(apiCode, issue, data); + } + } + + public static Result fail(String key, Object value) { + Map map = new HashMap(); + map.put(key, value); + return result(ApiErrorCode.FAIL, map); + } + public Result() { + } +} diff --git a/src/main/java/com/sczx/store/common/enums/ApiErrorCode.java b/src/main/java/com/sczx/store/common/enums/ApiErrorCode.java new file mode 100644 index 0000000..2175e28 --- /dev/null +++ b/src/main/java/com/sczx/store/common/enums/ApiErrorCode.java @@ -0,0 +1,55 @@ +package com.sczx.store.common.enums; + + +import com.sczx.store.common.IApiCode; + +/** + * @Author: 张黎 + * @Date: 2025/07/09/20:22 + * @Description: + */ +public enum ApiErrorCode implements IApiCode { + SUCCESS("0", "操作成功"), + ARG_ERROR("401000", "参数错误"), + NOT_PERMISSION("401001", "没有权限"), + HTTP_MEDIA_TYPE_NOT_SUPPORTED_ERROR("401002", "media类型出错"), + HTTP_METHOD_NOT_ALLOW_ERROR("401003", "http请求method错误"), + BODY_NOT_MATCH("401004", "请求的数据格式不符!"), + NOT_FOUND("404000", "你请求的路径不存在"), + UNAUTHORIZED("404001", "非法访问"), + FAIL("500000", "操作失败"), + INNER_ERROR("500001", "服务器内部异常"); + + private final String code; + private final String msg; + + private ApiErrorCode(String code, String msg) { + this.code = code; + this.msg = msg; + } + + public static ApiErrorCode getApiCode(String code) { + ApiErrorCode[] ecs = values(); + ApiErrorCode[] var2 = ecs; + int var3 = ecs.length; + + for(int var4 = 0; var4 < var3; ++var4) { + ApiErrorCode ec = var2[var4]; + if (ec.getCode().equals(code)) { + return ec; + } + } + + return SUCCESS; + } + + @Override + public String getCode() { + return this.code; + } + + @Override + public String getMsg() { + return this.msg; + } +} diff --git a/src/main/java/com/sczx/store/controller/StoreClientController.java b/src/main/java/com/sczx/store/controller/StoreClientController.java index 55fc8bc..a11dfcc 100644 --- a/src/main/java/com/sczx/store/controller/StoreClientController.java +++ b/src/main/java/com/sczx/store/controller/StoreClientController.java @@ -2,17 +2,15 @@ package com.sczx.store.controller; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.sczx.store.common.Result; import com.sczx.store.dto.CompanyStoreDTO; import com.sczx.store.dto.req.StoreDistanceReq; import com.sczx.store.service.StoreService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - /** *

* 门店 前端控制器 @@ -31,7 +29,7 @@ public class StoreClientController { @ApiOperation(value = "根据经纬度查询门店信息") @PostMapping("/listStoresByBBoxWithDistance") - ResponseEntity> listStoresByBBoxWithDistance(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody StoreDistanceReq storeDistanceReq){ - return ResponseEntity.ok(storeService.listStoresByBBoxWithDistance(storeDistanceReq, pageNo, pageSize)); + Result> listStoresByBBoxWithDistance(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody StoreDistanceReq storeDistanceReq){ + return Result.ok(storeService.listStoresByBBoxWithDistance(storeDistanceReq, pageNo, pageSize)); } } diff --git a/src/main/java/com/sczx/store/exception/BizException.java b/src/main/java/com/sczx/store/exception/BizException.java new file mode 100644 index 0000000..38c2c73 --- /dev/null +++ b/src/main/java/com/sczx/store/exception/BizException.java @@ -0,0 +1,85 @@ +package com.sczx.store.exception; + + +import com.sczx.store.common.IApiCode; +import com.sczx.store.common.enums.ApiErrorCode; + +/** + * + * @author zhangli + */ +public class BizException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + protected String errorCode; + /** + * 错误信息 + */ + protected String errorMsg; + + public BizException() { + super(); + } + + public BizException(IApiCode apiCode) { + super(apiCode.getCode()); + this.errorCode = apiCode.getCode(); + this.errorMsg = apiCode.getMsg(); + } + + public BizException(IApiCode apiCode, Throwable cause) { + super(apiCode.getCode(), cause); + this.errorCode = apiCode.getCode(); + this.errorMsg = apiCode.getMsg(); + } + + public BizException(String errorMsg) { + super(errorMsg); + this.errorCode = ApiErrorCode.FAIL.getCode(); + this.errorMsg = errorMsg; + } + + public BizException(String errorCode, String errorMsg) { + super(errorCode); + this.errorCode = errorCode; + this.errorMsg = errorMsg; + } + + public BizException(String errorCode, String errorMsg, Throwable cause) { + super(errorCode, cause); + this.errorCode = errorCode; + this.errorMsg = errorMsg; + } + + + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } + + @Override + public String getMessage() { + return errorMsg; + } + + @Override + public Throwable fillInStackTrace() { + return this; + } + +} diff --git a/src/main/java/com/sczx/store/exception/GlobalExceptionHandler.java b/src/main/java/com/sczx/store/exception/GlobalExceptionHandler.java index 66cb231..4935160 100644 --- a/src/main/java/com/sczx/store/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/sczx/store/exception/GlobalExceptionHandler.java @@ -1,28 +1,17 @@ package com.sczx.store.exception; +import com.sczx.store.common.Result; import lombok.extern.slf4j.Slf4j; - -import org.springframework.http.ResponseEntity; - import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; -import java.util.Map; + /** - * 应用模块名称

- *

- * 代码描述

- *

- * Copyright: Copyright (C) 2021 CD Finance Management Co., Ltd. All rights reserved.

- *

- * Company: 中和农信项目管理有限公司

- * - * @author zhonghui - * @since 2021/9/13 2:46 PM + * @author zhangli */ @Slf4j @ControllerAdvice @@ -36,12 +25,25 @@ public class GlobalExceptionHandler { * @param e * @return */ - @ExceptionHandler(value = Exception.class) + @ExceptionHandler(value = BizException.class) @ResponseBody - public ResponseEntity bizExceptionHandler(HttpServletRequest req, Exception e) { - log.error("服务器异常!原因是:{}",e.getMessage(), e); - return ResponseEntity.noContent().build(); + public Result bizExceptionHandler(HttpServletRequest req, BizException e) { + log.warn("发生业务异常!原因是:{}", e.getErrorMsg(), e); + return Result.fail(e.getErrorCode(), e.getErrorMsg()); } + @ExceptionHandler(value = InnerException.class) + @ResponseBody + public Result innerExceptionHandler(HttpServletRequest req, InnerException e) { + log.error("发生服务内部异常!原因是:{}", e.getErrorMsg(), e); + return Result.fail(e.getErrorCode(), e.getErrorMsg()); + } + + @ExceptionHandler(value = Exception.class) + @ResponseBody + public Result exceptionHandler(HttpServletRequest req, Exception e) { + log.error("意料外异常!原因是:{}", e.getMessage(), e); + return Result.fail("99999", "系统内部异常"); + } } diff --git a/src/main/java/com/sczx/store/exception/InnerException.java b/src/main/java/com/sczx/store/exception/InnerException.java new file mode 100644 index 0000000..4c6d0be --- /dev/null +++ b/src/main/java/com/sczx/store/exception/InnerException.java @@ -0,0 +1,86 @@ +package com.sczx.store.exception; + + +import com.sczx.store.common.IApiCode; +import com.sczx.store.common.enums.ApiErrorCode; + +/** + * + * @author zhangli + + */ +public class InnerException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + protected String errorCode; + /** + * 错误信息 + */ + protected String errorMsg; + + public InnerException() { + super(); + } + + public InnerException(IApiCode apiCode) { + super(apiCode.getCode()); + this.errorCode = apiCode.getCode(); + this.errorMsg = apiCode.getMsg(); + } + + public InnerException(IApiCode apiCode, Throwable cause) { + super(apiCode.getCode(), cause); + this.errorCode = apiCode.getCode(); + this.errorMsg = apiCode.getMsg(); + } + + public InnerException(String errorMsg) { + super(errorMsg); + this.errorCode = ApiErrorCode.INNER_ERROR.getCode(); + this.errorMsg = errorMsg; + } + + public InnerException(String errorCode, String errorMsg) { + super(errorCode); + this.errorCode = errorCode; + this.errorMsg = errorMsg; + } + + public InnerException(String errorCode, String errorMsg, Throwable cause) { + super(errorCode, cause); + this.errorCode = errorCode; + this.errorMsg = errorMsg; + } + + + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } + + @Override + public String getMessage() { + return errorMsg; + } + + @Override + public Throwable fillInStackTrace() { + return this; + } + +}