springboot的全局异常处理

. 新建一个类GlobalDefaultExceptionHandler
在class上注解 @ControllerAdvice 方法上注解 @ExceptionHandler(value=Exception.class)

这样程序出错, 就会返回默认配置的信息了

package com.iwhere.scrapy.exception;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import com.iwhere.scrapy.enums.PublicRespEnum;
import com.iwhere.scrapy.model.ResponseModel; @ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(Exception.class)
@ResponseBody
public ResponseModel defaultHandler(HttpServletRequest request, Exception exception) {
StringBuffer url = request.getRequestURL();
String params = request.getQueryString();
// 发生地点
int lineNum = ;
String className = null;
String methodName = null;
StackTraceElement[] st = exception.getStackTrace();
if (ArrayUtils.isNotEmpty(st)) {
lineNum = st[].getLineNumber();
className = st[].getClassName();
methodName = st[].getMethodName();
} String message = exception.getMessage();
LOGGER.info("Exception: {}#{}() 在第{}行发生{}异常!!! url: {}, params: {}, info: {}", className, methodName, lineNum,
exception.getClass().getName(), url, params, message); return ResponseModel.getModel(PublicRespEnum.RESCODE_500, message);
} }

其中ResponseModel 是一个统一返回模型

原创地址: http://www.cnblogs.com/wenbronk/p/6850785.html 转载请注明出处, 谢谢

springboot-20-全局异常处理的更多相关文章

  1. SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务

    ============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...

  2. springmvc、 springboot 项目全局异常处理

    异常在项目中那是不可避免的,通常情况下,我们需要对全局异常进行处理,下面介绍两种比较常用的情况. 准备工作: 在捕获到异常的时候,我们通常需要返回给前端错误码,错误信息等,所以我们需要手动封装一个js ...

  3. 【springboot】全局异常处理

    转自: https://blog.csdn.net/cp026la/article/details/86495196 前言: 开发中异常的处理必不可少,常用的就是 throw 和 try catch, ...

  4. 【转载】springboot四 全局异常处理

    http://tengj.top/2018/05/16/springboot13/ https://www.jb51.net/article/110533.htm

  5. springboot的全局异常处理类

    import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import or ...

  6. springboot 全局异常处理

    springboot 全局异常处理 研究了半天springboot的全局异常处理,虽然还是需要再多整理一下,但是对于常见的404和500足以可以区分开,能够根据这两个异常分别处理 首先配置视图解析路径 ...

  7. springboot实现定时任务,异步操作,统一结果返回,全局异常处理,拦截器及事务处理

    本文是作者原创,版权归作者所有.若要转载,请注明出处. 本文都是springboot的常用和实用功能,话不多说开始吧 定时任务 1.启动类开启注解 @EnableScheduling //开启基于注解 ...

  8. 【第二十三章】 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  9. 第二十三章 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  10. springboot中 简单的SpringMvc全局异常处理

    1.全局异常处理类:GlobalExceptionHandler.java package com.lf.exception; import java.util.HashMap; import jav ...

随机推荐

  1. 使用Docker、CoreOS、Mesos部署可扩展的Web应用

    [编者的话]本文作者重点介绍了如何使用Docker.CoreOS.Mesos.Vulcand.对象存储来部署一个可扩展的Web应用,他首先介绍了为什么要选择这些工具以及与其它工具相比这些工具的优势.紧 ...

  2. CodeIgniter使用中写的一些文章

    CI的captcha替代类库:  http://www.ifixedbug.com/posts/codeigniter-captcha-library 原生的captcha不是太好用,自己组装一个吧. ...

  3. sql server partition分区与group by 分组

    例子:在一个StudentScore表中,有序号ID,班级ClassId,学生姓名Name,性别Sex,语文成绩ChineseScore,数学成绩MathScore,平均成绩AverageScore等 ...

  4. sharepoint 2013工具

    caml designer 2013 生成caml工具 http://camlbuilder.codeplex.com/ 设计工具 CamlDesigner2013 爬网工具 HtmlAgilityP ...

  5. linux 使用内存作为 /tmp 临时文件夹

    方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...

  6. 【OCP题库】最新CUUG OCP 12c 071考试题库(65题)

    65.(22-16) choose the best answer: The CUSTOMERS table has the following structure: You need to writ ...

  7. “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  8. “全栈2019”Java异常第二十二章:try-with-resources语句详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  9. String类的操作方法

    因String属于java核心包lang包的东西,所以不需要导包! /* * 字符串操作 * */ String name = "jck"; String name1 = &quo ...

  10. 结合业务,精炼SQL

    现代网站,性能的瓶颈都围绕着数据库的性能来谈.数据库是存储的核心部件,在日益增长的流量中会凸显数据库的性能瓶颈.从<淘宝技术十年>书中来看,淘宝发展历程中从MYSQL换成了ORACLE又换 ...