WebUtils封裝返回值

2021-08-11 01:59:45 字數 1245 閱讀 6305

在專案會有一些請求來規定返回值給前台,一般封裝個jsonobject物件即可,主要包括:code:code,msg:message,data:data三個字段

主要有成功和出錯的兩種情況,這裡我們可以封裝乙個responseenum列舉類專門用於錯誤**和錯誤資訊提示

如下類

public

enum responseenum

public string getcode()

public string getmessage()

}

然後寫乙個webutils類來封裝每種返回請求方式

public

class

webutils

//成功,有message

public

static jsonobject createsuccresult(object data)

//成功,引用某個responseenum類常量來實現

private

static jsonobject createresp(responseenum responseenum)

//失敗:沒有message

public

static jsonobject createerrorresult()

//失敗:引用responseenum類常量

public

static jsonobject createerrorresult(responseenum responseenum)

//失敗,引用某個responseenum類常量,和資料

public

static jsonobject createerrorresult(responseenum responseenum, object data)

//封裝異常資訊

public

static jsonobject createerrorresult(serviceexception e)

}

如何呼叫呢,以後寫介面就不用建立乙個jsonobject

函式返回值和封裝

需要用到函式內部的變數或者是函式的時候需要使用return 進行資料返回 函式返回值 return 可以將函式內部的變數或函式返回到函式外面去使用 語法 return 需要返回的內容 特點 return 一次只能返回乙個,寫多個最後只會返回最後乙個 函式中只要遇到return函式就結束了 retur...

snprintf返回值陷阱封裝

snprintf 函式用於將格式化的資料寫入字串,其原型為 int snprintf char str,int size,char format argument,返回值 對於snprintf有乙個不安全的地方就是它的返回值 當要寫入的argument的長度大於str的長度時,返回值不是str的長度...

vue 封裝通用方法,其他接受返回值返回

專案如果乙個方法多次呼叫,建議常勝剝離出來,寫乙個方法其他方法可以呼叫此方法,本文是親身測試經驗主要解決掛載乙個通用介面的方法,但是需要同步接收其返回值再往下進行。一 掛載公共方法 1 可以直接在main.js掛載全域性方法 vue.prototype.msgsuccess function msg...