From 8927b3323ffe52a836044636d6547092932d90d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 8 Jun 2025 18:54:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=AE=B0=E5=BD=95=E6=8E=A5=E5=8F=A3=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/zhangdeman/context/Response.java | 43 +++++++++++++------ .../cn/zhangdeman/context/RuntimeContext.java | 3 +- .../handler/GlobalExceptionHandler.java | 13 ++++-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/main/java/cn/zhangdeman/context/Response.java b/src/main/java/cn/zhangdeman/context/Response.java index 22713a5..e526e83 100644 --- a/src/main/java/cn/zhangdeman/context/Response.java +++ b/src/main/java/cn/zhangdeman/context/Response.java @@ -2,6 +2,7 @@ package cn.zhangdeman.context; import cn.zhangdeman.consts.RecordField; import cn.zhangdeman.exception.CustomException; +import cn.zhangdeman.logger.LoggerInstance; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -45,7 +46,11 @@ public class Response implements Serializable { @JsonProperty(RecordField.RESPONSE_COOKIE) private Map cookie = new HashMap<>(); // 响应cookie @JsonIgnore + private String contentType = "application/json;charset=utf-8"; + @JsonIgnore private RuntimeContext runtimeContext; + @JsonIgnore + private Boolean handleSuccess; // 是否处理成功响应 // 响应实例 public Response(RuntimeContext runtimeContext) { @@ -90,48 +95,60 @@ public class Response implements Serializable { } // 请求成功, 200 状态码 - public Response success(Data data) { - return success(HttpStatus.OK, data); + public Response success(RuntimeContext runtimeContext, Data data) { + return success(runtimeContext, HttpStatus.OK, data); } // 请求成功, 自定义http状态码 - public Response success(HttpStatus httpStatus, Data data) { + public Response success(RuntimeContext runtimeContext, HttpStatus httpStatus, Data data) { + setHandleSuccess(true); if (null == getCode()) { setCode("0"); } if (null == message) { setMessage("请求成功"); } - return any(httpStatus, getCode(), getMessage(), data); + return any(runtimeContext, httpStatus, getCode(), getMessage(), data); } // 请求失败, 返回200 - public Response failure(String code, String message, Object errorDetail) { - return failure(HttpStatus.OK, code, message, errorDetail); + public Response failure(RuntimeContext runtimeContext, String code, String message, Object errorDetail) { + return failure(runtimeContext, HttpStatus.OK, code, message, errorDetail); } - public Response failure(HttpStatus httpStatus, String code, String message, Object errorDetail) { + public Response failure(RuntimeContext runtimeContext, HttpStatus httpStatus, String code, String message, Object errorDetail) { + setHandleSuccess(false); setErrorDetail(errorDetail); - return any(httpStatus, code, message, null); + return any(runtimeContext, httpStatus, code, message, null); } - public Response failure(CustomException customException) { - return failure(HttpStatus.OK, customException); + public Response failure(RuntimeContext runtimeContext, CustomException customException) { + return failure(runtimeContext, HttpStatus.OK, customException); } - public Response failure(HttpStatus httpStatus, CustomException customException) { + public Response failure(RuntimeContext runtimeContext, HttpStatus httpStatus, CustomException customException) { + setHandleSuccess(false); setErrorDetail(customException.getStackTrace()); // 异常堆栈 - return any(httpStatus, customException.getCode(), customException.getMessage(), null); + return any(runtimeContext, httpStatus, customException.getCode(), customException.getMessage(), null); } // 任意姿势的响应 - public Response any(HttpStatus httpStatus, String code, String message, Data data) { + public Response any(RuntimeContext runtimeContext, HttpStatus httpStatus, String code, String message, Data data) { + this.runtimeContext = runtimeContext; setHttpStatus(httpStatus); + addHeader("Content-Type", getContentType()); // header追加响应类型 setHttpCode(httpStatus.value()); setCode(code); setMessage(message); setData(data); setResponseHeaderAndCookie(); + this.runtimeContext.setResponse((Response)this); + // 记录日志 + if (getHandleSuccess()) { + LoggerInstance.REQUEST_OUTPUT_LOGGER.info("接口处理成功"); + } else { + LoggerInstance.REQUEST_OUTPUT_LOGGER.error("接口处理失败"); + } return this; } // 序列化 diff --git a/src/main/java/cn/zhangdeman/context/RuntimeContext.java b/src/main/java/cn/zhangdeman/context/RuntimeContext.java index bdd754a..dbfa8f4 100644 --- a/src/main/java/cn/zhangdeman/context/RuntimeContext.java +++ b/src/main/java/cn/zhangdeman/context/RuntimeContext.java @@ -37,7 +37,8 @@ public class RuntimeContext implements Serializable { @JsonIgnore private HttpServletRequest httpServletRequest;// 请求实例 @JsonIgnore - private HttpServletResponse httpServletResponse; // 相应实例 + private HttpServletResponse httpServletResponse; // 响应实例 + // 序列化 @Override diff --git a/src/main/java/cn/zhangdeman/exception/handler/GlobalExceptionHandler.java b/src/main/java/cn/zhangdeman/exception/handler/GlobalExceptionHandler.java index 825a160..aea30e6 100644 --- a/src/main/java/cn/zhangdeman/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/cn/zhangdeman/exception/handler/GlobalExceptionHandler.java @@ -39,7 +39,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(cn.zhangdeman.exception.CustomException.class) public ResponseEntity handleBusinessException(CustomException customException, HttpServletRequest httpServletRequest) { RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest); - Response response = new Response<>(runtimeContext).failure(customException); + Response response = new Response<>(runtimeContext).failure(runtimeContext, customException); return getResponseEntity(response); } @@ -62,16 +62,23 @@ public class GlobalExceptionHandler { }); RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest); - Response response = new Response<>(runtimeContext).failure("400", String.join(" - ", errors), errDetailList); + Response response = new Response<>(runtimeContext).failure(runtimeContext, "400", String.join(" - ", errors), errDetailList); return getResponseEntity(response); } + // 处理没有此方法的异常 + @ExceptionHandler(java.lang.NoSuchMethodError.class) + public ResponseEntity handleNoSuchMethodException(Exception ex, HttpServletRequest httpServletRequest) { + RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest); + Response response = new Response<>(runtimeContext).failure(runtimeContext,"500", ex.getMessage(), ex.getCause()); + return getResponseEntity(response); + } // 处理其他未捕获的异常 @ExceptionHandler(Exception.class) public ResponseEntity handleGlobalException(Exception ex, HttpServletRequest httpServletRequest) { RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest); - Response response = new Response<>(runtimeContext).failure("500", ex.getMessage(), ex.getCause()); + Response response = new Response<>(runtimeContext).failure(runtimeContext,"500", ex.getMessage(), ex.getCause()); return getResponseEntity(response); } }