如何構(gòu)建一個(gè)自定義返回

創(chuàng)建一個(gè)BaseRes的類

package com.example.lxkjavatest.Utils;

import lombok.Data;

import lombok.Getter;

import lombok.NoArgsConstructor;

@Getter

@NoArgsConstructor

@Data

public class BaseRes<T> {

? ? ? ? private int code;? ? // 狀態(tài)碼(如 200=成功,500=失?。?/p>

? ? ? ? private Stringmessage;? // 提示信息

? ? ? ? private T data;? ? ? // 返回的數(shù)據(jù)(泛型)

? ? ? ? private BaseRes(int code, String message){

? ? ? ? ? ? ? ? this.code = code;

? ? ? ? ? ? ? ? this.message = message;

? ? ? ? }

? ? ? ? private BaseRes(int code, String message, T data){

? ? ? ? ? ? ? ? this.code = code;

? ? ? ? ? ? ? ? this.message = message;

? ? ? ? ? ? ? ? this.data = data;

? ? ? ? }

? ? ? ? public static <T> BaseRes<T> error(String message, int code, T data){ return? new BaseRes<T>(code, message, data);}

? ? ? ? public static <T> BaseRes<T> error(String message, int code){ return? new BaseRes<T>(code, message);}

? ? ? ? public static <T> BaseRes<T> ok(String message){

? ? ? ? ? ? ? ? return new BaseRes<T>(200,message);

? ? ? ? }

? ? ? ? public static <T> BaseRes<T> ok(String message, T data){

? ? ? ? ? ? ? ? return new BaseRes<T>(200,message, data);

? ? ? ? }

? ? ? ? public static <T> BaseRes<T> error(String message){

? ? ? ? ? ? ? ? return new BaseRes<T>(500,message);

? ? ? ? }

? ? ? ? public static <T> BaseRes<T> error(String message, T data){

? ? ? ? ? ? ? ? return new BaseRes<T>(500,message, data);

? ? ? ? }

}


在返回時(shí)可任意定義返回code、message與data的類型

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容