基于Java編寫一個通用返回工具類Result
更新時間:2023年07月20日 17:19:21 作者:ProsperousEnding
Java項目搭建時,常常需要去封裝一個通用型的Result工具類,下面小編就和大家分享一個已經封裝好的常用的返回類,希望對大家有所幫助
前言
Java項目搭建時,常常需要去封裝一個通用型的Result工具類,下面就是我自己封裝的常用的返回類,可以直接使用。(有部分Swagger注解,使用時可忽略)
第一步、創(chuàng)建ReusltUtils工具類
package com.code.walker.utils; import com.code.walker.constant.ResultCode; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; import lombok.experimental.Accessors; import java.io.Serializable; /** * @author ProsperousEnding-fhl * @create 2023-07-20-15:16 */ @Getter @Setter @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) @ApiModel(value = "響應信息體") public class ResultUtils<T> implements Serializable { /** * 響應碼 */ @Getter @Setter @ApiModelProperty(value = "響應標記:成功標記=0,失敗1") private Integer code; /** * 響應信息 */ @Getter @Setter @ApiModelProperty(value = "響應信息") private String message; /** * 響應數據 */ @Getter @Setter @ApiModelProperty(value = "響應數據") private T data; @Getter @Setter @ApiModelProperty(value = "返回狀態(tài)") private boolean status; private ResultUtils(ResultCode resultCode,T data,boolean status){ this.code = resultCode.getCode(); this.message = resultCode.getMessage(); this.data = data; this.status=status; } /** * 無數據成功返回 * * @return */ public static <T>ResultUtils success(){ return new ResultUtils<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),null,true); } /** * 帶數據返回 */ public static <T> ResultUtils success(T data){ return new ResultUtils<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data,true); } /** * 失敗 */ public static <T>ResultUtils fail(){ return new ResultUtils<T>(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMessage(), null,false); } /** * 失敗 */ public static <T> ResultUtils fail(T data){ return new ResultUtils<T>(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMessage(), data,false); } @Override public String toString() { return "ResultUtils [code=" + code + ", message=" + message + ", data=" + data + "]"; } }
第二步 創(chuàng)建常用的報錯信息類
在日常開發(fā)中可不寫,將ReusltUtils中的ResultCode的代碼換成常量即可
注:主要是為了美觀以及修改方便,所以去單獨的封裝一個常量信息類
/** * @author ProsperousEnding-fhl * @create 2023-07-20-15:46 */ @Getter public enum ResultCode { /** * 成功 */ SUCCESS(200, "成功"), FAIL(1000, "失敗"), FAILED(400, "請求失敗"), NOT_FOUND(404, "未找到"), SERVER_ERROR(500, " 服務器內部出錯 "), /** * 錯誤參數 */ PARAM_IS_INVALID(1001, "參數無效"), PARAM_IS_BLANK(1002, "參為空"), PARAM_TYPE_ERROR(1003, "參數類型錯誤"), PARAM_NOT_COMPLETE(1004, "參數缺失"), /** * 用戶錯誤 */ USER_NOT_LOGIN_IN(2001, "用戶未登錄"), USER_LOGIN_ERROR(2002, "賬號不存在或者密碼錯誤"), USER_ACCOUNT_FORBIDDEN(2003, "賬戶被禁用"), USER_NOT_EXISTS(2004, "用戶不存在"), USER_HAS_EXISTED(2005, "用戶已存在"); /** * 代碼 */ private final Integer code; /** * 信息 */ private final String message; private ResultCode(Integer code, String message) { this.code = code; this.message = message; } }
第三步、調用方法以及返回樣式
調用方式:
使用時可以直接使用ResultUtils.方法的方式
public Result getUser() { User user1=new User(); user1.setName("codeTalker") return Result.success(user1); }
返回樣式:
{ "code": 200, "message": "成功", "data":{ "name":"codeTalker" } "status": true }
以上就是基于Java編寫一個通用返回工具類Result的詳細內容,更多關于Java通用返回工具類的資料請關注腳本之家其它相關文章!
相關文章
Springboot 整合 Dubbo/ZooKeeper 實現 SOA 案例解析
這篇文章主要介紹了Springboot 整合 Dubbo/ZooKeeper 詳解 SOA 案例,需要的朋友可以參考下2017-11-11