springboot的全局异常处理类
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 异常处理器
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 请求方式不支持
*/
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
@ResponseStatus(code = HttpStatus.METHOD_NOT_ALLOWED)
public JsonResult handleException(HttpRequestMethodNotSupportedException e) {
logger.error(e.getMessage(), e);
return JsonResult.error("不支持' " + e.getMethod() + "'请求");
}
@ExceptionHandler(Exception.class)
public JsonResult handleException(Exception e) throws Exception {
logger.error(e.getMessage(), e);
return JsonResult.error("服务器错误,请联系管理员");
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public JsonResult handleValidException(MethodArgumentNotValidException e){
//日志记录错误信息
String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
log.error("=================校验器触发错误================" + defaultMessage);
//将错误信息返回给前台
return JsonResult.error(defaultMessage);
}
@ExceptionHandler(BusinessException.class)
public JsonResult handleValidException(BusinessException e){
//日志记录错误信息
String defaultMessage = e.getMessage();
log.error("=================校验器触发错误================" + defaultMessage);
//将错误信息返回给前台
return JsonResult.error(defaultMessage);
}
}
springboot的全局异常处理类的更多相关文章
- 【springboot】全局异常处理
转自: https://blog.csdn.net/cp026la/article/details/86495196 前言: 开发中异常的处理必不可少,常用的就是 throw 和 try catch, ...
- SpringMVC的处理器全局异常处理类
SpringMVC的处理器全局异常处理类 package com.huawei.utils; import org.springframework.web.servlet.HandlerExcepti ...
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
- springmvc、 springboot 项目全局异常处理
异常在项目中那是不可避免的,通常情况下,我们需要对全局异常进行处理,下面介绍两种比较常用的情况. 准备工作: 在捕获到异常的时候,我们通常需要返回给前端错误码,错误信息等,所以我们需要手动封装一个js ...
- 【转载】springboot四 全局异常处理
http://tengj.top/2018/05/16/springboot13/ https://www.jb51.net/article/110533.htm
- 【第二十三章】 springboot + 全局异常处理
一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...
- 第二十三章 springboot + 全局异常处理
一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...
- springboot中 简单的SpringMvc全局异常处理
1.全局异常处理类:GlobalExceptionHandler.java package com.lf.exception; import java.util.HashMap; import jav ...
- springboot 全局异常处理
springboot 全局异常处理 研究了半天springboot的全局异常处理,虽然还是需要再多整理一下,但是对于常见的404和500足以可以区分开,能够根据这两个异常分别处理 首先配置视图解析路径 ...
随机推荐
- flutter系列之:Material主题的基础-MaterialApp
简介 为了简化大家的使用,虽然flutter推荐所有的widget都有自己来进行搭建,但是在大框架上面,flutter提供了Material和Cupertino两种主题风格的Widgets集合,大家可 ...
- 输入法词库解析(二)搜狗拼音细胞词库.scel(.qcel)
详细代码:https://github.com/cxcn/dtool 前言 .scel 是搜狗拼音输入法所使用的细胞词库格式,可以在 https://pinyin.sogou.com/dict/ 下载 ...
- 装饰Hexo博客以及部署个人站点
我的博客最开始采用的是Hexo+hexo-theme-next搭建的,使用GitHub Pages托管并进行自动化部署,写文发布的流程非常简单方便,云端写作发布也轻而易举. 本来事情到这里就应该结束了 ...
- 【学习笔记】RNN算法的pytorch实现
一些新理解 之前我有个疑惑,RNN的网络窗口,换句话说不也算是一个卷积核嘛?那所有的网络模型其实不都是一个东西吗?今天又听了一遍RNN,发现自己大错特错,还是没有学明白阿.因为RNN的窗口所包含的那一 ...
- Beats:Beats在Kibana中的集中管理
我们可以通过在命令行中对我们的Beats进行管理,比如我们可以启动metric几个模块,我们可以通过如下的命令来执行: ./metricbeat modules enable apache mysql ...
- Dockerfile文件:设置变量启动的时候传递进去
from openjdk:8-jdk-alpine RUN ln -sf /usr/share/zoneinfo/Asia/shanghai /etc/localtime RUN echo 'Asia ...
- CentOS7部署FastDFS+nginx模块
软件下载 # 已经事先把所需软件下载好并上传到/usr/local/src目录了 https://github.com/happyfish100/libfastcommon/archive/V1.0. ...
- 使用metricbeat监控system和nginx
下载并解压缩metricbeat metricbeat.yml配置文件设置: setup.kibana: host: "192.168.75.21:5601" output.ela ...
- 文档的CURD
Index方式,需要指定ID 如果ID不存在,创建新的文档,状态是created 如果ID存在,会先删除现有ID的文档,然后创建一个新文档,ID数加1,状态是updated PUT test/_doc ...
- host主机监控规则
1.先在 Prometheus 主程序目录下创建rules目录,然后在该目录下创建 host.yml文件,内容如下: 内容很多,可以根据实际情况进行调整. 规则参考网址:https://awesome ...