SpringBoot统一异常处理
/**
* 异常处理器
*/
@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统一异常处理的更多相关文章
- spring 或 springboot统一异常处理
spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...
- SpringBoot 统一异常处理
统一异常处理: @ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactor ...
- SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚
通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...
- springboot统一异常处理类及注解参数为数组的写法
统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...
- springboot统一异常处理及返回数据的处理
一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...
- Springboot统一异常处理(@ControllerAdvice)
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...
- 【异常处理】Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- 配置springboot在访问404时自定义返回结果以及统一异常处理
在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息. 如下是springBoot自带的错误结果信息: ...
- springboot返回统一接口与统一异常处理
springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...
随机推荐
- es索引管理工具-curator
elasticsearch-curator 是官方收购的开源社区周边产品,用来管理es的索引和快照. 官方文档:https://www.elastic.co/guide/en/elasticsear ...
- VUE基本常识
1.运行vue项目 项目根目录git Bash here npm run dev 为了能直接打开项目 配置项目package.json 添加--open 如下图: 2.坑:VUE初写小项目问 ...
- docker 部署nginx
# 下载镜像 docker pull nginx # 挂载 文件 和文件夹必须存在 docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $P ...
- Linux-KVM
一.安装 ①直接使用yum安装:yum -yq install qemu-kvm qemu-kvm-tools virt-install qemu-img bridge-utils libvirt v ...
- css实现横向带箭头步骤流程效果
通过纯css实现圆角的步骤流程效果,扩展性强,可以随意增加减少数量,兼容性ie6+. 具体代码: <style type="text/css"> ul{margin:0 ...
- Python 安装包报错
以管理员身份运行cmd (requests为为所安装的包名) >>pip install reuqestsCollecting reuqests Could not find a vers ...
- vim 插件 -- taglist
taglist 插件是基于ctags生成的tags文件一个工具.主要是用来生成当前文件的结构.如:函数名.变量名结构.具体如下图: 下载 https://www.vim.org/scripts/scr ...
- C#十进制与任意进制的转换
/// <summary> /// 将十进制转换为指定的进制 /// </summary> /// <param name="Val">十进制值 ...
- Ceres Solver 入门稍微多一点
其实ceres solver用了挺多的,可能是入门不精,有时候感觉感觉不理解代码上是怎么实现的,这次就通过ceres的官网仔细看了一些介绍,感觉对cpp了解更好了一些. 跟g2o的比较的话,感觉cer ...
- centos 日志文件
以下介绍的是20个位于/var/log/ 目录之下的日志文件.其中一些只有特定版本采用,如dpkg.log只能在基于Debian的系统中看到./var/log/messages — 包括整体系统信息, ...