SpringBoot統一API響應結果封裝的示例詳解
更新時間:2025年03月27日 09:15:01 作者:青燈文案
在Spring Boot項目中,統一API響應結果封裝是一種常用的技術實踐,旨在提高開發(fā)效率、降低代碼重復率,并提供一致的API響應格式,從而簡化前后端交互和錯誤處理,感興趣的小伙伴可以了解下
封裝內容
響應結果封裝是將后端服務返回的響應數據按照統一的格式進行封裝,這個格式通常包括狀態(tài)碼、狀態(tài)信息(或稱為消息)、返回數據等關鍵信息。
- 狀態(tài)碼(Code):表示請求的處理結果,如成功、失敗、特定錯誤等。狀態(tài)碼通常是一組預定義的整數或枚舉值。
- 狀態(tài)信息/消息(Message):與狀態(tài)碼相對應的文本描述,用于提供關于請求處理結果的更多信息。
- 返回數據(Data):實際業(yè)務處理的結果數據,其類型可能因API而異。
此外,有些封裝還會包含時間戳、請求ID等附加信息,以便于日志追蹤和問題排查。
封裝示例
1、Result
package com.wen.data; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** * 統一 API 響應結果封裝 */ @Data @Builder @AllArgsConstructor @NoArgsConstructor public class Result<T> { private int code; private String message; private T data; public Result<T> setCode(ResultCode resultCode) { this.code = resultCode.code; this.message = resultCode.message; return this; } public Result<T> setMessage(String message) { this.message = message; return this; } public Result<T> setData(T data) { this.data = data; return this; } }
2、ResultCode
package com.wen.data; /** * 響應碼枚舉 */ public enum ResultCode { // 這里可以根據多個場景設置不同的響應碼,供前端判斷問題并進行解釋 SUCCESS(1,"SUCCESS"), FAIL(400,"FAIL"), NOT_FOUND(401,"interface not found"), ERROR(500,"System Exception!"); public int code; public String message; ResultCode(int code, String message){ this.code = code; this.message = message; } }
3、ResultGenerator
package com.wen.data; /** * 響應結果生成 */ public class ResultGenerator { public static Result<?> genSuccessResult(){ return new Result<>().setCode(ResultCode.SUCCESS); } public static Result<?> genSuccessResult(Object data){ return new Result<>().setCode(ResultCode.SUCCESS).setData(data); } public static Result<?> genFailResult(String message){ return new Result<>().setCode(ResultCode.FAIL).setMessage(message); } public static Result<?> genSpecialResult(ResultCode resultCode, String message){ return new Result<>().setCode(resultCode).setMessage(message); } }
4、TestController
package com.wen.controller; import com.wen.data.Result; import com.wen.data.ResultGenerator; import com.wen.dto.TbUser; import com.wen.service.TestService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @Autowired private TestService testService; @GetMapping("/select") public Result<?> selectUserByPage( @Param("pageSize") Integer pageSize, @Param("pageNumber") Integer pageNumber){ // 生成結果 return ResultGenerator.genSuccessResult(testService.selectUserByPage(pageSize, pageNumber)); } }
5、生成結果
{
"code": 1,
"message": "SUCCESS",
"data": "2024-07-19"
}
以上就是SpringBoot統一API響應結果封裝的示例詳解的詳細內容,更多關于SpringBoot統一API響應結果的資料請關注腳本之家其它相關文章!
相關文章
logback輸出日志屏蔽quartz的debug等級日志方式
這篇文章主要介紹了logback輸出日志屏蔽quartz的debug等級日志方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08利用Java8 Optional類優(yōu)雅如何地解決空指針問題
這篇文章主要給大家介紹了關于如何利用Java8 Optional類優(yōu)雅解決空指針問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Mockito 結合 Springboot 進行應用測試的方法詳解
這篇文章主要介紹了Mockito 結合 Springboot 進行應用測試的方法詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11