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方法 ...
随机推荐
- 在nginx环境下,直接用域名访问(首页)
①: server { listen 80; server_name www.njm1.com; location = / { #=/规则可以直接访问域名.如:www.njm1.com.跳转到http ...
- Unity与服务区交互数据
Unity与服务区交互数据 Unity可能在用的时候使用到登陆等需要与服务器交互数据.今天尝试使用了WWW类和WWWForm类来实现Get请求与Post请求. 1.WWW Unity圣典解释: WWW ...
- Idea Live Templates
常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...
- [CH0304]IncDec Sequence
和NOIP2018DAY1T1类似的题目,但思维难度高多了. 这题既可以抬高路面,也可以降低路面,而且目标平面不确定,就难起来了. 但是两道题的基本思路几乎一样,同样我们将 2~n 的高度差分,1之所 ...
- Tensorflow中使用tfrecord方式读取数据-深度学习-周振洋
本博客默认读者对神经网络与Tensorflow有一定了解,对其中的一些术语不再做具体解释.并且本博客主要以图片数据为例进行介绍,如有错误,敬请斧正. 使用Tensorflow训练神经网络时,我们可以用 ...
- scatter注记词2
couch ranch bind ski extra bring note embrace tape they stick legend
- jdk1.8新特性-Lambda表达式使用要点
前言 在jdk1.8出来的时候看到过,没怎么了解.但是最近再看kafka和spark框架,框架示例中ava版的很多地方用到Lambda表达式,发现使用Lambda表达式代码确实简单了好多,有些例子大致 ...
- ZOJ 3689 Digging(DP)
Description When it comes to the Maya Civilization, we can quickly remind of a term called the end o ...
- 最全NB-IoT/eMTC物联网解决方案名录汇总
NB-IoT/eMTC等蜂窝物联网技术的成熟和商用,占据低功耗广域网络(LPWAN)的主流地位,推动全球物联网新一轮发展热潮,越来越多的行业开始采用物联网方案来解决解决实际问题.实现落地应用,越来越多 ...
- sql update limit1
更新限制:为了避免全表更新,错误更新影响太多,加上limit1 多了一层保障.