SpringBoot配置系統(tǒng)全局異常映射處理
一、異常分類
這里的異常分類從系統(tǒng)處理異常的角度看,主要分類兩類:業(yè)務異常和系統(tǒng)異常。
1、業(yè)務異常
業(yè)務異常主要是一些可預見性異常,處理業(yè)務異常,用來提示用戶的操作,提高系統(tǒng)的可操作性。
常見的業(yè)務異常提示:
1)請輸入xxx
2)xxx不能為空
3)xxx重復,請更換
2、系統(tǒng)異常
系統(tǒng)異常主要是一些不可預見性異常,處理系統(tǒng)異常,可以讓展示出一個友好的用戶界面,不易給用戶造成反感。如果是一個金融類系統(tǒng),在用戶界面出現(xiàn)一個系統(tǒng)異常的崩潰界面,很有可能直接導致用戶流失。
常見的系統(tǒng)異常提示:
1)頁面丟失404
2)服務器異常500
二、解決應用啟動后404界面

1、引入頁面Jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、自定義首頁接口
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(ModelMap modelMap) {
modelMap.addAttribute("name","知了一笑") ;
return "index";
}
}
3、首頁界面
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>
4、運行效果

三、SpringBoot2.0中異常處理
1、項目結構圖

2、自定義業(yè)務異常類
public class ServiceException extends Exception {
public ServiceException (String msg){
super(msg);
}
}
3、自定義異常描述對象
public class ReturnException {
// 響應碼
private Integer code;
// 異常描述
private String msg;
// 請求的Url
private String url;
// 省略 get set 方法
}
4、統(tǒng)一異常處理格式
1)兩個基礎注解
@ControllerAdvice 定義統(tǒng)一的異常處理類
@ExceptionHandler 定義異常類型對應的處理方式
2)代碼實現(xiàn)
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
// 異常以Json格式返回 等同 ExceptionHandler + ResponseBody 注解
// @RestControllerAdvice
public class HandlerException {
/**
* 自定義業(yè)務異常映射,返回JSON格式提示
*/
@ExceptionHandler(value = ServiceException.class)
@ResponseBody
public ReturnException handler01 (HttpServletRequest request,ServiceException e){
ReturnException returnException = new ReturnException() ;
returnException.setCode(600);
returnException.setMsg(e.getMessage());
returnException.setUrl(String.valueOf(request.getRequestURL()));
return returnException ;
}
/**
* 服務異常
*/
@ExceptionHandler(value = Exception.class)
public ModelAndView handler02 (HttpServletRequest request,Exception e){
ModelAndView modelAndView = new ModelAndView() ;
modelAndView.addObject("ExeMsg", e.getMessage());
modelAndView.addObject("ReqUrl", request.getRequestURL());
modelAndView.setViewName("/exemsg");
return modelAndView ;
}
}
5、簡單的測試接口
@Controller
public class ExeController {
/**
* {
* "code": 600,
* "msg": "業(yè)務異常:ID 不能為空",
* "url": "http://localhost:8003/exception01"
* }
*/
@RequestMapping("/exception01")
public String exception01 () throws ServiceException {
throw new ServiceException("業(yè)務異常:ID 不能為空");
}
@RequestMapping("/exception02")
public String exception02 () throws Exception {
throw new Exception("出現(xiàn)異常,全體臥倒");
}
}

四、源代碼地址
GitHub:知了一笑
https://github.com/cicadasmile/spring-boot-base
以上就是SpringBoot配置系統(tǒng)全局異常映射處理的詳細內(nèi)容,更多關于SpringBoot 異常映射處理的資料請關注腳本之家其它相關文章!
相關文章
springBoot集成mybatis 轉(zhuǎn)換為 mybatis-plus方式
這篇文章主要介紹了springBoot集成mybatis 轉(zhuǎn)換為 mybatis-plus方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
解析SpringBoot中使用LoadTimeWeaving技術實現(xiàn)AOP功能
這篇文章主要介紹了SpringBoot中使用LoadTimeWeaving技術實現(xiàn)AOP功能,AOP面向切面編程,通過為目標類織入切面的方式,實現(xiàn)對目標類功能的增強,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-09-09
Spring Boot如何優(yōu)雅的使用多線程實例詳解
這篇文章主要給大家介紹了關于Spring Boot如何優(yōu)雅的使用多線程的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-05-05
Java?IO流—異常及捕獲異常處理?try…catch…finally
這篇文章主要介紹了Java?IO流—異常及捕獲異常處理?try…catch…finally,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java中@DS+@Transactional注解切換數(shù)據(jù)源失效解決方案
本文主要介紹了@DS+@Transactional注解切換數(shù)據(jù)源失效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06

