1. 程式人生 > >/error處理

/error處理

RR path 簡述 ram set 自定義 技術分享 att web

1 BasicErrorController

  1.1 簡述

    SpringMVC框架在出現錯誤時有一個默認的錯誤請求 /error;出現異常之後執行/error請求之前框架會判斷出現異常的請求類型,然後根據請求類型判斷是返回一個HTML頁面還是JSON格式的錯誤信息

  1.2 源碼分析

    BasicErrorController主要處理 /error 請求,該類中有兩個處理 /error 請求的方法,分別是 errorHtml 和 error,前者返回的是HTML頁面後者返回的是JSON格式的數據

    技巧01:如果是瀏覽器發出的請求出現異常後跳轉到 /error 就會執行 errorHtml,然後返回一個HTML頁面

    技巧02:如果是其他客戶端發出請求出現異常後跳轉到 /error 就會執行 error ,然後返回一個JSON格式數據

技術分享圖片
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.autoconfigure.web.servlet.error;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace; import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping({"${server.error.path:${error.path:/error}}"}) public class BasicErrorController extends AbstractErrorController { private final ErrorProperties errorProperties; public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) { this(errorAttributes, errorProperties, Collections.emptyList()); } public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) { super(errorAttributes, errorViewResolvers); Assert.notNull(errorProperties, "ErrorProperties must not be null"); this.errorProperties = errorProperties; } public String getErrorPath() { return this.errorProperties.getPath(); } @RequestMapping( produces = {"text/html"} ) public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { HttpStatus status = this.getStatus(request); Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML))); response.setStatus(status.value()); ModelAndView modelAndView = this.resolveErrorView(request, response, status, model); return modelAndView != null ? modelAndView : new ModelAndView("error", model); } @RequestMapping @ResponseBody public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL)); HttpStatus status = this.getStatus(request); return new ResponseEntity(body, status); } protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { IncludeStacktrace include = this.getErrorProperties().getIncludeStacktrace(); if (include == IncludeStacktrace.ALWAYS) { return true; } else { return include == IncludeStacktrace.ON_TRACE_PARAM ? this.getTraceParameter(request) : false; } } protected ErrorProperties getErrorProperties() { return this.errorProperties; } }
BasicErrorController.java

  1.3 實例

    1.3.1 瀏覽器

      利用瀏覽器請求一個不存在的url

        技術分享圖片

    1.3.2 postman

      利用postman請求一個不存在的url

        技術分享圖片

  1.4 修改默認的HTML頁面

    如果是瀏覽器發送的請求出現異常後,跳轉到 /error 請求後會根據錯誤編號返回對應的HTML文檔,例如:404.html、500.html

    技巧01:我們可以重寫這些404.html等文檔,直接在 src/main/resources 路徑下創建一個 resources/error 文件夾,形如:

      技術分享圖片

技術分享圖片
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>你所訪問的資源不存在</h2>
</body>
</html>
404.html

    技巧02:在到 error 文件夾內部創建 404.html 等異常HTML文件即可,重啟項目後利用瀏覽器訪問一個不存在的url就會返回自定義的那個404.html文檔,例如:

      技術分享圖片

/error處理