From 78ca098488327eaaa4123372b5311b309e1a2426 Mon Sep 17 00:00:00 2001 From: gewuyou Date: Sat, 31 May 2025 22:42:04 +0800 Subject: [PATCH] feat(webmvc): Add an international exception handling module - Added exception-i18n module to handle international exceptions - Reconstruct the original exception module and change it to exception-i18n - Added WebMvcExceptionAutoConfiguration class to realize automatic configuration of international exceptions - Added GlobalException and InternalException classes for global exception handling - Added GlobalExceptionHandler class to realize international global exception handling - Update the project structure and add necessary dependencies and configurations --- .../exception-i18n/.gitattributes | 3 + forgeboot-webmvc/exception-i18n/.gitignore | 40 +++++++++++++ .../exception-i18n/build.gradle.kts | 18 ++++++ .../WebMvcExceptionAutoConfiguration.kt | 59 +++++++++++++++++++ .../entities/WebMvcExceptionI18nProperties.kt | 2 +- .../exception/i18n/core/GlobalException.kt} | 4 +- .../exception/i18n/core/InternalException.kt} | 2 +- .../i18n/handler/GlobalExceptionHandler.kt} | 18 +++--- ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../messages.properties | 0 .../messages_en.properties | 0 .../messages_zh_CN.properties | 0 12 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 forgeboot-webmvc/exception-i18n/.gitattributes create mode 100644 forgeboot-webmvc/exception-i18n/.gitignore create mode 100644 forgeboot-webmvc/exception-i18n/build.gradle.kts create mode 100644 forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/WebMvcExceptionAutoConfiguration.kt rename forgeboot-webmvc/{exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception => exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n}/config/entities/WebMvcExceptionI18nProperties.kt (97%) rename forgeboot-webmvc/{exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nGlobalException.kt => exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/GlobalException.kt} (89%) rename forgeboot-webmvc/{exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nInternalException.kt => exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/InternalException.kt} (97%) rename forgeboot-webmvc/{exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/I18nGlobalExceptionHandler.kt => exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/handler/GlobalExceptionHandler.kt} (90%) create mode 100644 forgeboot-webmvc/exception-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename forgeboot-webmvc/{exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter => exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter}/messages.properties (100%) rename forgeboot-webmvc/{exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter => exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter}/messages_en.properties (100%) rename forgeboot-webmvc/{exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter => exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter}/messages_zh_CN.properties (100%) diff --git a/forgeboot-webmvc/exception-i18n/.gitattributes b/forgeboot-webmvc/exception-i18n/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/forgeboot-webmvc/exception-i18n/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/forgeboot-webmvc/exception-i18n/.gitignore b/forgeboot-webmvc/exception-i18n/.gitignore new file mode 100644 index 0000000..5a979af --- /dev/null +++ b/forgeboot-webmvc/exception-i18n/.gitignore @@ -0,0 +1,40 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Kotlin ### +.kotlin diff --git a/forgeboot-webmvc/exception-i18n/build.gradle.kts b/forgeboot-webmvc/exception-i18n/build.gradle.kts new file mode 100644 index 0000000..b7f51f7 --- /dev/null +++ b/forgeboot-webmvc/exception-i18n/build.gradle.kts @@ -0,0 +1,18 @@ +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-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/WebMvcExceptionAutoConfiguration.kt b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/WebMvcExceptionAutoConfiguration.kt new file mode 100644 index 0000000..b9e50f2 --- /dev/null +++ b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/WebMvcExceptionAutoConfiguration.kt @@ -0,0 +1,59 @@ +package com.gewuyou.forgeboot.webmvc.exception.i18n.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.i18n.config.entities.WebMvcExceptionI18nProperties +import com.gewuyou.forgeboot.webmvc.exception.i18n.handler.GlobalExceptionHandler +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.annotation.Order + +/** + *Web MVC 异常自动配置 + * + * @since 2025-05-13 11:48:01 + * @author gewuyou + */ +@EnableConfigurationProperties(WebMvcExceptionI18nProperties::class) +@Configuration +class WebMvcExceptionAutoConfiguration { + /** + *默认消息解析器 + * + * @since 2025-05-03 16:21:43 + * @author gewuyou + */ + @Bean + @Order(Int.MAX_VALUE) + @ConditionalOnMissingBean + fun defaultMessageResolver(): MessageResolver = MessageResolver { code, _ -> code } + + /** + *默认请求ID提供商 + * + * @since 2025-05-03 16:22:18 + * @author gewuyou + */ + @Bean + @Order(Int.MAX_VALUE) + @ConditionalOnMissingBean + fun defaultRequestIdProvider(): RequestIdProvider = RequestIdProvider { "" } + + @Bean + @ConditionalOnMissingBean + fun i18nGlobalExceptionHandler( + webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties, + messageResolver: MessageResolver, + requestIdProvider: RequestIdProvider, + ): GlobalExceptionHandler { + log.info("本地化全局异常处理程序创建成功!") + return GlobalExceptionHandler( + webMvcExceptionI18nProperties, + messageResolver, + requestIdProvider + ) + } +} \ No newline at end of file diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionI18nProperties.kt b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/entities/WebMvcExceptionI18nProperties.kt similarity index 97% rename from forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionI18nProperties.kt rename to forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/entities/WebMvcExceptionI18nProperties.kt index 6130b60..d656c6f 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/config/entities/WebMvcExceptionI18nProperties.kt +++ b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/config/entities/WebMvcExceptionI18nProperties.kt @@ -1,4 +1,4 @@ -package com.gewuyou.forgeboot.webmvc.exception.config.entities +package com.gewuyou.forgeboot.webmvc.exception.i18n.config.entities import com.gewuyou.forgeboot.webmvc.extension.i18n.I18nKeys import org.springframework.boot.context.properties.ConfigurationProperties diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nGlobalException.kt b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/GlobalException.kt similarity index 89% rename from forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nGlobalException.kt rename to forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/GlobalException.kt index c22c423..3236d17 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nGlobalException.kt +++ b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/GlobalException.kt @@ -1,4 +1,4 @@ -package com.gewuyou.forgeboot.webmvc.exception.core +package com.gewuyou.forgeboot.webmvc.exception.i18n.core import com.gewuyou.forgeboot.i18n.api.I18nResponseInformation import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException @@ -10,7 +10,7 @@ import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException * @author gewuyou * @since 2024-11-23 16:45:10 */ -open class I18nGlobalException : I18nBaseException { +open class GlobalException : I18nBaseException { /** * 构造函数:初始化全局异常 * diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nInternalException.kt b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/InternalException.kt similarity index 97% rename from forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nInternalException.kt rename to forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/InternalException.kt index 2053811..df92a87 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/core/I18nInternalException.kt +++ b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/core/InternalException.kt @@ -9,7 +9,7 @@ import com.gewuyou.forgeboot.i18n.api.I18nInternalInformation * @author gewuyou * @since 2024-11-24 21:14:03 */ -open class I18nInternalException : RuntimeException { +open class InternalException : RuntimeException { /** * 错误信息 */ diff --git a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/I18nGlobalExceptionHandler.kt b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/handler/GlobalExceptionHandler.kt similarity index 90% rename from forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/I18nGlobalExceptionHandler.kt rename to forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/handler/GlobalExceptionHandler.kt index cae7ef6..d21a1f3 100644 --- a/forgeboot-webmvc/exception/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/handler/I18nGlobalExceptionHandler.kt +++ b/forgeboot-webmvc/exception-i18n/src/main/kotlin/com/gewuyou/forgeboot/webmvc/exception/i18n/handler/GlobalExceptionHandler.kt @@ -1,13 +1,13 @@ -package com.gewuyou.forgeboot.webmvc.exception.handler +package com.gewuyou.forgeboot.webmvc.exception.i18n.handler 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.dto.I18nResult -import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionI18nProperties -import com.gewuyou.forgeboot.webmvc.exception.core.I18nGlobalException -import com.gewuyou.forgeboot.webmvc.exception.core.I18nInternalException +import com.gewuyou.forgeboot.webmvc.exception.i18n.config.entities.WebMvcExceptionI18nProperties +import com.gewuyou.forgeboot.webmvc.exception.i18n.core.GlobalException +import com.gewuyou.forgeboot.webmvc.exception.core.InternalException import jakarta.validation.ConstraintViolationException @@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice * @since 2024-04-13 上午12:22:18 */ @RestControllerAdvice -class I18nGlobalExceptionHandler( +class GlobalExceptionHandler( private val webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties, private val messageResolver: MessageResolver, private val requestIdProvider: RequestIdProvider, @@ -129,8 +129,8 @@ class I18nGlobalExceptionHandler( * @return 返回的结果 * @since 2024/4/13 下午1:56 */ - @ExceptionHandler(I18nGlobalException::class) - fun handleGlobalException(e: I18nGlobalException): I18nResult { + @ExceptionHandler(GlobalException::class) + fun handleGlobalException(e: GlobalException): I18nResult { return I18nResult.failure( e.errorCode, e.errorI18nMessageCode, null, e.errorI18nMessageArgs, messageResolver, requestIdProvider @@ -145,8 +145,8 @@ class I18nGlobalExceptionHandler( * @param e 异常 * @return 返回的结果 */ - @ExceptionHandler(I18nInternalException::class) - fun handleGlobalException(e: I18nInternalException): I18nResult { + @ExceptionHandler(InternalException::class) + fun handleGlobalException(e: InternalException): I18nResult { log.error("内部异常: 异常信息: {}", e.errorMessage, e) e.i18nInternalInformation?.responseI8nMessageCode?.let { log.error( diff --git a/forgeboot-webmvc/exception-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/forgeboot-webmvc/exception-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..6dd2adf --- /dev/null +++ b/forgeboot-webmvc/exception-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.gewuyou.forgeboot.webmvc.exception.i18n.config.WebMvcExceptionAutoConfiguration \ No newline at end of file diff --git a/forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages.properties b/forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages.properties similarity index 100% rename from forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages.properties rename to forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages.properties diff --git a/forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages_en.properties b/forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages_en.properties similarity index 100% rename from forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages_en.properties rename to forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages_en.properties diff --git a/forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages_zh_CN.properties b/forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages_zh_CN.properties similarity index 100% rename from forgeboot-webmvc/exception/src/main/resources/i18n/forgeboot-webmvc-exception-spring-boot-starter/messages_zh_CN.properties rename to forgeboot-webmvc/exception-i18n/src/main/resources/i18n/forgeboot-webmvc-exception-i18n-spring-boot-starter/messages_zh_CN.properties