成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

你會全局統一格式返回嗎?

開發 前端
相信全局統一格式返回這個東西在每個項目中都非常重要的,前端需要一個統一的格式給前端,所以我們后端需要封裝好結構給前端。

[[428153]]

本文轉載自微信公眾號「java后端指南」,作者KING鵬哥  。轉載本文請聯系java后端指南公眾號。

相信全局統一格式返回這個東西在每個項目中都非常重要的,前端需要一個統一的格式給前端,所以我們后端需要封裝好結構給前端。

1結構

我目前接觸的結構基本上是這樣的。

  1. "code":"0"
  2. "msg":"請求正常"
  3. "data":{} 

2實現

創建一個統一返回前端的實體類

  1. public class R extends HashMap<String, Object> { 
  2.     private static final long serialVersionUID = 1L; 
  3.  
  4.     /** 
  5.      * 狀態碼 
  6.      */ 
  7.     public static final String CODE_TAG = "code"
  8.  
  9.     /** 
  10.      * 返回內容 
  11.      */ 
  12.     public static final String MSG_TAG = "msg"
  13.  
  14.     /** 
  15.      * 數據對象 
  16.      */ 
  17.     public static final String DATA_TAG = "data"
  18.  
  19.     /** 
  20.      * 初始化一個新創建的 AjaxResult 對象,使其表示一個空消息。 
  21.      */ 
  22.     public R() { 
  23.     } 
  24.  
  25.     /** 
  26.      * 初始化一個新創建的 AjaxResult 對象 
  27.      * 
  28.      * @param code 狀態碼 
  29.      * @param msg  返回內容 
  30.      */ 
  31.     public R(String code, String msg) { 
  32.         super.put(CODE_TAG, code); 
  33.         super.put(MSG_TAG, msg); 
  34.     } 
  35.  
  36.     /** 
  37.      * 初始化一個新創建的 AjaxResult 對象 
  38.      * 
  39.      * @param code 狀態碼 
  40.      * @param msg  返回內容 
  41.      * @param data 數據對象 
  42.      */ 
  43.     public R(String code, String msg, Object data) { 
  44.         super.put(CODE_TAG, code); 
  45.         super.put(MSG_TAG, msg); 
  46.         if (null!=data) { 
  47.             super.put(DATA_TAG, data); 
  48.         } 
  49.     } 
  50.  
  51.     /** 
  52.      * 方便鏈式調用 
  53.      * 
  54.      * @param key 
  55.      * @param value 
  56.      * @return 
  57.      */ 
  58.     @Override 
  59.     public R put(String key, Object value) { 
  60.         super.put(key, value); 
  61.         return this; 
  62.     } 
  63.  
  64.     /** 
  65.      * 返回成功消息 
  66.      * 
  67.      * @return 成功消息 
  68.      */ 
  69.     public static R success() { 
  70.         return R.success("操作成功"); 
  71.     } 
  72.  
  73.     /** 
  74.      * 返回成功數據 
  75.      * 
  76.      * @return 成功消息 
  77.      */ 
  78.     public static R success(Object data) { 
  79.         return R.success("操作成功", data); 
  80.     } 
  81.  
  82.     /** 
  83.      * 返回成功消息 
  84.      * 
  85.      * @param msg 返回內容 
  86.      * @return 成功消息 
  87.      */ 
  88.     public static R success(String msg) { 
  89.         return R.success(msg, null); 
  90.     } 
  91.  
  92.     /** 
  93.      * 返回成功消息 
  94.      * 
  95.      * @param msg  返回內容 
  96.      * @param data 數據對象 
  97.      * @return 成功消息 
  98.      */ 
  99.     public static R success(String msg, Object data) { 
  100.         return new R("0", msg, data); 
  101.     } 
  102.  
  103.     /** 
  104.      * 返回錯誤消息 
  105.      * 
  106.      * @return 
  107.      */ 
  108.     public static R error() { 
  109.         return R.error("操作失敗"); 
  110.     } 
  111.  
  112.     /** 
  113.      * 返回錯誤消息 
  114.      * 
  115.      * @param msg 返回內容 
  116.      * @return 警告消息 
  117.      */ 
  118.     public static R error(String msg) { 
  119.         return R.error("-1", msg); 
  120.     } 
  121.  
  122.     /** 
  123.      * 返回錯誤消息 
  124.      * 
  125.      * @param msg  返回內容 
  126.      * @param data 數據對象 
  127.      * @return 警告消息 
  128.      */ 
  129.     public static R error(String msg, Object data) { 
  130.         return new R("-1", msg, data); 
  131.     } 
  132.  
  133.     /** 
  134.      * 返回錯誤消息 
  135.      * 
  136.      * @param code 狀態碼 
  137.      * @param msg  返回內容 
  138.      * @return 警告消息 
  139.      */ 
  140.     public static R error(String code, String msg) { 
  141.         return new R(code, msg, null); 
  142.     } 

3異常處理

正常情況下的返回結構已經弄好了,異常情況下我們也要做處理的。

首先新建幾個異常類,根據實際需要創建。

  1. /** 
  2.  * 基本異常 
  3.  */ 
  4. @Getter 
  5. public class BaseException extends RuntimeException { 
  6.     private static final long serialVersionUID = 1L; 
  7.  
  8.     /** 
  9.      * 所屬模塊 
  10.      */ 
  11.     private final String module; 
  12.     /** 
  13.      * 錯誤碼 
  14.      */ 
  15.     private final String code; 
  16.     /** 
  17.      * 錯誤碼對應的參數 
  18.      */ 
  19.     private final Object[] args; 
  20.     /** 
  21.      * 錯誤消息 
  22.      */ 
  23.     private final String message; 
  24.  
  25.     public BaseException(String module, String code, Object[] args, String message) { 
  26.         this.module = module; 
  27.         this.code = code; 
  28.         this.args = args; 
  29.         this.message = message; 
  30.     } 
  31.  
  32.     public BaseException(String module, String code, Object[] args) { 
  33.         this(module, code, args, null); 
  34.     } 
  35.  
  36.     public BaseException(String module, String defaultMessage) { 
  37.         this(module, nullnull, defaultMessage); 
  38.     } 
  39.  
  40.     public BaseException(String code, Object[] args) { 
  41.         this(null, code, args, null); 
  42.     } 
  43.     public BaseException(String module, String code, String message) { 
  44.         this(null, code, null, message); 
  45.     } 
  46.     public BaseException(String message) { 
  47.         this(nullnullnull, message); 
  48.     } 
  49.     public String getCode() { 
  50.         return code; 
  51.     } 
  52.     public String getMsg() { return message; } 
  53.     public Object[] getArgs() { 
  54.         return args; 
  55.     } 
  56.     public String getDefaultMessage() { return getMessage(); } 
  1. /** 
  2.  * 自定義異常 
  3.  */ 
  4. public class CustomException extends RuntimeException { 
  5.     private static final long serialVersionUID = 1L; 
  6.  
  7.     private String code; 
  8.  
  9.     private final String message; 
  10.  
  11.     public CustomException(String message) { 
  12.         this.message = message; 
  13.     } 
  14.  
  15.     public CustomException(String message, String code) { 
  16.         this.message = message; 
  17.         this.code = code; 
  18.     } 
  19.  
  20.     public CustomException(String message, Throwable e) { 
  21.         super(message, e); 
  22.         this.message = message; 
  23.     } 
  24.  
  25.     @Override 
  26.     public String getMessage() { 
  27.         return message; 
  28.     } 
  29.  
  30.     public String getCode() { 
  31.         return code; 
  32.     } 

我就創建了兩個異常類。

4創建全局異常攔截器

  1. @RestControllerAdvice 
  2. @Slf4j 
  3. public class GlobalExceptionHandler { 
  4.     /** 
  5.      * 基礎異常 
  6.      */ 
  7.     @ExceptionHandler(BaseException.class) 
  8.     public R baseException(BaseException e) { 
  9.         return R.error(e.getDefaultMessage()); 
  10.     } 
  11.  
  12.     /** 
  13.      * 業務異常 
  14.      */ 
  15.     @ExceptionHandler(CustomException.class) 
  16.     public R businessException(CustomException e) { 
  17.         if (StringUtils.isNotBlank(e.getCode())) { 
  18.             return R.error(e.getMessage()); 
  19.         } 
  20.         return R.error("-1", e.getMessage()); 
  21.     } 
  22.  
  23.     @ExceptionHandler(Exception.class) 
  24.     public R handleException(Exception e) { 
  25.         log.error(e.getMessage(), e); 
  26.         return R.error(String.format("未知錯誤%s",e.getMessage())); 
  27.     } 
  28.  
  29.  

其中狀態碼我們可以單獨定義一個類,我為了方便就不寫了。

5測試

  1. @RestController 
  2. public class TestAction { 
  3.     @RequestMapping("/test"
  4.     public R test(){ 
  5.        return R.success("請求成功",null); 
  6.     } 
  7.     @RequestMapping("/test2"
  8.     public R test2(){ 
  9.         Student student=new Student(); 
  10.         student.setName("king"); 
  11.         student.setPassword("123456"); 
  12.         return R.success(student); 
  13.     } 
  14.     @RequestMapping("/test3"
  15.     public R test3(){ 
  16.         return R.error("請求失敗"); 
  17.     } 
  18.     @RequestMapping("/test4"
  19.     public R test4(){ 
  20.         throw new CustomException("失敗了"); 
  21.     } 
  22.  
  23.     @RequestMapping("/test5"
  24.     public R test5() throws Exception { 
  25.         throw new Exception("失敗了"); 
  26.     } 

大家可以自己去看看,如果有問題歡迎來騷擾我。

 

視頻講解:https://www.bilibili.com/video/BV1e341117av/

 

責任編輯:武曉燕 來源: java后端指南
相關推薦

2019-09-29 10:23:09

APIJava編程語言

2022-08-31 08:19:04

接口returnCode代碼

2010-03-03 18:28:31

RSA 2010聯想網御

2015-11-10 17:55:35

微軟

2025-04-09 08:00:00

FastAPI統一響應全局異常處理

2022-07-06 08:01:05

數據庫分布式

2021-08-19 15:36:09

數據備份存儲備份策略

2024-04-23 08:31:57

pythonfalse

2011-04-28 14:20:56

華碩上網本VX6

2021-08-27 14:11:49

統一安全工作區桌面虛擬化應用安全

2023-11-28 14:32:04

2021-04-14 06:53:52

C# 修飾符 Public

2021-04-16 15:02:11

CAP理論分布式

2024-02-22 08:31:26

數據恢復工具MySQL回滾SQL

2021-10-11 10:25:33

排列nums數組

2014-04-22 10:50:31

統一通信UCBYOD

2012-06-20 10:47:25

Team Leader

2019-05-07 15:49:27

AI人工智能藝術

2019-08-22 14:02:00

Spring BootRestful APIJava

2021-09-09 08:58:32

Excel數據處理函數
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品欧美久久久久久久 | 国产探花在线精品一区二区 | 国产精品久久久久久久久久 | 久久逼逼 | 欧美精品中文字幕久久二区 | 三级高清| 在线播放国产一区二区三区 | 狠狠入ady亚洲精品经典电影 | 亚洲久视频 | 男人天堂免费在线 | 国产一区视频在线 | 亚洲精品在线免费观看视频 | 国产黄色小视频在线观看 | 自拍偷拍欧美 | 97国产精品| a级性视频 | 99re热精品视频 | 日韩和的一区二在线 | 中文字幕丁香5月 | 国产精品久久久久久久久久免费看 | 国产特级毛片 | 亚洲协和影视 | 九九久久精品 | 国产精品久久久久久久久久久久 | 污污的网站在线观看 | 在线观看第一页 | 国产精品久久久久久久久免费丝袜 | 欧美色图另类 | 国产日韩欧美 | 欧美精品网站 | 欧美一级欧美三级在线观看 | 国产精品视频偷伦精品视频 | 黄色日批视频 | www.亚洲成人网 | 久久人体视频 | 在线免费观看毛片 | 国产精品久久国产精品久久 | 国产成人99久久亚洲综合精品 | 日日干夜夜操天天操 | 91精品国产91久久综合桃花 | 亚洲 欧美 另类 日韩 |