1. 程式人生 > >Spring boot(9) 的異常,以及異常頁面的處理

Spring boot(9) 的異常,以及異常頁面的處理

一,如果發生錯誤,

返回的頁面內容如下

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Aug 04 16:02:48 CST 2016 There was an unexpected error (type=Internal Server Error, status=500).


二,在src/main/resources/templates下面新建error.html,則出現異常會跳轉到error.html頁面(我的spring boot採用了thymeleaf模板)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>error_test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h5>error page</h5>
</body>
</html>

但是以上的最終展示都對使用者不夠友好。

三,自定義異常處理

@ControllerAdvice
public class ExeptionHandler {

	@ExceptionHandler(value = Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("e", e);
		mv.addObject("uri", req.getRequestURI());
		return mv;
	}
}
然後修改error.html頁面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>error_test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h5>error page</h5>
	<div th:text="${uri}"></div>
	<div th:text="${e.toString()}"></div>
</body>
</html>
測試示例:
		try {
			int a = 100/0;
		} catch (Exception e) {
			throw e;
		}
頁面最終顯示
error page
/redis/get java.lang.ArithmeticException: / by zero

四,tips:可以在application.properties裡自定義錯誤頁面的請求路徑

比如

error.path=/error

相關推薦

Spring boot(9) 的異常以及異常頁面處理

一,如果發生錯誤, 返回的頁面內容如下 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Th

spring boot 下 500 404 403錯誤頁面處理

1.場景說明 Spring Boot 下http 500 404 403錯誤頁面處理。同時文章是在《SpringBoot介面服務處理Whitelabel Error Page》基礎上新增內容,更加詳細的說明請參考這篇文章。本文直說明差異部分。 2

springboot整合mapper-spring-boot-starter時使用SelectByPrimaryKey時出現的型別轉換異常

我也是被這個東西給整蒙了 使用SelectByPrimaryKey時出現的型別轉換異常 java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String at org

Spring Boot整合shiro出現UnavailableSecurityManagerException 異常

問題描述:由於在java過濾器中通過User token = (User) SecurityUtils.getSubject().getPrincipal();獲取當前的登入使用者的資訊,報UnavailableSecurityManagerException這個異常。spr

Spring Boot使用RabbitMQ出現詭異異常:Failed to send reply with payload 'OK'Cannot determine ReplyTo message

今天專案中出現一個詭異的異常:org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception at org.sprin

Spring Boot使用thymeleaf模板時報異常:template might not exist or might not be accessible by any of the configured Template Resolvers

logs pla 開頭 spring 方法 temp ring mode acc 錯誤如下: template might not exist or might not be accessible by any of the configured Template R

spring boot 1.5.4 統一異常處理(九)

springboot springboot1.5.4 springboot整合springdatajpa springboot集成jdbctemplate springboot異常處理 上一篇:springboot 1.5.4 配置文件詳解(八) 1 Spring Boot統一異

Spring boot 前後臺分離項目 怎麽處理spring security 拋出的異常

輸入 etl tor login 異常 stat dup valid load 最近在開發一個項目 前後臺分離的 使用 spring boot + spring security + jwt 實現用戶登錄權限控制等操作。但是 在用戶登錄的時候,怎麽處理spring sec

解決Myeclipse啟動Spring Boot項目報出莫名異常

ima 分享 重新 app 異常 ips nbsp myeclips 有時 有時候明明代碼、配置都是正確的,但是一啟動卻報出莫名其妙的異常。 主要原因是resource包下的xml、yml文件或者其他配置文件路徑不正確,解決方法如下: 第一步、 第二步、如果Exclude

spring boot中使用dubbo出現異常java.lang.IllegalStateException: <dubbo:service interface="" /> interface not allow null!

今天我在閒的時候,自己在springBoot中使用dubbo的時候,啟動的時候,發現啟動失敗,報java.lang.IllegalStateException: <dubbo:service interface="" /> interface not allow null! 這個異常。我首先看了是

Spring Boot實戰系列(4)統一異常處理

專案開發中保證零異常似乎是不可能的,不論是系統異常還是程式本身的編碼問題造成的異常資訊都要以一種約定的資料結構返回,友好的處理方式在前後端分離模式下(後端提供API介面給到前端)能大大增加大家的溝通、工作效率。基於Spring Boot進行異常統一處理,本文中主要用到@ControllerAdvice註

Spring Boot 單檔案多檔案上傳以及將檔案寫入響應中

單檔案上傳 配置檔案設定 @Component public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement(){ Multipa

Spring Boot中使用thymeleaf以及各種取值判斷選擇擷取等方式

Spring Boot中使用thymeleaf Spring Boot支援FreeMarker、Groovy、Thymeleaf和Mustache四種模板解析引擎,官方推薦使用Thymeleaf。 spring-boot-starter-thymeleaf 在Spring B

Spring Boot 2 Webflux的全域性異常處理

本文首先將會回顧Spring 5之前的SpringMVC異常處理機制,然後主要講解Spring Boot 2 Webflux的全域性異常處理機制。 SpringMVC的異常處理 Spring 統一異常處理有 3 種方式,分別為: 使用 @ExceptionHandler 註解 實現 Handl

Spring Boot下 org.hibernate.AnnotationException異常的一種解決方法

異常如下 Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.xiaomo.xxxx.model.yyy.Clock.user references an un

spring boot jackon反序列化異常

1.Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token; 檢測到該欄位是string,而反序列化結果應該是list 2.Illegal unquoted

mybatis傳多個引數(不使用@param註解情況下),3.4.2版本之後使用#{0}-#{n}引起的引數繫結異常以及settings屬性中useActualParamName的作用。

解決方案: mybatis的xml配置檔案中宣告settings屬性的useActualParamName引數值為false <setting name="useActualParamName" value="false" /> 程式碼展示

Spring boot 解決 hibernate no session異常

啟動類中加入  @Beanpublic OpenEntityManagerInViewFilter openEntityManagerInViewFilter(){ return new OpenEntityManagerInViewFilter();}配置檔案中加入 spring.jpa.open-i

Spring-Boot--日誌操作【全域性異常捕獲訊息處理☞日誌控制檯輸出+日誌檔案記錄】

       最好的演示說明,不是上來就貼配置檔案和程式碼,而是,先來一波配置檔案的註釋,再來一波程式碼的測試過程,最後再出個技術在專案中的應用效果,這樣的循序漸進的方式,才會讓讀者更加清楚的理解一項技

Spring Boot 學習筆記 10 : Flyway 異常

最近使用 SpringBoot 整合 Mybatis 開發專案,關於資料庫表結構的生成決定採用 Flyway 或者 Liquibase 這樣的資料庫版本工具,或者叫資料庫遷移工具。在使用 Flyway 的過程中,遇到一些問題先記錄下來。 在 app