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. R12.1.3 patch9239090

    参考文档:Oracle E-Business Suite Release 12.1.3 Readme [ID 1080973.1]1.调整参数_disable_fast_validate=TRUEpg ...

  2. 在Github注册账户

    https://github.com/JasonHaoz

  3. 查看JVM内存使用情况

    Runtime run = Runtime.getRuntime(); long max = run.maxMemory()/(1024*1024); long total = run.totalMe ...

  4. 大坑记录 - shell脚本删除操作

    背景 jenkins执行去执行shell命令,其中引用了一些jenkins的变量,如${WORKSPACE}这种,因为每次执行jenkins比较慢,于是想复制脚本出来想本地调试一下,直接复制了脚本过来 ...

  5. 对象初始化的完整过程(C#)

    1.静态构造函数 在引入本文的主题之前,我们先来铺垫一下吧,看看静态构造函数的概念及用途. C#中允许创建无参数构造函数,该函数仅执行一次.它一般被用来初始化静态字段.CLR不能保证在某个特定时刻执行 ...

  6. WPF定时刷新UI界面

    代码: using NHibernate.Criterion; using System; using System.Collections.Generic; using System.Collect ...

  7. windows 10 RelativePanel

    The new RelativePanel implements a style of layout that is defined by the relationships between its ...

  8. 使用ssm实现校验密码

    由于审题不清,在完成作业“servlet实现进行用户名和密码验证”中使用了jdbc连接数据库的方式实现,没用静态方式验证,故本次作业使用ssm实现 本次作业上传到百度网盘:链接:https://pan ...

  9. 回去看linux的指令1

    SYNC CL : MSM8953 @ CL#:12212299 PROJECT PATH : // Platform / N / NILE / COMBINATION / MSM8953 Cross ...

  10. loadrunner录制的基本知识

    1.http/html录制选择web_url,如下图所示: 开始录制->Options->Recording->HTML Advanced->选择web_url->OK ...