在写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全局异常处理 自定义返回数据结构的更多相关文章

  1. Spring 全局异常处理

    [参考文章]:Spring全局异常处理的三种方式 [参考文章]:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 [参考文章]:@ControllerAdvic ...

  2. 《Spring全局异常处理》从零掌握@ControllerAdvice注解

    一.开门见山 在前后端分离框架的大趋势下,前后端基本的职责已经确定. 前端主要负责界面的处理以及基本的判空检验.数据来源则通过vue调用后端发布的接口. 后端的原型还是mvc的模式: controll ...

  3. Spring全局异常处理的三种方式

    在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度高,工作 ...

  4. Spring全局异常处理

    最近学习Spring时,认识到Spring异常处理的强大.之前处理工程异常,代码中最常见的就是try-catch-finally,有时一个try,多个catch,覆盖了核心业务逻辑: 1 try{ 2 ...

  5. Spring Boot 如何自定义返回错误码错误信息

    说明 在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端 INTERNAL_SERVER_ERROR(500, "Intern ...

  6. Spring Boot Web 自定义返回值(通用)

    在项目下新建common.entity包,包中包含两个文件Result数据类,ResultCode接口文件 Result.class @Data @NoArgsConstructor public c ...

  7. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  8. Spring Cloud Gateway的全局异常处理

    Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求. 网关都是给接口 ...

  9. spring boot 全局异常处理及自定义异常类

    全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...

随机推荐

  1. TPO-14 C1 Locate a political book

    TPO-14 C1 Locate a political book 第 1 段 1.Listen to a conversation between the student and librarian ...

  2. python函数参数默认值及重要警告

    最有用的形式是对一个或多个参数指定一个默认值.这样创建的函数,可以用比定义时允许的更少的参数调用,比如: def ask_ok(prompt, retries=4, reminder='Please ...

  3. 学霸系统UI项目功能说明书 v1.0版本

    发布人员:软件工程实践小队. 发布内容:学霸系统UI项目说明书. 版本:学霸v1.0版本. 学霸系统UI项目说明书 v1.0版本分为以下部分: Part 1:用户须知: Part 2:功能实现: Pa ...

  4. 第三次寒假作业 sketch 了解

    什么是sketch? sketch 是一种基于散列的数据结构,可以在高速网络环境中,实时地存储流量特征信息,只占用较小的空间资源,并且具备在理论上可证明的估计精度与内存的平衡特性. 通过设置散列函数, ...

  5. iOS开发跳转指定页面

    for (UIViewController *VC in self.navigationController.viewControllers) { if ([VC isKindOfClass:[Car ...

  6. vuex管理页面标题

    1.在store -> mutation-types.js文件新增常量 export const UPDATE_TITLE = 'UPDATE_TITLE' 2.新增文件title.js目录结构 ...

  7. 结对作业二——WordCount进阶版

    软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...

  8. Java多线程同步机制之同步块(方法)——synchronized

    在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法或者代码块,解决了资源共享.下面代码示意三个窗口购5张火车票: package com.jikexueyuan.t ...

  9. 自学网络 arp_ignore/arp_announce

    1)机器上有好几个IP地址,如何让网卡只接收自己IP地址的数据包: 如何只接收自己网卡的数据包 http://www.cnblogs.com/honpey/p/8447819.html 相关的配置ar ...

  10. NCAIOC

    NCAIOC Npm Cli All In One Client https://github.com/xgqfrms/NCAIOC https://cdn.xgqfrms.xyz/web-ide/i ...