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方法 ...
随机推荐
- 关于自学C语言开始时应该注意的问题分享—未完待续......
---恢复内容开始--- 自学C语言编程总结 第1章C语言概述 1. 如果用户将主函数的返回值类型定义为了void,则不需要返回任何值: 2. C语言的基本结构包括主函数和程序体两部分 ...
- TPO-15 C1 The campus newspaper's reporter position
TPO-15 C1 The campus newspaper's reporter position 第 1 段 1.Listen to a conversation between a Studen ...
- Zookeeper与Eureka的区别
Zookeeper与Eureka的区别 想要了解Zk与eureka的区别首先要知道CAP定理 CAP定理 Mysql强一致性(数据唯一出处),设计数据库设计的三范式 (表必须有主键:表不能有重复的列: ...
- 数数字 (Digit Counting,ACM/ICPC Dannang 2007 ,UVa1225)
题目描述:算法竞赛入门经典习题3-3 #include <stdio.h> #include <string.h> int main(int argc, char *argv[ ...
- (转)GEM -次表面散射的实时近似
次表面散射(Subsurface Scattering),简称SSS,或3S,是光射入非金属材质后在内部发生散射, 最后射出物体并进入视野中产生的现象, 即光从表面进入物体经过内部散射,然后又通过物体 ...
- Redis+Keepalived高可用方案详细分析
背景 目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中.且目前官方开发中的Redis Cluster提供的功能尚不完善(可参考官方网站或http://www.redisdoc.com/ ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...
- ubuntu 安装 hustoj
https://github.com/zhblue/hustoj 准备工作: http://www.java123.net/v/961634.html 1.首先打开命令行,切换到root身份,获得最新 ...
- Java中的死锁问题
死锁问题: 例如有两个线程, 线程1与线程2. 线程1在执行的过程中, 要锁定对象1, 2才能完成整个操作, 首先锁定对象1, 再锁定对象2. 线程2在执行的过程中, 要锁定对象2, 1才能完成整个操 ...
- ubuntu中下载sublime相关问题
1.SublimeText3的安装 在网上搜索了一些ubuntu下关于sublime-text-3安装的方法,在这里针对自己尝试的情况进行反馈: 方法一(未成功): 在终端输入以下代码: sudo a ...