日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CommonResult响应工具类封装

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CommonResult响应工具类封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

        • CommonResult
        • IErrorCode
        • ResultCode

CommonResult

package com.app.tools;/*** 通用返回對象* @param <T>*/ public class CommonResult <T>{/*** 狀態碼*/private long code;/*** 提示信息*/private String message;/*** 數據封裝*/private T data;protected CommonResult() {}protected CommonResult(long code, String message, T data) {this.code = code;this.message = message;this.data = data;}/*** 成功返回結果** @param data 獲取的數據*/public static <T> CommonResult<T> success(T data) {return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);}/*** 成功返回結果** @param data 獲取的數據* @param message 提示信息*/public static <T> CommonResult<T> success(T data, String message) {return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);}/*** 失敗返回結果* @param errorCode 錯誤碼*/public static <T> CommonResult<T> failed(IErrorCode errorCode) {return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);}/*** 失敗返回結果* @param errorCode 錯誤碼* @param message 錯誤信息*/public static <T> CommonResult<T> failed(IErrorCode errorCode,String message) {return new CommonResult<T>(errorCode.getCode(), message, null);}/*** 失敗返回結果* @param message 提示信息*/public static <T> CommonResult<T> failed(String message) {return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);}/*** 失敗返回結果*/public static <T> CommonResult<T> failed() {return failed(ResultCode.FAILED);}/*** 參數驗證失敗返回結果*/public static <T> CommonResult<T> validateFailed() {return failed(ResultCode.VALIDATE_FAILED);}/*** 參數驗證失敗返回結果* @param message 提示信息*/public static <T> CommonResult<T> validateFailed(String message) {return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);}/*** 未登錄返回結果*/public static <T> CommonResult<T> unauthorized(T data) {return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);}/*** 未授權返回結果*/public static <T> CommonResult<T> forbidden(T data) {return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);}public long getCode() {return code;}public void setCode(long code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public T getData() {return data;}public void setData(T data) {this.data = data;} }

IErrorCode

package com.app.tools;/*** 常用API返回對象接口*/ public interface IErrorCode {/*** 返回碼*/long getCode();/*** 返回信息*/String getMessage(); }

ResultCode

package com.app.tools;public enum ResultCode implements IErrorCode{SUCCESS(200, "操作成功"),FAILED(500, "操作失敗"),VALIDATE_FAILED(404, "參數檢驗失敗"),UNAUTHORIZED(401, "暫未登錄或token已經過期"),FORBIDDEN(403, "沒有相關權限");private long code;private String message;private ResultCode(long code, String message) {this.code = code;this.message = message;}public long getCode() {return code;}public String getMessage() {return message;} }

總結

以上是生活随笔為你收集整理的CommonResult响应工具类封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。