SpringBoot 全局异常
全局异常
如果程序有异常自动跳转到这个方法
/**
* 全局异常处理类
*/
@ControllerAdvice
public class ExceptionHandler {
@org.springframework.web.bind.annotation.ExceptionHandler // 表示这个异常捕获方法
public String exceptionhandler(Exception e, Model model){
model.addAttribute("Msg",e.getMessage() + "服务器错误.....");
return "error";
}
}
错误页面
<h1 th:text="${Msg}"></h1>
控制器 人为制造异常
@Controller
public class GameController {
@RequestMapping("/test")
public String test(){
int x = 1;
int y = 0;
int c = 1 / 0;
return "login";
}
}
浏览器测试


浙公网安备 33010602011771号