接着前面幾篇web處理請求的博文,本文將說明,當出現異常的場景下,如404請求url不存在,,403無權,500服務器異常時,咱們能夠如何處理html
原文友鏈: SpringBoot系列教程web篇之40四、500異常頁面配置java
<!-- more -->git
首先得搭建一個web應用纔有可能繼續後續的測試,藉助SpringBoot搭建一個web應用屬於比較簡單的活;github
建立一個maven項目,pom文件以下web
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7</version> <relativePath/> <!-- lookup parent from update --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.45</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
依然是通常的流程,pom依賴搞定以後,寫一個程序入口spring
/** * Created by @author yihui in 15:26 19/9/13. */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
在SpringBoot項目中,自己提供了一個默認的異常處理頁面,當咱們但願使用自定義的404,500等頁面時,能夠如何處理呢?json
在默認的狀況下,要配置異常頁面很是簡單,在資源路徑下面,新建 error
目錄,在下面添加400.html
, 500html
頁面便可服務器
項目結構如上,注意這裏的實例demo是沒有使用模板引擎的,因此咱們的異常頁面放在static目錄下;若是使用瞭如FreeMaker模板引擎時,能夠將錯誤模板頁面放在template目錄下websocket
接下來實際測試下是否生效, 咱們先定義一個可能出現服務器500的服務app
@Controller @RequestMapping(path = "page") public class ErrorPageRest { @ResponseBody @GetMapping(path = "divide") public int divide(int sub) { System.out.println("divide1"); return 1000 / sub; } }
請求一個不存在的url,返回咱們定義的400.html
頁面
<html> <head> <title>404頁面</title> </head> <body> <h3>頁面不存在</h3> </body> </html>
請求一個服務器500異常,返回咱們定義的500.html
頁面
<html> <head> <title>500頁面</title> </head> <body> <h2 style="color: red;">服務器出現異常!!!</h2> </body> </html>
看上面的使用比較簡單,天然會有個疑問,這個異常頁面是怎麼返回的呢?
從項目啓動的日誌中,注意一下RequestMappingHandlerMapping
能夠發現裏面有個/error
的路徑不是咱們本身定義的,從命名上來看,這個多半就是專門用來處理異常的Controller -> BasicErrorController
, 部分代碼以下
@Controller @RequestMapping("${server.error.path:${error.path:/error}}") public class BasicErrorController extends AbstractErrorController { @Override public String getErrorPath() { return this.errorProperties.getPath(); } @RequestMapping(produces = "text/html") public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { HttpStatus status = getStatus(request); Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes( request, isIncludeStackTrace(request, MediaType.TEXT_HTML))); response.setStatus(status.value()); ModelAndView modelAndView = 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 = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL)); HttpStatus status = getStatus(request); return new ResponseEntity<>(body, status); } }
這個Controller中,一個返回網頁的接口,一個返回Json串的接口;咱們前面使用的應該是第一個,那咱們什麼場景下會使用到第二個呢?
Accept
,來限定咱們只但願獲取json的返回便可本篇內容比較簡單,概括爲兩句話以下
/error
目錄下盡信書則不如,以上內容,純屬一家之言,因我的能力有限,不免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
下面一灰灰的我的博客,記錄全部學習和工做中的博文,歡迎你們前去逛逛