springboot创建统一异常拦截器全局处理 异常
1.创建Exception类
public class MyException extends RuntimeException {
private ErrorCodeEnum errorCode;
public MyException(ErrorCodeEnum errorCode) {
this.errorCode = errorCode;
}
public ErrorCodeEnum getErrorCode() {
return errorCode;
}
public void setErrorCode(ErrorCodeEnum errorCode) {
this.errorCode = errorCode;
}
}
2.异常枚举类
public enum ErrorCodeEnum {
INCORRECT_PASSWORD(10001, "密码错误!"),
INUSED_USER(10002, "账号错误!"),
INCORRECT_MSG(10003, "验证码错误!"),
ErrorCodeEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
private int code;
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
3.全局异常处理类
@ControllerAdvice
public class MyExceptionHandler {
//捕捉到的异常
@ExceptionHandler(value = MyException.class)
public ResponseEntity<Result> handleServiceException(MyException exception) {
ErrorCodeEnum errorCode = exception.getErrorCode();
return new ResponseEntity(new ResultUtil<String>().setError(errorCode.getCode(), errorCode.getMsg(), ""), HttpStatus.BAD_REQUEST);
}
//其他异常
@ExceptionHandler
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<Result> hadleServerException(Exception exception) {
exception.printStackTrace();
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
String msg = "server error, please try again later";
Class exceptionClazz = exception.getClass();
if (Objects.equals(MissingServletRequestParameterException.class, exceptionClazz)) {
msg = "incorrect parameter";
httpStatus = HttpStatus.BAD_REQUEST;
} else if (Objects.equals(HttpRequestMethodNotSupportedException.class, exceptionClazz)) {
httpStatus = HttpStatus.BAD_REQUEST;
msg = exception.getMessage();
}
return new ResponseEntity(new ResultUtil<String>().setError(httpStatus.value(), msg, ""), httpStatus);
}
}
springboot创建统一异常拦截器全局处理 异常的更多相关文章
- Spring Cloud Gateway之全局异常拦截器
/** * @version 2019/8/14 * @description: 异常拦截器 * @modified: */ @Slf4j public class JsonExceptionHand ...
- 【快学springboot】12.实现拦截器
前言 之前在[快学springboot]6.WebMvcConfigurer配置静态资源和解决跨域里有用到WebMvcConfigurer接口来实现静态资源的映射和解决跨域请求,并且在文末还说了Web ...
- 我心中的核心组件(可插拔的AOP)~第四回 异常拦截器
回到目录 之前说过有关拦截器的文章,第二回 缓存拦截器,事实上,在那讲里说的最多是AOP和缓存组件,对于拦截的概念并没有详细的说明,这一讲,不说AOP,主要说一下拦截器,拦截器Interceptio ...
- Java自定义异常类以及异常拦截器
自定义异常类不难,但下面这个方法,它的核心是异常拦截器类. 就算是在分布式系统间进行传递也可以,只要最顶层的服务有这个异常拦截器类(下例是在 springboot 项目中) 1.自定义异常类,继承自 ...
- SpringBoot从零单排 ------ 拦截器的使用
在项目开发中我们常常需要对请求进行验证,如登录校验.权限验证.防止重复提交等等,通过拦截器来过滤请求.自定义一个拦截器需要实现HandlerInterceptor接口.代码如下: import org ...
- springboot(五)过滤器和拦截器
前言 过滤器和拦截器二者都是AOP编程思想的提现,都能实现诸如权限检查.日志记录等.二者有一定的相似之处,不同的地方在于: Filter是servlet规范,只能用在Web程序中,而拦截器是Sprin ...
- SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆
在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...
- springBoot 2.X-自定义拦截器
package com.cx.springboot.myInter; import javax.servlet.http.HttpServletRequest; import javax.servle ...
- 玩转 SpringBoot 2 快速整合拦截器
概述 首先声明一下,这里所说的拦截器是 SpringMVC 的拦截器 HandlerInterceptor.使用SpringMVC 拦截器需要做如下操作: 创建拦截器类需要实现 HandlerInte ...
随机推荐
- PowerScript语言基础
注释: 以 "//" 开头,其后书写注释内容,常用于单行注释. "/-/"中间的部分为注释,便于多行说明. //这是一个单行注释 INTEGER I I = I ...
- MySQL 5.7.9版本sql_mode=only_full_group_by
这会导致select中只能出现group by后面出现的表的字段. 其实如果使用其他表的字段,聚合函数对无法对应其他表的字段. 建议放在子查询里. 如果想打破这个规则,可以设置sql_mode变量,将 ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal根据前中序数组恢复出原来的树
[抄题]: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Android 发送邮件以及定时发送邮件的实现
本文以腾讯企业邮箱为例,展示如何发送邮件 及相关问题 选择腾讯企业邮箱是因为腾讯企业邮箱一般都是开启了smtp服务 项目地址:https://gitee.com/bimingcong/MySendE ...
- HttpWebRequest 高效并发问题
默认请求连接数 是2,在服务器操作系统上默认为10. 如果不修改这个并发连接限制,那么客户端同时可以建立的 http 连接数就只有2个或10个. System.Net.ServicePointMana ...
- 使用Maven搭建SpringMVC
1.创建Maven Project 注意选择webapp 2.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- 让IE8支持html5中的video标签
这是一篇综合几个前辈的解决方案. 使用video的时候,要遇到的问题. ①不兼容ie9及其以下版本 在<head>里添加两行, 参考张鑫旭前辈的博客,但是在ie8中薄播放. <!-- ...
- Python获取当前类的所有成员属性
# -*- coding: utf-8 -*- class Market(object): def __init__(self): self.title = 'apple' self.count = ...
- Floyd多源最短路
可以对每一个顶点使用Dijkstra算法求多源最短路. 这里我们来介绍另一种解法:Floyd Floyd算法的主要思想是迭代.每次迭代会朝着答案更近一步. 首先定义一个二维数组Dk[i][j](k初始 ...