Merge pull request '提交解释1' (#2) from 提交1 into main

王兆华
pcygfhfke 4 months ago
commit c9c6ac0012

@ -1,24 +1,52 @@
package com.shanzhu.market.common.advice; package com.shanzhu.market.common.advice;
// 定义当前类所在的包路径
import com.shanzhu.market.common.constants.HttpStatus; import com.shanzhu.market.common.constants.HttpStatus;
// 导入包含HTTP状态码常量的工具类
import com.shanzhu.market.common.exception.BusinessException; import com.shanzhu.market.common.exception.BusinessException;
// 导入自定义的业务异常类
import com.shanzhu.market.common.web.response.JsonResult; import com.shanzhu.market.common.web.response.JsonResult;
// 导入用于封装JSON响应结果的工具类
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
// 导入Spring框架的异常处理注解
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
// 导入Spring框架的全局异常处理注解
/** /**
* *
*/ */
// 定义类的注释,说明该类的作用
@RestControllerAdvice @RestControllerAdvice
// 声明该类为全局异常处理类返回JSON格式的响应
public class ExceptionControllerAdvice { public class ExceptionControllerAdvice {
// 定义全局异常处理类
@ExceptionHandler(RuntimeException.class) @ExceptionHandler(RuntimeException.class)
// 声明方法处理RuntimeException类型的异常
public JsonResult<?> commonExceptionHandler(RuntimeException ex){ public JsonResult<?> commonExceptionHandler(RuntimeException ex){
// 定义处理RuntimeException异常的方法返回JsonResult对象
ex.printStackTrace(); ex.printStackTrace();
// 打印异常堆栈信息到控制台
return JsonResult.error(HttpStatus.CODE_BUSINESS_ERROR,ex.getMessage()); return JsonResult.error(HttpStatus.CODE_BUSINESS_ERROR,ex.getMessage());
// 返回封装的错误响应,包含状态码和异常信息
} }
@ExceptionHandler(BusinessException.class) @ExceptionHandler(BusinessException.class)
// 声明方法处理BusinessException类型的异常
public JsonResult<?> businessHanler(BusinessException ex){ public JsonResult<?> businessHanler(BusinessException ex){
// 定义处理BusinessException异常的方法返回JsonResult对象
return JsonResult.error(HttpStatus.CODE_BUSINESS_ERROR,ex.getMessage()); return JsonResult.error(HttpStatus.CODE_BUSINESS_ERROR,ex.getMessage());
// 返回封装的错误响应,包含状态码和异常信息
} }
} }
Loading…
Cancel
Save