/**
* 异常处理器
*/
@RestControllerAdvice //
public class BDExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
LogService logService;
//
// /**
// * 自定义异常
// */
// @ExceptionHandler(BDException.class)
// public R handleBDException(BDException e) {
// logger.error(e.getMessage(), e);
// R r = new R();
// r.put("code", e.getCode());
// r.put("msg", e.getMessage());
// return r;
// }
//
@ExceptionHandler(DuplicateKeyException.class)
public AppR handleDuplicateKeyException(DuplicateKeyException e) {
logger.error(e.getMessage(), e);
return AppR.error("数据库中已存在该记录");
}
//
//404 @ExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException.class)
// public R noHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException e) {
// logger.error(e.getMessage(), e);
// return R.error(404, "没找找到页面");
// } @ExceptionHandler(AuthorizationException.class)
public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) {
logger.error(e.getMessage(), e);
/*if (HttpServletUtils.jsAjax(request)) {
return AppR.error(403, "未授权");
}*/
return AppR.error(403, "未授权");
}
@ExceptionHandler(SecurityException.class)
public Object handleSecurityException(SecurityException e, HttpServletRequest request) {
//System.out.println("111111111111111");
logger.error(e.getMessage(), e);
return AppR.error(404, "找不到路径");
} @ExceptionHandler({Exception.class})
public Object handleException(Exception e, HttpServletRequest request, HttpServletResponse response) { LogDO logDO = new LogDO();
logDO.setGmtCreate(new Date());
//logDO.setOperation(Const);
logDO.setMethod(request.getRequestURL().toString());
logDO.setParams(e.toString());
/*UserDO current = ShiroUtils.getUser();
if(null!=current){
logDO.setUserId(current.getUserId());
logDO.setUsername(current.getUsername());
}*/
// logService.save(logDO);
//System.out.println(e.getMessage()); //System.out.println(HttpServletUtils.jsAjax(request));
/*if (HttpServletUtils.jsAjax(request)) {
System.out.println("ajax"); }*/
logger.error(e.getMessage(), e);
//System.out.println(response.getStatus());
return AppR.error(500, "系统错误");
}
}

@ControllerAdvice注解的类可以全局拦截指定的异常,并做想要的包装处理,比如跳转到别的页面,或者返回指定的数据格式等等。

@ExceptionHandler:统一处理某一类异常

SpringBoot统一异常处理的更多相关文章

  1. spring 或 springboot统一异常处理

    spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...

  2. SpringBoot 统一异常处理

    统一异常处理: @ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactor ...

  3. SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚

    通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...

  4. springboot统一异常处理类及注解参数为数组的写法

    统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...

  5. springboot统一异常处理及返回数据的处理

    一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...

  6. Springboot统一异常处理(@ControllerAdvice)

    import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...

  7. 【异常处理】Springboot对Controller层方法进行统一异常处理

    Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...

  8. 配置springboot在访问404时自定义返回结果以及统一异常处理

    在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息. 如下是springBoot自带的错误结果信息: ...

  9. springboot返回统一接口与统一异常处理

    springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...

随机推荐

  1. Sublime Text3安装evernote插件

    关键字 Markdown编辑器.Evernote.Sublime Text3   正文 Sublime Text3安装evernote插件方法如下: 1.使用Package Control安装ever ...

  2. 一个axios的简单教程

    转载于:https://www.jianshu.com/p/13cf01cdb81f 转载仅供个人学习 首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js ...

  3. 中国建设工程造价管理系统 http://zaojiasys.jianshe99.com/cecaopsys/

    建造师造价管理系统漏洞提示: 可以绕过,直接进入后台,为了安全起见,我就不多说了,. 里面的数据,从小学,中学,高中,大学,户口,电话,身份等, 很全, 本人没有破坏任何数据,

  4. 学号 2018-2019-20175212 童皓桢《Java程序设计》第5周学习总结

    学号 2018-2019-20175212 <Java程序设计>第5周学习总结 教材学习内容总结 接口 声明接口: interface 名字: 接口体:接口体中只有常量无变量,只有抽象方法 ...

  5. maven作用

    1.控制项目间依赖关系方案 A项目依赖B项目的某些功能.将B项目打包为jar,并在A项目的Library下导入B的jar文件. 这样做有着很明显的缺点:        1.如果在开发过程中,发现B中的 ...

  6. mobike

    1.单向链表的原地反转 public class A { public A next; private int n; public A(int n) { this.n = n; } public st ...

  7. Linux内核分析--进程创建,执行,切换

    学号:351 原创作品转载请注明出处本实验来源 https://github.com/mengning/linuxkernel/ 实验要求 从整理上理解进程创建.可执行文件的加载和进程执行进程切换,重 ...

  8. leetcode python 041首个缺失正数

    ##限定时间复杂度O(n)num=[0,5,3,1,2,-2,4,8,5,6]num=set(num)d=1for i in range(1,len(num)+1):    if d in num:  ...

  9. spark提交jar包时出现unsupported major.minor version 52.0错误的解决方案

    一.问题: 最近在spark集群上做一个项目,打包提交jar包时,出现了unsupported major.minor version 52.0的报错,而在local模式运行却能正常运行! 二.错误原 ...

  10. webpack 大概

    entry output loader: rules: [ {test:匹配文件格式,  use: 使用的loader}, {test:匹配文件格式,  use: 使用的loader}, ...//l ...