前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler统一异常处理代码实例运行

《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler统一异常处理代码实例运行

作者头像
一个会写诗的程序员
发布2018-08-20 10:24:46
6990
发布2018-08-20 10:24:46
举报

统一异常处理

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

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

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

代码实例

ExceptionHandlerAdvice.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<%--
  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的手工操作。欢迎试用。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 统一异常处理
  • 代码实例
  • 运行
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档