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方法 ...
随机推荐
- Selenium 入门到精通系列:六
Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...
- ortp打印日志
//向字符串中打印数据 static char* ms_strdup_vprintf(const char *fmt, va_list ap) { ; char *p,*np; #ifndef WIN ...
- HTML+JS = 网站注册界面源代码
本注册页面未设置编码方式和兼容性,已测试,在Chrome浏览器显示正常 <!DOCTYPE html> <html> <head> <title>注册页 ...
- Github协作图想
首先 git pull 从远程拉下代码,并在本地与本地代码自动合并 在本地解决冲突后,可将本地代码进行远程推送 版本库的Repository中存储的是版本树状链,每一根链接线代表每一次的修改,每一个节 ...
- Mount qcow2 image
1.Mount a qcow2 image qemu-nbd - QEMU Disk Network Block Device Server: Export QEMU disk image using ...
- 【转】Backbone.js学习笔记(二)细说MVC
文章转自: http://segmentfault.com/a/1190000002666658 对于初学backbone.js的同学可以先参考我这篇文章:Backbone.js学习笔记(一) Bac ...
- 将HTML页面页脚固定在页面底部(多种方法实现)
当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...
- 如何遍历一个文件夹(C语言实现)
#include<io.h> #include<stdio.h> int main() { long Handle; struct _finddata_t FileInfo; ...
- Linux上安装MySQL - 12条命令搞定MySql
从零开始安装mysql数据库 : 按照该顺序执行 : a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...
- asp.net 后台注册(调用)JS
1.使用Page.ClientScript.RegisterClientScriptBlock 使用 Page.ClientScript.RegisterClientScriptBlock可以防止ja ...