天天看點

SpringBoot2.0 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址

一、異常分類

這裡的異常分類從系統處理異常的角度看,主要分類兩類:業務異常和系統異常。

1、業務異常

業務異常主要是一些可預見性異常,處理業務異常,用來提示使用者的操作,提高系統的可操作性。

常見的業務異常提示:

1)請輸入xxx

2)xxx不能為空

3)xxx重複,請更換

2、系統異常

系統異常主要是一些不可預見性異常,處理系統異常,可以讓展示出一個友好的使用者界面,不易給使用者造成反感。如果是一個金融類系統,在使用者界面出現一個系統異常的崩潰界面,很有可能直接導緻使用者流失。

常見的系統異常提示:

1)頁面丢失404

2)伺服器異常500

二、解決應用啟動後404界面

SpringBoot2.0 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址

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 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址

三、SpringBoot2.0中異常處理

1、項目結構圖

SpringBoot2.0 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址

2、自定義業務異常類

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、統一異常處理格式

1)兩個基礎注解

@ControllerAdvice 定義統一的異常處理類

@ExceptionHandler 定義異常類型對應的處理方式

2)代碼實作

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 {
    /**
     * 自定義業務異常映射,傳回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": "業務異常:ID 不能為空",
     *    "url": "http://localhost:8003/exception01"
     *  }
     */
    @RequestMapping("/exception01")
    public String exception01 () throws ServiceException {
        throw new ServiceException("業務異常:ID 不能為空");
    }

    @RequestMapping("/exception02")
    public String exception02 () throws Exception {
        throw new Exception("出現異常,全體卧倒");
    }
}           
SpringBoot2.0 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址

四、源代碼位址

GitHub位址:知了一笑
https://github.com/cicadasmile
碼雲位址:知了一笑
https://gitee.com/cicadasmile           
SpringBoot2.0 基礎案例(03):配置系統全局異常映射處理一、異常分類二、解決應用啟動後404界面三、SpringBoot2.0中異常處理四、源代碼位址