自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你會全局統(tǒng)一格式返回嗎?

開發(fā) 前端
相信全局統(tǒng)一格式返回這個東西在每個項(xiàng)目中都非常重要的,前端需要一個統(tǒng)一的格式給前端,所以我們后端需要封裝好結(jié)構(gòu)給前端。

[[428153]]

本文轉(zhuǎn)載自微信公眾號「java后端指南」,作者KING鵬哥  。轉(zhuǎn)載本文請聯(lián)系java后端指南公眾號。

相信全局統(tǒng)一格式返回這個東西在每個項(xiàng)目中都非常重要的,前端需要一個統(tǒng)一的格式給前端,所以我們后端需要封裝好結(jié)構(gòu)給前端。

1結(jié)構(gòu)

我目前接觸的結(jié)構(gòu)基本上是這樣的。

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

2實(shí)現(xiàn)

創(chuàng)建一個統(tǒng)一返回前端的實(shí)體類

  1. public class R extends HashMap<String, Object> { 
  2.     private static final long serialVersionUID = 1L; 
  3.  
  4.     /** 
  5.      * 狀態(tài)碼 
  6.      */ 
  7.     public static final String CODE_TAG = "code"
  8.  
  9.     /** 
  10.      * 返回內(nèi)容 
  11.      */ 
  12.     public static final String MSG_TAG = "msg"
  13.  
  14.     /** 
  15.      * 數(shù)據(jù)對象 
  16.      */ 
  17.     public static final String DATA_TAG = "data"
  18.  
  19.     /** 
  20.      * 初始化一個新創(chuàng)建的 AjaxResult 對象,使其表示一個空消息。 
  21.      */ 
  22.     public R() { 
  23.     } 
  24.  
  25.     /** 
  26.      * 初始化一個新創(chuàng)建的 AjaxResult 對象 
  27.      * 
  28.      * @param code 狀態(tài)碼 
  29.      * @param msg  返回內(nèi)容 
  30.      */ 
  31.     public R(String code, String msg) { 
  32.         super.put(CODE_TAG, code); 
  33.         super.put(MSG_TAG, msg); 
  34.     } 
  35.  
  36.     /** 
  37.      * 初始化一個新創(chuàng)建的 AjaxResult 對象 
  38.      * 
  39.      * @param code 狀態(tài)碼 
  40.      * @param msg  返回內(nèi)容 
  41.      * @param data 數(shù)據(jù)對象 
  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.      * 方便鏈?zhǔn)秸{(diào)用 
  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.      * 返回成功數(shù)據(jù) 
  75.      * 
  76.      * @return 成功消息 
  77.      */ 
  78.     public static R success(Object data) { 
  79.         return R.success("操作成功", data); 
  80.     } 
  81.  
  82.     /** 
  83.      * 返回成功消息 
  84.      * 
  85.      * @param msg 返回內(nèi)容 
  86.      * @return 成功消息 
  87.      */ 
  88.     public static R success(String msg) { 
  89.         return R.success(msg, null); 
  90.     } 
  91.  
  92.     /** 
  93.      * 返回成功消息 
  94.      * 
  95.      * @param msg  返回內(nèi)容 
  96.      * @param data 數(shù)據(jù)對象 
  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 返回內(nèi)容 
  116.      * @return 警告消息 
  117.      */ 
  118.     public static R error(String msg) { 
  119.         return R.error("-1", msg); 
  120.     } 
  121.  
  122.     /** 
  123.      * 返回錯誤消息 
  124.      * 
  125.      * @param msg  返回內(nèi)容 
  126.      * @param data 數(shù)據(jù)對象 
  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 狀態(tài)碼 
  137.      * @param msg  返回內(nèi)容 
  138.      * @return 警告消息 
  139.      */ 
  140.     public static R error(String code, String msg) { 
  141.         return new R(code, msg, null); 
  142.     } 

3異常處理

正常情況下的返回結(jié)構(gòu)已經(jīng)弄好了,異常情況下我們也要做處理的。

首先新建幾個異常類,根據(jù)實(shí)際需要創(chuàng)建。

  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.      * 錯誤碼對應(yīng)的參數(shù) 
  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.     } 

我就創(chuàng)建了兩個異常類。

4創(chuàng)建全局異常攔截器

  1. @RestControllerAdvice 
  2. @Slf4j 
  3. public class GlobalExceptionHandler { 
  4.     /** 
  5.      * 基礎(chǔ)異常 
  6.      */ 
  7.     @ExceptionHandler(BaseException.class) 
  8.     public R baseException(BaseException e) { 
  9.         return R.error(e.getDefaultMessage()); 
  10.     } 
  11.  
  12.     /** 
  13.      * 業(yè)務(wù)異常 
  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.  

其中狀態(tài)碼我們可以單獨(dú)定義一個類,我為了方便就不寫了。

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/

 

責(zé)任編輯:武曉燕 來源: java后端指南
相關(guān)推薦

2019-09-29 10:23:09

APIJava編程語言

2022-08-31 08:19:04

接口returnCode代碼

2010-03-03 18:28:31

RSA 2010聯(lián)想網(wǎng)御

2015-11-10 17:55:35

微軟

2025-04-09 08:00:00

FastAPI統(tǒng)一響應(yīng)全局異常處理

2022-07-06 08:01:05

數(shù)據(jù)庫分布式

2024-04-23 08:31:57

pythonfalse

2021-08-19 15:36:09

數(shù)據(jù)備份存儲備份策略

2023-11-28 14:32:04

2021-08-27 14:11:49

統(tǒng)一安全工作區(qū)桌面虛擬化應(yīng)用安全

2011-04-28 14:20:56

華碩上網(wǎng)本VX6

2021-04-14 06:53:52

C# 修飾符 Public

2021-04-16 15:02:11

CAP理論分布式

2024-02-22 08:31:26

數(shù)據(jù)恢復(fù)工具MySQL回滾SQL

2019-08-22 14:02:00

Spring BootRestful APIJava

2021-07-16 08:58:35

SpringBoot

2021-10-11 10:25:33

排列nums數(shù)組

2014-04-22 10:50:31

統(tǒng)一通信UCBYOD

2012-06-20 10:47:25

Team Leader

2019-05-07 15:49:27

AI人工智能藝術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號