From 7c914e23afbd07aa4a120cb23a2da1290a9ee131 Mon Sep 17 00:00:00 2001 From: gewuyou Date: Sat, 31 May 2025 22:42:40 +0800 Subject: [PATCH] refactor(webmvc-exception):Refactoring the Web MVC exception handling module - Removed international configuration and processing logic - Optimized error message copy - Simplified configuration property structure - Updated exception handling logic, unified error code and error message handling method - Added automatic configuration of modules --- forgeboot-webmvc/exception/build.gradle.kts | 7 ---- .../WebMvcExceptionAutoConfiguration.kt | 32 +++---------------- .../entities/WebMvcExceptionProperties.kt | 14 ++++---- .../handler/GlobalExceptionHandler.kt | 12 +++---- ...ot.autoconfigure.AutoConfiguration.imports | 1 + 5 files changed, 19 insertions(+), 47 deletions(-) create mode 100644 forgeboot-webmvc/exception/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/forgeboot-webmvc/exception/build.gradle.kts b/forgeboot-webmvc/exception/build.gradle.kts index b7f51f7..eb6ffa6 100644 --- a/forgeboot-webmvc/exception/build.gradle.kts +++ b/forgeboot-webmvc/exception/build.gradle.kts @@ -1,18 +1,11 @@ plugins{ - alias(libs.plugins.forgeboot.i18n.keygen) alias(libs.plugins.kotlin.plugin.spring) } dependencies { implementation(project(Modules.Core.EXTENSION)) - api(project(Modules.I18n.STARTER)) api(project(Modules.TRACE.STARTER)) implementation(project(Modules.Webmvc.DTO)) compileOnly(libs.springBootStarter.validation) compileOnly(libs.springBootStarter.web) kapt(libs.springBoot.configuration.processor) } -i18nKeyGen { - rootPackage.set("com.gewuyou.forgeboot.webmvc.extension.i18n") - readPath.set("src/main/resources/i18n/${project.name}") - level.set(3) -} diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/WebMvcExceptionAutoConfiguration.kt b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/WebMvcExceptionAutoConfiguration.kt index fda2b32..0d84424 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/WebMvcExceptionAutoConfiguration.kt +++ b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/WebMvcExceptionAutoConfiguration.kt @@ -3,12 +3,9 @@ package com.gewuyou.forgeboot.webmvc.exception.config import com.gewuyou.forgeboot.core.extension.log import com.gewuyou.forgeboot.i18n.api.MessageResolver import com.gewuyou.forgeboot.trace.api.RequestIdProvider -import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionI18nProperties import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionProperties import com.gewuyou.forgeboot.webmvc.exception.handler.GlobalExceptionHandler -import com.gewuyou.forgeboot.webmvc.exception.handler.I18nGlobalExceptionHandler import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -20,8 +17,8 @@ import org.springframework.core.annotation.Order * @since 2025-05-13 11:48:01 * @author gewuyou */ -@EnableConfigurationProperties(WebMvcExceptionI18nProperties::class, WebMvcExceptionProperties::class) @Configuration +@EnableConfigurationProperties(WebMvcExceptionProperties::class) class WebMvcExceptionAutoConfiguration { /** *默认消息解析器 @@ -47,31 +44,12 @@ class WebMvcExceptionAutoConfiguration { @Bean @ConditionalOnMissingBean - @ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "true") - fun i18nGlobalExceptionHandler( - webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties, - messageResolver: MessageResolver, - requestIdProvider: RequestIdProvider, - ): I18nGlobalExceptionHandler { - log.info("本地化全局异常处理程序创建成功!") - return I18nGlobalExceptionHandler( - webMvcExceptionI18nProperties, - messageResolver, - requestIdProvider - ) - } - - @Bean - @ConditionalOnMissingBean - @ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "false") - fun i18nGlobalExceptionHandler( + fun globalExceptionHandler( webMvcExceptionProperties: WebMvcExceptionProperties, requestIdProvider: RequestIdProvider, ): GlobalExceptionHandler { - log.info("全局异常处理程序创建成功!") - return GlobalExceptionHandler( - webMvcExceptionProperties, - requestIdProvider - ) + log.info("Web MVC 异常处理器 已创建!") + return GlobalExceptionHandler(webMvcExceptionProperties, requestIdProvider) } + } \ No newline at end of file diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionProperties.kt b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionProperties.kt index 77717aa..30d0367 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionProperties.kt +++ b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionProperties.kt @@ -8,12 +8,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties * @author gewuyou * @since 2025-05-13 11:06:46 */ -@ConfigurationProperties("forgeboot.webmvc.exception.default") +@ConfigurationProperties("forgeboot.webmvc.exception") class WebMvcExceptionProperties { companion object { - const val ERROR_MESSAGE_ILLEGAL_PARAMETERS = "Illegal parameters, please check the input!" - const val ERROR_MESSAGE_INTERNAL_SERVER = - "If there is an internal execution error, please report the request ID of this request!" + const val ERROR_MESSAGE_INVALID_PARAMETERS = "Invalid parameters. Please verify your input." + const val ERROR_MESSAGE_INTERNAL_SERVER = "Internal server error. Please report the request ID for troubleshooting." + } /** @@ -52,14 +52,14 @@ class WebMvcExceptionProperties { * * @param defaultValidationExceptionErrorMessage 默认验证异常的错误消息 */ - var defaultValidationExceptionErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS + var defaultValidationExceptionErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS /** * 设置默认验证异常的字段错误消息 * * @param defaultValidationExceptionFieldErrorMessage 默认验证异常的字段错误消息 */ - var defaultValidationExceptionFieldErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS + var defaultValidationExceptionFieldErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS /** * 设置默认无效参数异常的错误代码 @@ -73,7 +73,7 @@ class WebMvcExceptionProperties { * * @param defaultInvalidParameterErrorMessage 默认无效参数异常的错误消息 */ - var defaultInvalidParameterErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS + var defaultInvalidParameterErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS /** * 设置默认内部服务器错误异常的错误代码 diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/GlobalExceptionHandler.kt b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/GlobalExceptionHandler.kt index 284dd39..94bfe16 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/GlobalExceptionHandler.kt +++ b/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/GlobalExceptionHandler.kt @@ -39,7 +39,7 @@ class GlobalExceptionHandler( fun handleOtherException(e: Exception): R { log.error("other exception:", e) return R.failure( - webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode.toString(), + webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode, webMvcExceptionProperties.otherGeneralExternalExceptionErrorMessage, null, requestIdProvider ) @@ -59,7 +59,7 @@ class GlobalExceptionHandler( // 返回字段错误 for (fieldError in ex.bindingResult.fieldErrors) { return R.failure( - HttpStatus.BAD_REQUEST.value().toString(), + HttpStatus.BAD_REQUEST.value(), fieldError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionFieldErrorMessage, null, requestIdProvider @@ -68,14 +68,14 @@ class GlobalExceptionHandler( // 返回全局错误 for (objectError in ex.bindingResult.globalErrors) { return R.failure( - HttpStatus.BAD_REQUEST.value().toString(), + HttpStatus.BAD_REQUEST.value(), objectError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionErrorMessage, null, requestIdProvider ) } return R.failure( - webMvcExceptionProperties.defaultValidationExceptionErrorCode.toString(), + webMvcExceptionProperties.defaultValidationExceptionErrorCode, webMvcExceptionProperties.defaultValidationExceptionErrorMessage, null, requestIdProvider @@ -95,12 +95,12 @@ class GlobalExceptionHandler( fun handleConstraintViolationException(ex: ConstraintViolationException): R { for (constraintViolation in ex.constraintViolations) { return R.failure( - HttpStatus.BAD_REQUEST.value().toString(), constraintViolation.message, + HttpStatus.BAD_REQUEST.value(), constraintViolation.message, null, requestIdProvider ) } return R.failure( - webMvcExceptionProperties.defaultInvalidParameterErrorCode.toString(), + webMvcExceptionProperties.defaultInvalidParameterErrorCode, webMvcExceptionProperties.defaultInvalidParameterErrorMessage, null, requestIdProvider diff --git a/forgeboot-webmvc/exception/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/forgeboot-webmvc/exception/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..69df3d0 --- /dev/null +++ b/forgeboot-webmvc/exception/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.gewuyou.forgeboot.webmvc.exception.config.WebMvcExceptionAutoConfiguration \ No newline at end of file