你会全局统一格式返回吗?

开发 前端
相信全局统一格式返回这个东西在每个项目中都非常重要的,前端需要一个统一的格式给前端,所以我们后端需要封装好结构给前端。

[[428153]]

本文转载自微信公众号「java后端指南」,作者KING鹏哥  。转载本文请联系java后端指南公众号。

相信全局统一格式返回这个东西在每个项目中都非常重要的,前端需要一个统一的格式给前端,所以我们后端需要封装好结构给前端。

1结构

我目前接触的结构基本上是这样的。


"code":"0"
"msg":"请求正常"
"data":{} 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

2实现

创建一个统一返回前端的实体类

public class R extends HashMap<String, Object> { 
    private static final long serialVersionUID = 1L; 
 
    /** 
     * 状态码 
     */ 
    public static final String CODE_TAG = "code"
 
    /** 
     * 返回内容 
     */ 
    public static final String MSG_TAG = "msg"
 
    /** 
     * 数据对象 
     */ 
    public static final String DATA_TAG = "data"
 
    /** 
     * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 
     */ 
    public R() { 
    } 
 
    /** 
     * 初始化一个新创建的 AjaxResult 对象 
     * 
     * @param code 状态码 
     * @param msg  返回内容 
     */ 
    public R(String code, String msg) { 
        super.put(CODE_TAG, code); 
        super.put(MSG_TAG, msg); 
    } 
 
    /** 
     * 初始化一个新创建的 AjaxResult 对象 
     * 
     * @param code 状态码 
     * @param msg  返回内容 
     * @param data 数据对象 
     */ 
    public R(String code, String msg, Object data) { 
        super.put(CODE_TAG, code); 
        super.put(MSG_TAG, msg); 
        if (null!=data) { 
            super.put(DATA_TAG, data); 
        } 
    } 
 
    /** 
     * 方便链式调用 
     * 
     * @param key 
     * @param value 
     * @return 
     */ 
    @Override 
    public R put(String key, Object value) { 
        super.put(key, value); 
        return this; 
    } 
 
    /** 
     * 返回成功消息 
     * 
     * @return 成功消息 
     */ 
    public static R success() { 
        return R.success("操作成功"); 
    } 
 
    /** 
     * 返回成功数据 
     * 
     * @return 成功消息 
     */ 
    public static R success(Object data) { 
        return R.success("操作成功", data); 
    } 
 
    /** 
     * 返回成功消息 
     * 
     * @param msg 返回内容 
     * @return 成功消息 
     */ 
    public static R success(String msg) { 
        return R.success(msg, null); 
    } 
 
    /** 
     * 返回成功消息 
     * 
     * @param msg  返回内容 
     * @param data 数据对象 
     * @return 成功消息 
     */ 
    public static R success(String msg, Object data) { 
        return new R("0", msg, data); 
    } 
 
    /** 
     * 返回错误消息 
     * 
     * @return 
     */ 
    public static R error() { 
        return R.error("操作失败"); 
    } 
 
    /** 
     * 返回错误消息 
     * 
     * @param msg 返回内容 
     * @return 警告消息 
     */ 
    public static R error(String msg) { 
        return R.error("-1", msg); 
    } 
 
    /** 
     * 返回错误消息 
     * 
     * @param msg  返回内容 
     * @param data 数据对象 
     * @return 警告消息 
     */ 
    public static R error(String msg, Object data) { 
        return new R("-1", msg, data); 
    } 
 
    /** 
     * 返回错误消息 
     * 
     * @param code 状态码 
     * @param msg  返回内容 
     * @return 警告消息 
     */ 
    public static R error(String code, String msg) { 
        return new R(code, msg, null); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.

3异常处理

正常情况下的返回结构已经弄好了,异常情况下我们也要做处理的。

首先新建几个异常类,根据实际需要创建。

/** 
 * 基本异常 
 */ 
@Getter 
public class BaseException extends RuntimeException { 
    private static final long serialVersionUID = 1L; 
 
    /** 
     * 所属模块 
     */ 
    private final String module; 
    /** 
     * 错误码 
     */ 
    private final String code; 
    /** 
     * 错误码对应的参数 
     */ 
    private final Object[] args; 
    /** 
     * 错误消息 
     */ 
    private final String message; 
 
    public BaseException(String module, String code, Object[] args, String message) { 
        this.module = module; 
        this.code = code; 
        this.args = args; 
        this.message = message; 
    } 
 
    public BaseException(String module, String code, Object[] args) { 
        this(module, code, args, null); 
    } 
 
    public BaseException(String module, String defaultMessage) { 
        this(module, nullnull, defaultMessage); 
    } 
 
    public BaseException(String code, Object[] args) { 
        this(null, code, args, null); 
    } 
    public BaseException(String module, String code, String message) { 
        this(null, code, null, message); 
    } 
    public BaseException(String message) { 
        this(nullnullnull, message); 
    } 
    public String getCode() { 
        return code; 
    } 
    public String getMsg() { return message; } 
    public Object[] getArgs() { 
        return args; 
    } 
    public String getDefaultMessage() { return getMessage(); } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
/** 
 * 自定义异常 
 */ 
public class CustomException extends RuntimeException { 
    private static final long serialVersionUID = 1L; 
 
    private String code; 
 
    private final String message; 
 
    public CustomException(String message) { 
        this.message = message; 
    } 
 
    public CustomException(String message, String code) { 
        this.message = message; 
        this.code = code; 
    } 
 
    public CustomException(String message, Throwable e) { 
        super(message, e); 
        this.message = message; 
    } 
 
    @Override 
    public String getMessage() { 
        return message; 
    } 
 
    public String getCode() { 
        return code; 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

我就创建了两个异常类。

4创建全局异常拦截器

@RestControllerAdvice 
@Slf4j 
public class GlobalExceptionHandler { 
    /** 
     * 基础异常 
     */ 
    @ExceptionHandler(BaseException.class) 
    public R baseException(BaseException e) { 
        return R.error(e.getDefaultMessage()); 
    } 
 
    /** 
     * 业务异常 
     */ 
    @ExceptionHandler(CustomException.class) 
    public R businessException(CustomException e) { 
        if (StringUtils.isNotBlank(e.getCode())) { 
            return R.error(e.getMessage()); 
        } 
        return R.error("-1", e.getMessage()); 
    } 
 
    @ExceptionHandler(Exception.class) 
    public R handleException(Exception e) { 
        log.error(e.getMessage(), e); 
        return R.error(String.format("未知错误%s",e.getMessage())); 
    } 
 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

其中状态码我们可以单独定义一个类,我为了方便就不写了。

5测试

@RestController 
public class TestAction { 
    @RequestMapping("/test"
    public R test(){ 
       return R.success("请求成功",null); 
    } 
    @RequestMapping("/test2"
    public R test2(){ 
        Student student=new Student(); 
        student.setName("king"); 
        student.setPassword("123456"); 
        return R.success(student); 
    } 
    @RequestMapping("/test3"
    public R test3(){ 
        return R.error("请求失败"); 
    } 
    @RequestMapping("/test4"
    public R test4(){ 
        throw new CustomException("失败了"); 
    } 
 
    @RequestMapping("/test5"
    public R test5() throws Exception { 
        throw new Exception("失败了"); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

大家可以自己去看看,如果有问题欢迎来骚扰我。

 

视频讲解:https://www.bilibili.com/video/BV1e341117av/

 

责任编辑:武晓燕 来源: java后端指南
相关推荐

2019-09-29 10:23:09

APIJava编程语言

2022-08-31 08:19:04

接口returnCode代码

2015-11-10 17:55:35

微软

2010-03-03 18:28:31

RSA 2010联想网御

2022-07-06 08:01:05

数据库分布式

2024-04-23 08:31:57

pythonfalse

2011-04-28 14:20:56

华硕上网本VX6

2021-08-19 15:36:09

数据备份存储备份策略

2023-11-28 14:32:04

2021-08-27 14:11:49

统一安全工作区桌面虚拟化应用安全

2021-04-14 06:53:52

C# 修饰符 Public

2021-04-16 15:02:11

CAP理论分布式

2019-08-22 14:02:00

Spring BootRestful APIJava

2021-10-11 10:25:33

排列nums数组

2014-04-22 10:50:31

统一通信UCBYOD

2021-07-16 08:58:35

SpringBoot

2021-09-09 08:58:32

Excel数据处理函数

2024-02-22 08:31:26

数据恢复工具MySQL回滚SQL

2022-10-28 21:11:52

m3u8网络

2016-04-29 17:38:31

华三/人才培养
点赞
收藏

51CTO技术栈公众号