SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息
如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/
templates 目录下创建 error.html 页面。注意:名称必须叫 error
1.controller:
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* <p>Project: springboot - DomeContorller</p>
* <p>Powered by scl On 2024-01-15 10:02:34</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@Controller
public class DomeController {
@RequestMapping("show1")
public String show1() {
System.out.println(6 / 0);
return "error";
}
@RequestMapping("show2")
public String show2() {
String str=null;
str.length();
return "error";
}
}
2.定义错误页面
error.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>error1</h1>
出错啦!!!!汗流浃背了!!!
<span th:text="${msg}"></span>
</body>
</html>
error.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>error2</h1>
出错啦!!!!汗流浃背了!!!
<span th:text="${msg}"></span>
</body>
</html>
3.定义全局异常处理类:
GlobalException:(普通的处理请求)
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.exception;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* <p>Project: springboot - GlobalException</p>
* <p>Powered by scl On 2024-01-15 10:11:29</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@Component
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if (e instanceof NullPointerException){
modelAndView.setViewName("error1");
} else if (e instanceof ArithmeticException) {
modelAndView.setViewName("error2");
}
modelAndView.addObject("msg",e.toString());
return modelAndView;
}
}
AjaxGlobalException:(ajax的请求方式,返回的使json数据)
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.exception;
import javafx.beans.property.adapter.ReadOnlyJavaBeanBooleanProperty;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* <p>Project: springboot - AjaxGlobalException</p>
* <p>Powered by scl On 2024-01-15 10:48:17</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
//@ControllerAdvice
public class AjaxGlobalException {
@ResponseBody
@ExceptionHandler
public Map errorHandler(Exception e){
Map<String , Object> m = new HashMap<>();
m.put("status",500);
m.put("msg",e.toString());
return m;
}
}