您的位置:首页 > 编程语言 > Java开发

《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler

2017-09-13 14:26 591 查看

统一异常处理

系统有一个统一异常处理的功能,可减少重复代码,又便于维护。

用@ControllerAdvice和@ExceptionHandler两个注解来做异常的统一处理。

@ControllerAdvice:作用于所有@Controller标注的Controller类
@ExceptionHandler:作用于所有@RequestMapping标注的方法抛出的指定类型的异常。

代码实例

ExceptionHandlerAdvice.java

package com.restfeel.advice

import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.ModelAndView

/**
* Created by jack on 2017/3/30.
*
* 系统全局统一异常处理
*/
@ControllerAdvice
class ExceptionHandlerAdvice {
@ExceptionHandler(value = Exception::class) //表示捕捉到所有的异常,你也可以捕捉一个你自定义的异常
fun exception(exception: Exception, request: WebRequest): ModelAndView {
val modelAndView = ModelAndView("jsp/error")//error页面
modelAndView.addObject("errorMessage", exception.message)
modelAndView.addObject("stackTrace", exception.stackTrace)
return modelAndView

}
}

error.jsp

<%--
Created by IntelliJ IDEA.
User: jack
Date: 2017/3/30
Time: 02:08
To change this template use File | Settings | File Templates.
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions"
prefix="fn" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="header.jsp"></jsp:include>
</head>
<body>
<jsp:include page="top-nav.jsp"></jsp:include>
<div class="col-sm-12">
<h1>系统异常统一处理</h1>
<h3>${errorMessage}></h3>
<h2>Debug</h2>
<a href="https://www.google.com/webhp?hl=zh-CN#safe=strict&hl=zh-CN&q=${errorMessage}"
class="btn btn-primary btn-lg" target="_blank" id="Google">Google</a>
<a href="https://www.baidu.com/s?wd=${errorMessage}" class="btn btn-info btn-lg"  target="_blank" id="Baidu">Baidu</a>
<a href="http://stackoverflow.com/search?q=${fn:substring(errorMessage,0,100)}"
class="btn btn-default btn-lg"  target="_blank" id="StackOverFlow">StackOverFlow</a>

<h2>异常堆栈跟踪日志StackTrace</h2>
<code>
<c:forEach items="${stackTrace}" var="line">
${line}
</c:forEach>
</code>
</div>

<footer class="panel-footer rest-footer">
<div class="footer-nav">
<a href="/" target="_blank" hidefocus="true">RestFeel</a>
|
<a href="https://universsky.github.io/" target="_blank">光剑免费图书馆</a>
|
<a href="https://jason-chen-2017.github.io/Jason-Chen-2017/" target="_blank">博客</a>
|
<a href="#" target="_blank" hidefocus="true">微信公众号:ols-lightshadow</a>
</div>
<div class="copyright">RestFeel 2017-7017</div>
</footer>

<!-- JavaScript -->
<script data-main="js/main" src="js/libs/require/require.js"></script>

<script type="text/javascript">
$(function () {
$('#Google').click()
$('#Baidu').click()
$('#StackOverFlow').click()
})
</script>

</body>
</html>

源码工程:
https://github.com/Jason-Chen-2017/restfeel

运行



系统异常统一处理



螢幕快照 2017-03-30 13.29.14.png



螢幕快照 2017-03-30 13.29.04.png



螢幕快照 2017-03-30 13.28.55.png

这个思路很有实用价值,大大减少了系统出问题debug,去赋值粘贴到google,baidu, stackoverflow的手工操作。欢迎试用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: