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 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...
随机推荐
- Kotlin入门(2)让App开发变得更容易
上一篇文章介绍了如何搭建Kotlin的开发环境,可是这个开发环境依然基于Android Studio,而在Android Studio上使用Java进行编码,本来就是理所应当的,何必还要专门弄个Kot ...
- springcloud 入门 8 (config配置中心)
Spring Cloud Config: 配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config. 在分布式系统中,由于服务数量巨多, ...
- 带你熟悉SQLServer2016中的System-Versioned Temporal Table 版本由系统控制的临时表
什么是 System-Versioned Temporal Table? System-Versioned Temporal Table,暂且容我管它叫版本由系统控制的临时表,它是 SQL Serve ...
- V4L2 driver -整体架构
我的uvc开源地址:gitee-uvc 字符设备驱动程序核心:V4L2本身就是一个字符设备,具有字符设备所有的特性,暴露接口给用户空间. V4L2 驱动核心:主要是构建一个内核中标准视频设备驱动的框架 ...
- U890采购入库单修改供应商
采购入库单表头 SELECT *FROM RdRecordWHERE (cCode = '0000051801') 采购入库单表体 SELECT *FROM RdRecordsWHERE (cPOID ...
- Spring boot 直接访问templates中html文件
application.properties 在浏览器中输入http://localhost:8080/index.html 会报一个 因为Spring boot 无法直接访问templates下的文 ...
- python随机生成6位数验证码
#随机生成6位数验证码 import randomcode = []for i in range(6): if i == str(random.randint(1,5)): cod ...
- laravel 使用构造器进行增删改查
使用原生语句进行增删改查 //$list = DB::select('select * from wt_category where id = :id', ['id' => 34]); //$i ...
- 如何在Windows平台下安装配置Memcached
Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一 ...
- <<linux device driver,third edition>> Chapter 4:Debugging Techniques
Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...