欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot詳解實(shí)現(xiàn)自定義異常處理頁(yè)面方法

 更新時(shí)間:2022年06月27日 08:59:53   作者:鳴鼓ming  
SpringBoot是Spring全家桶的成員之一,是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開發(fā)的腳手架

1.相關(guān)介紹

當(dāng)發(fā)生異常時(shí), 跳轉(zhuǎn)到我們自定義的異常處理頁(yè)面.

SpringBoot中只需在靜態(tài)資源目錄下創(chuàng)建一個(gè)error文件夾, 并把異常處理頁(yè)面放入其中, 頁(yè)面的命名與異常錯(cuò)誤代碼對(duì)應(yīng), 如404.html, 500.html.

5xx.html可以對(duì)應(yīng)所有錯(cuò)誤代碼為5開頭的錯(cuò)誤

默認(rèn)靜態(tài)資源目錄為類路徑(resources)下的:

  • /static
  • /public
  • /resources
  • /META-INF/resources

2.代碼實(shí)現(xiàn)

HelloController

package com.limi.springboottest2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @ResponseBody
    @GetMapping("/test1")
    public String test1(){
        int i = 10/0; //模擬500異常
        return "ok";
    }
}

404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>這是自定義404</h1>
</body>
</html>

5xx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>這是自定義5xx</h1>
</body>
</html>

3.運(yùn)行測(cè)試

測(cè)試404

測(cè)試500

使用postman測(cè)試

{
    "timestamp": "2022-06-22T04:12:13.740+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "trace": "java.lang.ArithmeticException: / by zero\r\n\tat com.limi.springboottest2.controller.HelloController.test1(HelloController.java:14),
    "message": "/ by zero",
    "path": "/test1"
}

返回的信息我們可以使用模板引擎(如thymeleaf)獲取并寫入自定義的異常處理頁(yè)面中

到此這篇關(guān)于SpringBoot詳解實(shí)現(xiàn)自定義異常處理頁(yè)面方法的文章就介紹到這了,更多相關(guān)SpringBoot異常處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論