基于@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配置统一异常处理的更多相关文章

  1. 基于Spring Boot的统一异常处理设计

    基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...

  2. spring boot 中统一异常处理

    基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...

  3. 基于spring boot的统一异常处理

    一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...

  4. Spring Boot实践——统一异常处理

    注解说明 @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现: /** * Special ...

  5. Spring Boot学习——统一异常处理

    本随笔记录使用Spring Boot统一处理异常. 本文实例是从数据库中根据ID查询学生信息,要求学生的年龄在14——20岁之间.小于14岁,提示“你可能在上初中”:大于20岁,提示“呢可能在上大学” ...

  6. 【Spring Boot】Spring Boot之统一异常处理

    一.统一异常处理的作用 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的统一捕获或者二次处理(比如空指针异常或sql异常正常是不能外抛)是非常必要的,然后右统一异 ...

  7. spring boot 2 统一异常处理

    spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestContr ...

  8. Spring Boot配置过滤器的两种方式

    过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...

  9. Spring Boot API 统一返回格式封装

    今天给大家带来的是Spring Boot API 统一返回格式封装,我们在做项目的时候API 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...

随机推荐

  1. Clumsy 利用无线网卡结合Clumsy软件模拟弱网络测试

    利用无线网卡结合Clumsy软件模拟弱网络测试 by:授客 QQ:1033553122 实践环境 Clumsy 0.2 下载地址:http://jagt.github.io/clumsy/downlo ...

  2. Linux 学习笔记之超详细基础linux命令 Part 5

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 4----------------- ...

  3. Jni OnLoad()和OnUnload()

    除了前面说的自定义JNI函数之外,JNI还提供了两个特殊函数,它们是JNI_OnLoad()和JNI_OnUnload(),分别在加载库和卸载库的时候调用. 1.JNI_OnLoad() Java调用 ...

  4. Flume Source 实例

    Flume Source 实例 Avro Source 监听avro端口,接收外部avro客户端数据流.跟前面的agent的Avro Sink可以组成多层拓扑结构. 1 2 3 4 5 6 7 8 9 ...

  5. 移动端上拉加载,下拉刷新效果Demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. oracle 数据库 导出与导入 expdb和impdb使用方法 (服务器本机)

    expdb 与exp 导出数据有区异,exp 无法导出空值表,用于客户端,expdb 只用于服务器端.备份出来的数据可再远程传输到另外一台linux 实现异地备份! 一  关于expdp和impdp ...

  7. January 15th, 2018 Week 03rd Monday

    We got things to do. Places to go. People to see. Futures to make. 我们有很多事情要做,有很多地方要去,有很多人要见,有很多美好的未来 ...

  8. web机试

    测试: <html><style> </style><title>Demo</title> <body><div > ...

  9. 如何取得select结果数据集的前10条记录。postgresql

    我用的是POSTGRESQL.select name from t_personal order by personal_id desc 我想取得上面结果数据的,前10条记录.SQL语句怎么改. 我记 ...

  10. (转)postgresql+postgis空间数据库使用总结

    转载地址:https://blog.csdn.net/qq_36588972/article/details/78902195 参考资料: pgrouting路径导航 https://www.cnbl ...