CommonResult响应工具类封装
生活随笔
收集整理的這篇文章主要介紹了
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响应工具类封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我想开一家影音工作室,不知道要买哪些设备
- 下一篇: Redis(案例二:高并发商品首页热点数