spring全局异常处理 自定义返回数据结构
在写api接口中,正常返回和异常错误返回我们都希望很清楚的将这些信息清楚的返回给用户,出现异常情况下需要清楚的知道是参数异常还是未知异常,而不是返回一个不正确的数据结构。
所以此处只针对写api接口时异常处理:
1、先自定义一个我们需要的异常类,注意要继承RuntimeException类。
public class ApplicationException extends RuntimeException {
private Integer errorId;
public ApplicationException(String message) {
super(message);
this.errorId = BasicErrorCodeEnum.INTERNAL_SERVER_ERROR.getCode();
}
public ApplicationException(String message, Throwable cause) {
super(message, cause);
this.errorId = BasicErrorCodeEnum.INTERNAL_SERVER_ERROR.getCode();
LogFactory.getLog().error("### error ###:: " + message, cause);
}
public ApplicationException(ErrorCodeEnum errorCodeEnum) {
super(errorCodeEnum.getMsg());
this.errorId = errorCodeEnum.getCode();
}
public ApplicationException(ErrorCodeEnum enums, String message) {
super(message);
this.errorId = enums.getCode();
}
public ApplicationException(Integer errorId, String message, Throwable cause) {
super(message, cause);
this.errorId = errorId;
}
public ApplicationException(Integer errorId) {
this.errorId = errorId;
}
public Integer getErrorId() {
return this.errorId;
}
}
2、异常返回数据结构
public class ExceptionResponse {
private Integer code ;
private String msg = "";
public ExceptionResponse() {
}
public ExceptionResponse(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3、异常拦截拦截类即controller增强类,这里指定拦截异常,未知默认的异常等等。
@ControllerAdvice(annotations = RestController.class)
@ResponseBody
public class GlobalExceptionHandler {
private static final Log LOG = LogFactory.getLog(GlobalExceptionHandler.class); @ExceptionHandler(value = UserException.class)
public ExceptionResponse defaultErrorHandler(HttpServletRequest req, UserException e) {
return new ExceptionResponse(e.getErrorEnum().getCode(), e.getErrorEnum().getMsg());
} @ExceptionHandler(value = HttpMessageNotReadableException.class)
public ExceptionResponse defaultErrorHandler(HttpServletRequest req, HttpMessageNotReadableException e) {
return new ExceptionResponse(CommonCodeEnum.BAD_REQUES.getCode(), CommonCodeEnum.BAD_REQUES.getMsg());
} @ExceptionHandler(value = Exception.class)
public ExceptionResponse defaultErrorHandler(HttpServletRequest req, Exception e) {
return new ExceptionResponse(CommonCodeEnum.INTERNAL_SERVER_ERROR.getCode(), CommonCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
} @ExceptionHandler(value = ApplicationException.class)
public ExceptionResponse defaultErrorHandler(HttpServletRequest req, ApplicationException e) {
return new ExceptionResponse(e.getErrorId(), e.getMessage());
} }
spring全局异常处理 自定义返回数据结构的更多相关文章
- Spring 全局异常处理
[参考文章]:Spring全局异常处理的三种方式 [参考文章]:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 [参考文章]:@ControllerAdvic ...
- 《Spring全局异常处理》从零掌握@ControllerAdvice注解
一.开门见山 在前后端分离框架的大趋势下,前后端基本的职责已经确定. 前端主要负责界面的处理以及基本的判空检验.数据来源则通过vue调用后端发布的接口. 后端的原型还是mvc的模式: controll ...
- Spring全局异常处理的三种方式
在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度高,工作 ...
- Spring全局异常处理
最近学习Spring时,认识到Spring异常处理的强大.之前处理工程异常,代码中最常见的就是try-catch-finally,有时一个try,多个catch,覆盖了核心业务逻辑: 1 try{ 2 ...
- Spring Boot 如何自定义返回错误码错误信息
说明 在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端 INTERNAL_SERVER_ERROR(500, "Intern ...
- Spring Boot Web 自定义返回值(通用)
在项目下新建common.entity包,包中包含两个文件Result数据类,ResultCode接口文件 Result.class @Data @NoArgsConstructor public c ...
- Spring MVC全局异常后返回JSON异常数据
问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...
- Spring Cloud Gateway的全局异常处理
Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求. 网关都是给接口 ...
- spring boot 全局异常处理及自定义异常类
全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...
随机推荐
- 初学Direct X(9) ——文字的显示
初学Direct X(9) --文字的显示 本次学习如何使用ID3DXFont创建字体,使得我们可以在任何安装了Windows系统中TrueType字体来打印文字,不过最好使用标准字体,这样文字在每一 ...
- 6.azkban的监控
azkaban自带的监控flow自带的邮件功能SLA总结写程序监控job情况监控azkaban的元数据库使用azkaban API监控总结 azkaban自带的监控 azkban目前仅仅支持邮件监控, ...
- ACM 第十一天
多校7题目 GuGuFishtion Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 理解windows模型
同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实绝大多数函数都是同步调用(例如sin, isdigit等).但是一般而言,我们在说同步.异步的时候,特指 ...
- 判断两个字符串是否相等【JAVA】
if(A.equals(B)){ } 之前总是用"=="来判断,但是在JAVA里面好像不行.所以,用equals(). 查了下资料. 原因:equal()比较的是对象的内容,&qu ...
- Thinkphp5获取数据库数据到视图
这是学习thinkhp5的基础篇笔记. 本文主要讲怎么配置数据库链接,以及查询数据库数据,并且最后将数据赋给视图. 数据库配置: thinkphp5的数据库配置默认在conf下的database.ph ...
- 浅述Try {} Catch{} 作用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...
- java 基础 --多态--009
1, 多态:同一个对象(事物),在不同时刻体现出来的不同状态 2, 多态的前提: A: 要有继承关系 B: 要有方法的重写 C: 要有父类引用指向子类对象 父 f = new 子(); 3, 多态访问 ...
- dwarf是如何处理栈帧的?
dwarf是如何处理栈帧的? DW_AT_frame_base 表明函数栈帧的起始点 95 < 1><0x000000ca> DW_TAG_subprogram 96 ...
- Redis 学习之集群
该文使用centos6.5 64位 redis3.2.8 一. redis-cluster架构图 集群通信:所有redis节点之间通过PING-PONG机制彼此互联,内部使用二进制鞋子优化传输速度 ...