《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler
来源:互联网 发布:web优化 sysctl 编辑:程序博客网 时间:2024/05/16 12:36
统一异常处理
系统有一个统一异常处理的功能,可减少重复代码,又便于维护。
用@ControllerAdvice和@ExceptionHandler两个注解来做异常的统一处理。
@ControllerAdvice:作用于所有@Controller标注的Controller类
@ExceptionHandler:作用于所有@RequestMapping标注的方法抛出的指定类型的异常。
代码实例
ExceptionHandlerAdvice.java
package com.restfeel.adviceimport org.springframework.web.bind.annotation.ControllerAdviceimport org.springframework.web.bind.annotation.ExceptionHandlerimport org.springframework.web.context.request.WebRequestimport org.springframework.web.servlet.ModelAndView/** * Created by jack on 2017/3/30. * * 系统全局统一异常处理 */@ControllerAdviceclass 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
运行
这个思路很有实用价值,大大减少了系统出问题debug,去赋值粘贴到google,baidu, stackoverflow的手工操作。欢迎试用。
阅读全文
0 0
- 《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler
- spring使用@ExceptionHandler、@ControllerAdvice统一异常处理
- @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
- @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
- Spring中@ControllerAdvice注解配合@ExceptionHandler实现统一异常处理
- springMVC-异常的全局处理 @ControllerAdvice , @ExceptionHandler(Exception.class)
- SpringBoot 统一异常处理 @ControllerAdvice 失效
- SpringBoot 统一异常处理--- @ControllerAdvice 使用
- SpringBoot基于@ControllerAdvice配置全局异常处理
- springboot 全局异常-ExceptionHandler
- 统一异常处理@ExceptionHandler
- 统一异常处理@ExceptionHandler
- 使用@ControllerAdvice及@ExceptionHandler(value = Exception.class)全局异常处理
- Spring中的@ControllerAdvice注解配合@ExceptionHandler使用实现异常处理
- @controllerAdvice 统一处理异常(spring-mvc)
- SpringBoot--统一异常处理
- SpringBoot-统一异常处理
- springboot统一异常处理
- 【tyvj1098】任务安排(dp)
- 《Springboot极简教程》Springboot使用Kotlin和Java混合编程
- Elam的caffe笔记之配置篇(六):Centos6.5下编译caffe及caffe的python3.6接口
- 《Springboot极简教程》@EnableMongoRepositories路径配置:APPLICATION FAILED TO START: Parameter 0 of constructor in
- 《Springboot极简教程》MappingMongoConverter:Failed to convert from type [java.lang.String] to type [long] for value 'null'; ne
- 《Springboot极简教程》系统异常全局统一处理:@ControllerAdvice plus @ExceptionHandler
- 《JSP极简教程》jsp c:forEach用法
- RequireJS极简入门教程
- 分布式系统理论进阶
- 比CPU效率高30倍阿里云推出FPGA人工智能加速服务
- VMWare虚拟机提供的桥接,nat和主机模式的区别
- 马云说中国是在互联网上的国家 一张图读懂中国到底有多强
- RequireJS 入门指南
- 《JSP极简教程》在jsp页面获取session对象