From 2505fc8333e1a94ab3ff29463b6380674f5374e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=98=E5=90=8E=E9=92=B0?= Date: Wed, 21 Aug 2024 22:20:41 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=BB=9F=E4=B8=80=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E5=BD=92=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ponseCodeStatus.java => ResponseCode.java} | 2 +- ...eStatusEnum.java => ResponseCodeEnum.java} | 9 ++- .../model/response/ResponseHelper.java | 10 +-- .../framework/model/response/RestResult.java | 6 +- .../filter/ApplicationBasicErrorFilter.java | 4 +- .../web/aspectj/NoRepeatSubmitAspectj.java | 6 +- .../web/exception/WebExceptionHandler.java | 81 ++++++++++++------- .../web/exception/WebServiceException.java | 10 +-- 8 files changed, 79 insertions(+), 49 deletions(-) rename base-framework-common/src/main/java/com/fuhouyu/framework/model/response/{ResponseCodeStatus.java => ResponseCode.java} (96%) rename base-framework-common/src/main/java/com/fuhouyu/framework/model/response/{ResponseCodeStatusEnum.java => ResponseCodeEnum.java} (88%) diff --git a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatus.java b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCode.java similarity index 96% rename from base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatus.java rename to base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCode.java index b8049d0..88ec4f4 100644 --- a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatus.java +++ b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCode.java @@ -24,7 +24,7 @@ * @author fuhouyu * @since 2024/8/13 17:39 */ -public interface ResponseCodeStatus { +public interface ResponseCode { /** * 获取响应码 diff --git a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatusEnum.java b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeEnum.java similarity index 88% rename from base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatusEnum.java rename to base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeEnum.java index b2e2179..4768dc2 100644 --- a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeStatusEnum.java +++ b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseCodeEnum.java @@ -24,7 +24,7 @@ * @author fuhouyu * @since 2024/8/13 17:40 */ -public enum ResponseCodeStatusEnum implements ResponseCodeStatus { +public enum ResponseCodeEnum implements ResponseCode { // 200 @@ -54,6 +54,11 @@ public enum ResponseCodeStatusEnum implements ResponseCodeStatus { */ METHOD_NOT_ALLOWED(405, "不支持的方法"), + /** + * 不支持的媒体类型 + */ + NOT_SUPPORT_MEDIA_TYPE(415, "不支持的媒体类型"), + /** * 服务内部错误 */ @@ -66,7 +71,7 @@ public enum ResponseCodeStatusEnum implements ResponseCodeStatus { private final String message; - ResponseCodeStatusEnum(int code, String message) { + ResponseCodeEnum(int code, String message) { this.code = code; this.message = message; } diff --git a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseHelper.java b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseHelper.java index f8a4e06..7a0b4ea 100644 --- a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseHelper.java +++ b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/ResponseHelper.java @@ -32,7 +32,7 @@ private ResponseHelper() { public static RestResult success(T data) { return RestResult.builder() - .withResponseCodeStatus(ResponseCodeStatusEnum.SUCCESS) + .withResponseCodeStatus(ResponseCodeEnum.SUCCESS) .withData(data).build(); } @@ -47,11 +47,11 @@ public static RestResult failed(int code, String message) { .build(); } - public static RestResult failed(ResponseCodeStatus responseCodeStatus) { - return failed(responseCodeStatus.getCode(), responseCodeStatus.getMessage()); + public static RestResult failed(ResponseCode responseCode) { + return failed(responseCode.getCode(), responseCode.getMessage()); } - public static RestResult failed(ResponseCodeStatus responseCodeStatus, String message) { - return failed(responseCodeStatus.getCode(), message); + public static RestResult failed(ResponseCode responseCode, String message) { + return failed(responseCode.getCode(), message); } } diff --git a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/RestResult.java b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/RestResult.java index 3af3b73..ea51916 100644 --- a/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/RestResult.java +++ b/base-framework-common/src/main/java/com/fuhouyu/framework/model/response/RestResult.java @@ -120,9 +120,9 @@ public static final class BaseResponseBuilder { private BaseResponseBuilder() { } - public BaseResponseBuilder withResponseCodeStatus(ResponseCodeStatus responseCodeStatus) { - this.code = responseCodeStatus.getCode(); - this.message = responseCodeStatus.getMessage(); + public BaseResponseBuilder withResponseCodeStatus(ResponseCode responseCode) { + this.code = responseCode.getCode(); + this.message = responseCode.getMessage(); return this; } diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java index 46cffe7..37c9915 100644 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java @@ -16,7 +16,7 @@ package com.fuhouyu.framework.security.core.filter; -import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum; +import com.fuhouyu.framework.model.response.ResponseCodeEnum; import com.fuhouyu.framework.model.response.ResponseHelper; import com.fuhouyu.framework.model.response.RestResult; import com.fuhouyu.framework.utils.JacksonUtil; @@ -43,7 +43,7 @@ public class ApplicationBasicErrorFilter extends BasicAuthenticationFilter { */ public ApplicationBasicErrorFilter(AuthenticationManager authenticationManager) { super(authenticationManager, (request, response, authException) -> { - RestResult restResult = ResponseHelper.failed(ResponseCodeStatusEnum.NOT_AUTH, authException.getMessage()); + RestResult restResult = ResponseHelper.failed(ResponseCodeEnum.NOT_AUTH, authException.getMessage()); String body = JacksonUtil.writeValueAsString(restResult); try (ServletOutputStream outputStream = response.getOutputStream()) { outputStream.write(body.getBytes(StandardCharsets.UTF_8)); diff --git a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/aspectj/NoRepeatSubmitAspectj.java b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/aspectj/NoRepeatSubmitAspectj.java index f2c6603..bcb506b 100644 --- a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/aspectj/NoRepeatSubmitAspectj.java +++ b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/aspectj/NoRepeatSubmitAspectj.java @@ -17,7 +17,7 @@ package com.fuhouyu.framework.web.aspectj; import com.fuhouyu.framework.cache.service.CacheService; -import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum; +import com.fuhouyu.framework.model.response.ResponseCodeEnum; import com.fuhouyu.framework.web.annotaions.NoRepeatSubmit; import com.fuhouyu.framework.web.constants.FormTokenConstant; import com.fuhouyu.framework.web.exception.WebServiceException; @@ -70,14 +70,14 @@ public Object doAround(ProceedingJoinPoint joinPoint, NoRepeatSubmit noRepeatSub String formTokenHeader = request.getHeader(noRepeatSubmit.headerToken()); if (!StringUtils.hasText(formTokenHeader)) { throw new WebServiceException( - ResponseCodeStatusEnum.INVALID_PARAM, + ResponseCodeEnum.INVALID_PARAM, noRepeatSubmit.message()); } Object formToken = cacheService.get(FormTokenConstant.TOKEN_PREFIX + formTokenHeader); if (Objects.isNull(formToken)) { throw new WebServiceException( - ResponseCodeStatusEnum.INVALID_PARAM, + ResponseCodeEnum.INVALID_PARAM, noRepeatSubmit.message()); } cacheService.delete(FormTokenConstant.TOKEN_PREFIX + formTokenHeader); diff --git a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebExceptionHandler.java b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebExceptionHandler.java index 93bea7e..e7977ec 100644 --- a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebExceptionHandler.java +++ b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebExceptionHandler.java @@ -17,7 +17,7 @@ package com.fuhouyu.framework.web.exception; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fuhouyu.framework.model.response.ResponseCodeStatusEnum; +import com.fuhouyu.framework.model.response.ResponseCodeEnum; import com.fuhouyu.framework.model.response.ResponseHelper; import com.fuhouyu.framework.model.response.RestResult; import com.fuhouyu.framework.utils.JacksonUtil; @@ -26,11 +26,15 @@ import jakarta.validation.ConstraintViolationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.dao.DataAccessException; +import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.FieldError; +import org.springframework.web.HttpMediaTypeException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingRequestValueException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.server.ResponseStatusException; @@ -65,7 +69,7 @@ public class WebExceptionHandler { @ExceptionHandler(Exception.class) public RestResult exceptionHandle(ServletWebRequest request, Exception e) { this.printExceptionLog(e, Exception.class.getSimpleName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.SERVER_ERROR); + return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR); } /** @@ -78,7 +82,7 @@ public RestResult exceptionHandle(ServletWebRequest request, Exception e) @ExceptionHandler(NoResourceFoundException.class) public RestResult noResourceFoundException(ServletWebRequest request, NoResourceFoundException e) { this.printExceptionLog(e, Exception.class.getSimpleName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.NOT_FOUND, + return ResponseHelper.failed(ResponseCodeEnum.NOT_FOUND, String.format("当前访问地址:%s 不存在", e.getMessage().replace("No static resource ", "").trim())); } @@ -92,7 +96,7 @@ public RestResult noResourceFoundException(ServletWebRequest request, NoRe @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public RestResult methodNotSupportException(ServletWebRequest request, HttpRequestMethodNotSupportedException e) { this.printExceptionLog(e, e.getClass().getName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.METHOD_NOT_ALLOWED); + return ResponseHelper.failed(ResponseCodeEnum.METHOD_NOT_ALLOWED); } /** @@ -105,20 +109,7 @@ public RestResult methodNotSupportException(ServletWebRequest request, Htt @ExceptionHandler(ResponseStatusException.class) public RestResult notFoundException(ServletWebRequest request, ResponseStatusException e) { this.printExceptionLog(e, e.getClass().getName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.SERVER_ERROR, e.getMessage()); - } - - /** - * 请求参数异常 - * - * @param request 请求 - * @param e 异常 - * @return 包装后的异常信息 - */ - @ExceptionHandler(MissingRequestValueException.class) - public RestResult parameterException(ServletWebRequest request, MissingRequestValueException e) { - this.printExceptionLog(e, e.getClass().getSimpleName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, e.getMessage()); + return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR, e.getMessage()); } /** @@ -146,24 +137,58 @@ public RestResult methodArgumentNotValidExceptionHandler(ServletWebRequest errorMessageMap.put(fieldError.getField(), fieldError.getDefaultMessage()); } this.printExceptionLog(e, e.getClass().getSimpleName(), request); - return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, JacksonUtil.writeValueAsString(errorMessageMap)); + return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, JacksonUtil.writeValueAsString(errorMessageMap)); } /** - * 请求入参转换异常 + * 参数异常都会在这里进行统一抛出。 * * @param request 请求 - * @param e 请求入参转换异常 - * @return 包装后的异常信息 + * @param e http缺少参数异常 + * @return 包装后的响应 */ - @ExceptionHandler(HttpMessageNotReadableException.class) - public RestResult readableException(ServletWebRequest request, HttpMessageNotReadableException e) { + @ExceptionHandler(value = { + MissingServletRequestParameterException.class, + IllegalArgumentException.class, + HttpMessageNotReadableException.class, + MissingRequestValueException.class, + NumberFormatException.class, + HttpMessageConversionException.class + }) + public RestResult handleHttpMediaTypeException(ServletWebRequest request, + Exception e) { this.printExceptionLog(e, e.getClass().getSimpleName(), request); - // 直接返回参数异常 - return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, e.getMessage()); + return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, e.getMessage()); + } + + /** + * 任何数据库的异常都会在这里进行抛出 + * + * @param e 异常信息 + * @param request web请求 + * @return 包装后的响应 + */ + @ExceptionHandler(value = DataAccessException.class) + public RestResult handleDataAccessException(ServletWebRequest request, DataAccessException e) { + this.printExceptionLog(e, e.getClass().getSimpleName(), request); + return ResponseHelper.failed(ResponseCodeEnum.SERVER_ERROR, e.getMessage()); } + /** + * 媒体类型错误 + * + * @param request 请求 + * @param e 媒体请求异常 + * @return 包装后的响应 + */ + @ExceptionHandler(HttpMediaTypeException.class) + public RestResult handleHttpMediaTypeException(ServletWebRequest request, HttpMediaTypeException e) { + this.printExceptionLog(e, e.getClass().getSimpleName(), request); + return ResponseHelper.failed(ResponseCodeEnum.NOT_SUPPORT_MEDIA_TYPE, e.getMessage()); + } + + /** * 入参的body体中的参数异常拦截器 如 @RequestBody @Size(min = 1, message= "xxx") List list * @@ -181,7 +206,7 @@ public RestResult constraintViolationException(ServletWebRequest request, sb.append(message).append("\n"); } // 直接返回参数异常 - return ResponseHelper.failed(ResponseCodeStatusEnum.INVALID_PARAM, sb.toString()); + return ResponseHelper.failed(ResponseCodeEnum.INVALID_PARAM, sb.toString()); } /** @@ -197,7 +222,7 @@ public void printExceptionLog(Exception e, String exceptionName, ServletWebReque String requestUrl = request.getRequest().getRequestURI(); LoggerUtil.error(LOGGER, """ - 异常: {} + 异常名称: {} 请求方式: {} 请求路径: {} 请求头参数: {} diff --git a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebServiceException.java b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebServiceException.java index 2bf68f3..e3c3009 100644 --- a/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebServiceException.java +++ b/base-framework-web-starter/src/main/java/com/fuhouyu/framework/web/exception/WebServiceException.java @@ -16,7 +16,7 @@ package com.fuhouyu.framework.web.exception; -import com.fuhouyu.framework.model.response.ResponseCodeStatus; +import com.fuhouyu.framework.model.response.ResponseCode; /** *

@@ -32,15 +32,15 @@ public class WebServiceException extends RuntimeException { private final String message; - private final ResponseCodeStatus responseStatus; + private final ResponseCode responseStatus; - public WebServiceException(ResponseCodeStatus responseStatus) { + public WebServiceException(ResponseCode responseStatus) { this.responseStatus = responseStatus; this.status = responseStatus.getCode(); this.message = responseStatus.getMessage(); } - public WebServiceException(ResponseCodeStatus responseStatus, String errorMessage) { + public WebServiceException(ResponseCode responseStatus, String errorMessage) { this.status = responseStatus.getCode(); this.message = errorMessage; this.responseStatus = responseStatus; @@ -55,7 +55,7 @@ public String getMessage() { return message; } - public ResponseCodeStatus getResponseStatus() { + public ResponseCode getResponseStatus() { return responseStatus; } }