Class GlobalExceptionHandler
java.lang.Object
com.elitesland.ystdms.sys.exception.GlobalExceptionHandler
全局异常处理器 - Spring Boot 3.5 最佳实践
确保在发生异常时也能清理MDC
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionhandleBindException(org.springframework.validation.BindException e, jakarta.servlet.http.HttpServletRequest request) 处理绑定异常 (BindException)handleBusinessException(BusinessException e, jakarta.servlet.http.HttpServletRequest request) 处理业务异常handleGenericException(Exception e, jakarta.servlet.http.HttpServletRequest request) 处理所有其他未捕获的受检异常等handleJsonProcessing(com.fasterxml.jackson.core.JsonProcessingException e, jakarta.servlet.http.HttpServletRequest request) JSON 序列化/反序列化失败(多为响应写出阶段,属受检异常分支 IOException)handleMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException e, jakarta.servlet.http.HttpServletRequest request) 处理HTTP消息不可读异常 (如JSON格式错误)handleMissingParams(org.springframework.web.bind.MissingServletRequestParameterException e, jakarta.servlet.http.HttpServletRequest request) 处理缺少请求参数异常handleResourceNotFoundException(ResourceNotFoundException e, jakarta.servlet.http.HttpServletRequest request) 处理资源未找到异常handleRuntimeException(RuntimeException e, jakarta.servlet.http.HttpServletRequest request) 处理运行时异常(必须位于Exception总兜底之前注册,避免宽泛的 Exception 先匹配掉所有异常)handleTypeMismatch(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e, jakarta.servlet.http.HttpServletRequest request) 处理方法参数类型不匹配异常handleUnauthorizedException(UnauthorizedException e, jakarta.servlet.http.HttpServletRequest request) 处理未授权异常handleValidationException(ValidationException e, jakarta.servlet.http.HttpServletRequest request) 处理验证异常handleValidationException(org.springframework.web.bind.MethodArgumentNotValidException e, jakarta.servlet.http.HttpServletRequest request) 处理参数验证异常 (MethodArgumentNotValidException)
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handleBusinessException
@ExceptionHandler(BusinessException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleBusinessException(BusinessException e, jakarta.servlet.http.HttpServletRequest request) 处理业务异常 -
handleValidationException
@ExceptionHandler(ValidationException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleValidationException(ValidationException e, jakarta.servlet.http.HttpServletRequest request) 处理验证异常 -
handleResourceNotFoundException
@ExceptionHandler(ResourceNotFoundException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleResourceNotFoundException(ResourceNotFoundException e, jakarta.servlet.http.HttpServletRequest request) 处理资源未找到异常 -
handleUnauthorizedException
@ExceptionHandler(UnauthorizedException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleUnauthorizedException(UnauthorizedException e, jakarta.servlet.http.HttpServletRequest request) 处理未授权异常 -
handleValidationException
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleValidationException(org.springframework.web.bind.MethodArgumentNotValidException e, jakarta.servlet.http.HttpServletRequest request) 处理参数验证异常 (MethodArgumentNotValidException) -
handleBindException
@ExceptionHandler(org.springframework.validation.BindException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleBindException(org.springframework.validation.BindException e, jakarta.servlet.http.HttpServletRequest request) 处理绑定异常 (BindException) -
handleMissingParams
@ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleMissingParams(org.springframework.web.bind.MissingServletRequestParameterException e, jakarta.servlet.http.HttpServletRequest request) 处理缺少请求参数异常 -
handleTypeMismatch
@ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleTypeMismatch(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e, jakarta.servlet.http.HttpServletRequest request) 处理方法参数类型不匹配异常 -
handleMessageNotReadable
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException e, jakarta.servlet.http.HttpServletRequest request) 处理HTTP消息不可读异常 (如JSON格式错误) -
handleJsonProcessing
@ExceptionHandler(com.fasterxml.jackson.core.JsonProcessingException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleJsonProcessing(com.fasterxml.jackson.core.JsonProcessingException e, jakarta.servlet.http.HttpServletRequest request) JSON 序列化/反序列化失败(多为响应写出阶段,属受检异常分支 IOException) -
handleRuntimeException
@ExceptionHandler(java.lang.RuntimeException.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleRuntimeException(RuntimeException e, jakarta.servlet.http.HttpServletRequest request) 处理运行时异常(必须位于Exception总兜底之前注册,避免宽泛的 Exception 先匹配掉所有异常) -
handleGenericException
@ExceptionHandler(java.lang.Exception.class) public org.springframework.http.ResponseEntity<ApiResult<Object>> handleGenericException(Exception e, jakarta.servlet.http.HttpServletRequest request) 处理所有其他未捕获的受检异常等
-