一、自定义错误页面
SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息
如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/
templates 目录下创建 error.html 页面。注意:名称必须叫 error
controller
package com.by.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ExceptionController {@RequestMapping("/Exception1")public String Exception1(){String str = null;str.length();return "index";}@RequestMapping("Exception2")public String Exception2(){int a = 10/0;return "index";}
}
html页面中使用到了thymeleaf,需要引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
自定义错误页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面</title>
</head>
<body>出错了,请与管理员联系。。。<span th:text="${error}"></span>
</body>
</html>
二、整合web访问全局异常处理器
1.处理思路
2.创建全局异常处理器
package com.by.exception;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;@Component
public class GlobalException implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {ModelAndView modelAndView = new ModelAndView();if(ex instanceof ArithmeticException){modelAndView.setViewName("error1");}else if(ex instanceof NullPointerException){modelAndView.setViewName("error2");}modelAndView.addObject("error",ex.toString());return modelAndView;}
}
3.错误页面
error1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-ArithmeticException</title>
</head>
<body>出错了,请与管理员联系。。。<span th:text="${error}"></span>
</body>
</html>
error2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面-NullPointerException</title>
</head>
<body>出错了,请与管理员联系。。。<span th:text="${error}"></span>
</body>
</html>
测试:
创建controller类
package com.by.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ExceptionController {@RequestMapping("/Exception1")public String Exception1(){String str = null;str.length();return "index";}@RequestMapping("Exception2")public String Exception2(){int a = 10/0;return "index";}
}
测试结果
三、整合ajax全局异常处理
1.创建全局异常处理器
package com.by.exception;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;@ControllerAdvice
public class AjaxException {/*** 处理全局异常* @param exception 异常* @return Map<String, Object>*/@ResponseBody@ExceptionHandler(value = Exception.class)public Map<String, Object> errorHandler(Exception exception) {Map<String, Object> map = new HashMap<>();map.put("status", 500);map.put("msg", exception.getMessage());return map;}
}
测试:
异常注销,无异常时,正常返回
发生异常时