SpringBoot異常處理(Whitelabel Error Page和自定義全局異常處理頁面)和整合ajax異常處理
1、springboot自帶的異常處理頁面Whitelabel Error Page
SpringBoot默認(rèn)的處理異常的機(jī)制:SpringBoot 默認(rèn)的已經(jīng)提供了一套處理異常的機(jī)制。一旦程序中出現(xiàn)了異常 SpringBoot 會(huì)向/error 的 url 發(fā)送請(qǐng)求。在 springBoot 中提供了一個(gè)叫 BasicErrorController 來處理/error 請(qǐng)求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁面來展示異常信息
自定義該頁面顯示內(nèi)容
如 果我 們 需 要 將 所 有 的 異 常 同 一 跳 轉(zhuǎn) 到 自 定 義 的 錯(cuò) 誤 頁 面 , 需 要 在src/main/resources/templates 目錄下創(chuàng)建 error.html 頁面。注意:名稱必須叫 error
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
出錯(cuò)了,請(qǐng)與管理員聯(lián)系。。。
</body>
</html>
在pom.xml文件中添加下面依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
編寫controller
package cn.fpl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("/show1")
public String show1(){
String str = null;
str.length();
return "index";
}
@RequestMapping("/show2")
public String show2(){
int a = 6/0;
return "index";
}
}
然后重新啟動(dòng)后異常頁面變?yōu)椋?br>
2、整合web訪問全局異常處理器
2.1、創(chuàng)建全局異常處理器
創(chuàng)建一個(gè)exception包在其中創(chuàng)建一個(gè)GlobaleExceptionHandler.java
package cn.fpl.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 GlobaleExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
if(e instanceof NullPointerException){
mv.setViewName("error1");
}else if(e instanceof ArithmeticException){
mv.setViewName("error2");
}
mv.addObject("msg", e.toString());
return mv;
}
}
2.2、在src/main/resources/templates 目錄下創(chuàng)建 error1.html 頁面和 error2.html 頁面
error1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>錯(cuò)誤提示頁面-ArithmeticException</title>
</head>
<body>
<h2>error1</h2>
出錯(cuò)了,請(qǐng)與管理員聯(lián)系。。。
<span th:text="${msg}"></span>
</body>
</html>
error2.html文章來源:http://www.zghlxwxcb.cn/news/detail-792731.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>錯(cuò)誤提示頁面-ArithmeticException</title>
</head>
<body>
<h2>error2</h2>
出錯(cuò)了,請(qǐng)與管理員聯(lián)系。。。
<span th:text="${msg}"></span>
</body>
</html>
3、整合ajax全局異常處理
3.1、在exception包下創(chuàng)建全局異常處理器AjaxGlobalException.java
/*
* Copyright (c) 2020, 2024, fpl1116.cn All rights reserved.
*
*/
package cn.fpl.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;
/**
* <p>Project: SpringBootDemo - AjaxGlobalException</p>
* <p>Powered by fpl1116 On 2024-01-15 10:48:48</p>
* <p>描述:<p>
*
* @author fpl1116 [2391940642@qq.com]
* @version 1.0
* @since 1.8
*/
@ControllerAdvice
public class AjaxGlobalException {
@ResponseBody
@ExceptionHandler
public Map errorHandler(Exception e){
Map<String, Object> map = new HashMap<>();
map.put("status", 500);
map.put("msg", e.toString());
return map;//{status:500, msg:異常信息}
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-792731.html
到了這里,關(guān)于SpringBoot異常處理(Whitelabel Error Page和自定義全局異常處理頁面)和整合ajax異常處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!