你會全局統(tǒng)一格式返回嗎?
本文轉(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)基本上是這樣的。
- {
- "code":"0",
- "msg":"請求正常",
- "data":{}
- }
2實(shí)現(xiàn)
創(chuàng)建一個統(tǒng)一返回前端的實(shí)體類
- public class R extends HashMap<String, Object> {
- private static final long serialVersionUID = 1L;
- /**
- * 狀態(tài)碼
- */
- public static final String CODE_TAG = "code";
- /**
- * 返回內(nèi)容
- */
- public static final String MSG_TAG = "msg";
- /**
- * 數(shù)據(jù)對象
- */
- public static final String DATA_TAG = "data";
- /**
- * 初始化一個新創(chuàng)建的 AjaxResult 對象,使其表示一個空消息。
- */
- public R() {
- }
- /**
- * 初始化一個新創(chuàng)建的 AjaxResult 對象
- *
- * @param code 狀態(tài)碼
- * @param msg 返回內(nèi)容
- */
- public R(String code, String msg) {
- super.put(CODE_TAG, code);
- super.put(MSG_TAG, msg);
- }
- /**
- * 初始化一個新創(chuàng)建的 AjaxResult 對象
- *
- * @param code 狀態(tài)碼
- * @param msg 返回內(nèi)容
- * @param data 數(shù)據(jù)對象
- */
- 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);
- }
- }
- /**
- * 方便鏈?zhǔn)秸{(diào)用
- *
- * @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("操作成功");
- }
- /**
- * 返回成功數(shù)據(jù)
- *
- * @return 成功消息
- */
- public static R success(Object data) {
- return R.success("操作成功", data);
- }
- /**
- * 返回成功消息
- *
- * @param msg 返回內(nèi)容
- * @return 成功消息
- */
- public static R success(String msg) {
- return R.success(msg, null);
- }
- /**
- * 返回成功消息
- *
- * @param msg 返回內(nèi)容
- * @param data 數(shù)據(jù)對象
- * @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 返回內(nèi)容
- * @return 警告消息
- */
- public static R error(String msg) {
- return R.error("-1", msg);
- }
- /**
- * 返回錯誤消息
- *
- * @param msg 返回內(nèi)容
- * @param data 數(shù)據(jù)對象
- * @return 警告消息
- */
- public static R error(String msg, Object data) {
- return new R("-1", msg, data);
- }
- /**
- * 返回錯誤消息
- *
- * @param code 狀態(tài)碼
- * @param msg 返回內(nèi)容
- * @return 警告消息
- */
- public static R error(String code, String msg) {
- return new R(code, msg, null);
- }
- }
3異常處理
正常情況下的返回結(jié)構(gòu)已經(jīng)弄好了,異常情況下我們也要做處理的。
首先新建幾個異常類,根據(jù)實(shí)際需要創(chuàng)建。
- /**
- * 基本異常
- */
- @Getter
- public class BaseException extends RuntimeException {
- private static final long serialVersionUID = 1L;
- /**
- * 所屬模塊
- */
- private final String module;
- /**
- * 錯誤碼
- */
- private final String code;
- /**
- * 錯誤碼對應(yīng)的參數(shù)
- */
- 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, null, null, 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(null, null, null, message);
- }
- public String getCode() {
- return code;
- }
- public String getMsg() { return message; }
- public Object[] getArgs() {
- return args;
- }
- public String getDefaultMessage() { return getMessage(); }
- }
- /**
- * 自定義異常
- */
- 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;
- }
- }
我就創(chuàng)建了兩個異常類。
4創(chuàng)建全局異常攔截器
- @RestControllerAdvice
- @Slf4j
- public class GlobalExceptionHandler {
- /**
- * 基礎(chǔ)異常
- */
- @ExceptionHandler(BaseException.class)
- public R baseException(BaseException e) {
- return R.error(e.getDefaultMessage());
- }
- /**
- * 業(yè)務(wù)異常
- */
- @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()));
- }
- }
其中狀態(tài)碼我們可以單獨(dú)定義一個類,我為了方便就不寫了。
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("失敗了");
- }
- }
大家可以自己去看看,如果有問題歡迎來騷擾我。
視頻講解:https://www.bilibili.com/video/BV1e341117av/