package com.backend.webbackend.Exception; import com.backend.webbackend.Vo.ResultVo; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @Slf4j @ControllerAdvice//主要作用:统一异常处理,全局数据绑定:通过InitBinder为所有控制器添加预定义的数据绑定规则,全局模型属性,通过ModelAttribute注解为所有控制器的模型添加公共属性 public class GlobalExceptionHandler { @ExceptionHandler(Exception.class)//异常捕获,当与ControllerAdvice注解一起使用时,可以捕获所有控制器抛出的异常,并统一处理 @ResponseBody//返回数据 public ResultVo error(Exception e){ log.error("Unhandled exception", e); return ResultVo.Error("",e.getMessage()); } }