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
This commit is contained in:
gewuyou 2025-05-31 22:42:40 +08:00
parent 78ca098488
commit 7c914e23af
5 changed files with 19 additions and 47 deletions

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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
/**
* 设置默认内部服务器错误异常的错误代码

View File

@ -39,7 +39,7 @@ class GlobalExceptionHandler(
fun handleOtherException(e: Exception): R<String> {
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<String> {
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

View File

@ -0,0 +1 @@
com.gewuyou.forgeboot.webmvc.exception.config.WebMvcExceptionAutoConfiguration