spring boot配置统一异常处理
基于@ControllerAdvice的统一异常处理
>.这里ServerException是我自定义的异常,和普通Exception分开处理
>.这里的RequestResult是我自定义的请求返回结果对象
ExceptionResolver.class
import com.dawn.blogspot.common.exception.ServerException;
import com.dawn.blogspot.common.response.RequestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; /**
* @author TangZedong
* @apiNote 统一异常处理器
* @since 2018/5/21 11:55
*/
@ControllerAdvice
public class ExceptionResolver {
// spring boot 2.0.4版本内部集成了log4j,所以不需要额外的配置log4j
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionResolver.class); /**
* 处理自定义异常{@link ServerException},并返回错误信息
*/
@ResponseBody
@ExceptionHandler(value = ServerException.class)
public RequestResult ServerException(ServerException e) {
return RequestResult.getFailedInstance(e.getCode(), e.getMessage() == null ? "系统异常" : e.getMessage());
} /**
* 处理自定义异常{@link ServerException},并返回错误信息
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public RequestResult ServerException() {
return RequestResult.getFailedInstance("系统异常");
} }
ServerException.class
/**
* @author TangZedong
* @apiNote 服务异常
* @since 2018/9/4 15:27
*/
public class ServerException extends RuntimeException {
// 错误代码
private short code = -1; public short getCode() {
return code;
} public ServerException(String message) {
super(message);
} public ServerException(short code, String message) {
super(message);
this.code = code;
} public ServerException(short code, String message, Throwable t) {
super(message, t);
this.code = code;
} }
RequestResult.class
/**
* @author TangZedong
* @apiNote 请求结果
* @since 2018/9/4 16:20
*/
public class RequestResult<T> {
private static final short SUCCESS_CODE = 0;
private static final short FAILED_CODE = -1; private static final boolean SUCCESS_STATUS = true;
private static final boolean FAILED_STATUS = false; private static final String NULL_MESSAGE = null;
private static final String NULL_DATA = null; private short code;
private String message;
private boolean success;
private T data; // 构造方法
public RequestResult(short code, String message, boolean success, T data) {
this.code = code;
this.message = message;
this.success = success;
this.data = data;
} // get方法
public short getCode() {
return code;
} public String getMessage() {
return message;
} public T getData() {
return data;
} public boolean isSuccess() {
return success;
} // 获取实例
public static <T> RequestResult getFailedInstance(short code, String message, T data) {
return new RequestResult(code, message, FAILED_STATUS, data);
} public static <T> RequestResult getFailedInstance(short code, String message) {
return new RequestResult(code, message, FAILED_STATUS, NULL_DATA);
} public static <T> RequestResult getFailedInstance(String message) {
return new RequestResult(FAILED_CODE, message, FAILED_STATUS, NULL_DATA);
} public static <T> RequestResult getSuccessInstance(short code, String message, T data) {
return new RequestResult(code, message, SUCCESS_STATUS, data);
} public static <T> RequestResult getSuccessInstance(short code, T data) {
return new RequestResult(code, NULL_MESSAGE, SUCCESS_STATUS, data);
} public static <T> RequestResult getSuccessInstance(T data) {
return new RequestResult(SUCCESS_CODE, NULL_MESSAGE, SUCCESS_STATUS, data);
} }
spring boot配置统一异常处理的更多相关文章
- 基于Spring Boot的统一异常处理设计
基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...
- spring boot 中统一异常处理
基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...
- 基于spring boot的统一异常处理
一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...
- Spring Boot实践——统一异常处理
注解说明 @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现: /** * Special ...
- Spring Boot学习——统一异常处理
本随笔记录使用Spring Boot统一处理异常. 本文实例是从数据库中根据ID查询学生信息,要求学生的年龄在14——20岁之间.小于14岁,提示“你可能在上初中”:大于20岁,提示“呢可能在上大学” ...
- 【Spring Boot】Spring Boot之统一异常处理
一.统一异常处理的作用 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的统一捕获或者二次处理(比如空指针异常或sql异常正常是不能外抛)是非常必要的,然后右统一异 ...
- spring boot 2 统一异常处理
spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestContr ...
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
- Spring Boot API 统一返回格式封装
今天给大家带来的是Spring Boot API 统一返回格式封装,我们在做项目的时候API 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...
随机推荐
- Android6.0源码下载编译刷入真机
编译环境是Ubuntu12.04.手机nexus 5,编译安卓6.0.1源码并烧录到真机. 源码用的是科大的镜像:http://mirrors.ustc.edu.cn/aosp-monthly/,下载 ...
- Django之验证
1. 滑动验证码补充说一下 极验科技:https://docs.geetest.com/install/deploy/server/python#下载SDK按照人家的实例操作即可 1.pip inst ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- MySQL修改编码为UTF-8无效果解决办法
本来这是一件很简单的事,有很多博客里都有教程,但却足足花了我半天的时间才解决问题. 可能是因为我的MySQL安装时没有选择默认路径的原因,按照网上的教程修改了下图中的my.ini配置文件后编码并没有发 ...
- 使用wxpy自动发送微信消息(加强版)
通过使用wxpy自动发送微信消息后,笔者又加强了发送消息,堪称消息爆炸式发送 目前设置的为10秒发送一次,发送9次,每次发送10条内容 import requests import wxpy from ...
- Windows服务器搭建Redis
1.下载安装Redis https://github.com/MicrosoftArchive/redis/releases 可以下载安装版(.msi)也可以下载解压版(.zip). 我直接下载的安装 ...
- Win10 + MASM32 + EditPlus 汇编语言编程环境设置
下载安装MASM32汇编环境 官方下载站:MASM32 环境变量配置 配置MasmHome变量,值为masm32的安装目录: 配置include和lib变量 include : %MasmHome%\ ...
- linux内网IP如果判断出网IP地址
[root@jumpserver ~]# curl https://ip.cn当前 IP: 162.14.210.16 来自: 河南省郑州市 xx网络
- 【ReactNative】Mac下分分钟打包 Android apk
时间:2016-11-20 09:17:07 地址:https://github.com/zhongxia245/blog/issues/52 Mac 下 ReactNative如何打包构建Andro ...
- node及socket.io实现简易websocket双向通信
技术栈: vue2.0 + node + websocket( socket.io ) 1. 安装依赖 初始化vue项目后输入下方指令安装依赖包 // 推荐cnpm安装 npm i vue-socke ...