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 封装统 ...
随机推荐
- Write Sling Servlet using a resource type and selector
@SlingServlet( resourceTypes = "geometrixx/components/homepage", selectors = "data&qu ...
- 【转载】Linux常用命令
Linux常用命令大全(非常全!!!) 转载出处:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html 系统信息 arch 显示机器的处理器架构( ...
- VMware下Debian开发环境部署之常见问题记录
本文讲介绍windows作为宿主机,linux虚拟机作为编译环境的开发环境搭建中最常用到的三个问题,详细描述了解决过程. 目录: 1.网路配置: 2.分辨率设置: 3.共享网盘设置: 1.网络设置,V ...
- angular组件
- Android ORC文字识别之识别身份证号等(附源码)
项目地址https://github.com/979451341/OrcTest 我们说说实现这个项目已实现的功能,能够截图手机界面的某一块,将这个某一块图片的Bitmap传给tess-two的代码来 ...
- element-ui的那些坑与总结
tags: 默认情况下,下划线是文本宽度 如果要加宽,则可以设置文本(label)的padding, 常规情况下,无法改label宽度,因为他是动态计算的 不过,可以通过自定义,把label拿出来,自 ...
- Spring、SpringMVC区别
1. 为什么使用Spring ? 1). 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合. 2). AOP编程的 ...
- Python类之类的成员
对于一个学C++的朋友来说,Python类中,哪些是私有成员,哪些是共有成员,估计一直傻傻分不清. 一.本篇博客要解决的问题: Python类中,哪些是私有成员?哪些是共有成员? 二. 关于Pytho ...
- 编译安装LAMP
编译安装MariaDB 创建MariaDB安装目录.数据库存放目录.建立用户和目录 先创建一个名为mysql且没有登录权限的用户和一个名为mysql的用户组,然后安装mysql所需的依赖库和依赖包,最 ...
- Unity 关于AssetBundle读取场景
一. 1.关于如何打包成ab包,就不多说了,网上很多教程,siki学院也有siki老师的免费视频教程挺详细的,可以看看 http://www.sikiedu.com/my/course/74 2.为了 ...