<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                下面提供4種方式來定制錯誤數據,最好的一種是第3種。 [TOC] # 1. SpringBoot默認的錯誤數據 當沒有定制任何錯誤處理頁面和數據時,訪問一個不存在的地址 http://localhost:8080/abc SpringBoot根據請求的客戶端不同響應不同的數據。 (1)瀏覽器訪問,默認顯示html。 ![](https://img.kancloud.cn/59/6f/596f86ddfe4e3389e38413031c7359b6_1394x308.jpg) (2)非瀏覽器訪問,默認顯示json。 ![](https://img.kancloud.cn/0d/be/0dbe4ce8bff2239e157c9f26f9fc2280_1470x384.jpg) <br/> # 2. 自定義異常處理器(一) 無論是瀏覽器訪問,還是非瀏覽器方法,顯示的異常數據都是JSON。 <br/> 步驟如下: **1. 自定義異常處理器** ```java 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 MyExceptionHandler { @ResponseBody @ExceptionHandler({RuntimeException.class}) // 要處理的異常 public Map<String, Object> handlerException(Exception e) { Map<String, Object> map = new HashMap<>(); map.put("code", "我自定義的狀態碼500000"); map.put("message", "這是我自定義的異常消息哦!"); return map; } } ``` **2. controller層** ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class IndexController { @GetMapping("/index") public String index(@RequestParam("id") int id) { if (id == 1) { throw new RuntimeException("RuntimeException!"); } return ""; } } ``` **3. 測試** 啟動項目后訪問一個不存在的地址 http://localhost:8080/index?id=1 ,得到如下結果。 (1)瀏覽器訪問 ![](https://img.kancloud.cn/0f/53/0f5345dc4a7971a155825a448a1f3d77_1411x170.jpg) (2)非瀏覽器訪問 ![](https://img.kancloud.cn/f6/15/f615386a09dcd8913c4e56c35d4a8cf8_1483x333.jpg) <br/> # 3. 自定義異常處理器(二) 瀏覽器訪問顯示錯誤數據顯示為html,非瀏覽器訪問顯示為json。 <br/> 步驟如下: **1. 自定義異常處理器** ```java import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @ControllerAdvice // 標注該類為異常處理器 public class MyExceptionHandler { @ExceptionHandler({RuntimeException.class}) // 要處理的異常 public String handlerException(Exception e, HttpServletRequest request) { Map<String, Object> map = new HashMap<>(); map.put("code", "我自定義的狀態碼500000"); map.put("message", "這是我自定義的異常消息哦!"); // 為了后面測試,請把status_code設置為500 request.setAttribute("javax.servlet.error.status_code", 500); request.setAttribute("ext", map); return "forward:/error"; // 這個error請求由SpringBoot默認提供好了的 } } ``` **2. 繼承DefaultErrorAttributes** ```java import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.stereotype.Component; import org.springframework.web.context.request.WebRequest; import java.util.Map; @Component public class MyErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace); map.put("company", "發生異常了,你能拿我怎么辦!"); map.put("message", "發生異常了,You get out 吧!"); Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0); map.put("ext", ext); return map; } } ``` **3. controller層** ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class IndexController { @GetMapping("/index") public String index(@RequestParam("id") int id) { if (id == 1) { throw new RuntimeException("RuntimeException!"); } return ""; } } ``` **4. 錯誤模板** *`resources/template/error/5xx.html`* ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>5xx</title> </head> <body> <div>ext.Message:[[${ext.message}]]</div> <div>Message:[[${message}]]</div> <div>code:[[${ext.code}]]</div> <div>company:[[${company}]]</div> </body> </html> ``` **5. 測試** 啟動項目后訪問 http://localhost:8080/index?id=1 得到如下結果。 (1)瀏覽器訪問 ![](https://img.kancloud.cn/bb/95/bb9568355f94ce10c5c7b8b23bea3fd0_1461x251.jpg) (2)非瀏覽器訪問 ```json { "timestamp": "2021-07-20T07:50:38.412+0000", "status": 500, "error": "Internal Server Error", "message": "發生異常了,You get out 吧!", "trace": "java.lang.RuntimeException: RuntimeException!\r\n\tat com.example.boot.controller.IndexController.index(IndexController.java:13)\r\n\tat ... "path": "/index", "company": "發生異常了,你能拿我怎么辦!", "ext": { "code": "我自定義的狀態碼500000", "message": "這是我自定義的異常消息哦!" } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看