(三)SpringBoot定义统一返回result对象
一:定义响应码枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.example.demo.core.ret; /** * @Description: 响应码枚举,参考HTTP状态码的语义 * @author * @date 2018/4/19 09:42 */ public enum RetCode { // 成功 SUCCESS( 200 ), // 失败 FAIL( 400 ), // 未认证(签名错误) UNAUTHORIZED( 401 ), // 接口不存在 NOT_FOUND( 404 ), // 服务器内部错误 INTERNAL_SERVER_ERROR( 500 ); public int code; RetCode( int code) { this .code = code; } } |
二:创建返回对象实体(泛型)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.example.demo.core.ret; import java.io.Serializable; import com.alibaba.fastjson.JSON; /** * @Description: 返回对象实体 * @author * @date 2018/4/19 09:43 */ public class RetResult<T> { public int code; private String msg; private T data; public RetResult<T> setCode(RetCode retCode) { this .code = retCode.code; return this ; } public int getCode() { return code; } public RetResult<T> setCode( int code) { this .code = code; return this ; } public String getMsg() { return msg; } public RetResult<T> setMsg(String msg) { this .msg = msg; return this ; } public T getData() { return data; } public RetResult<T> setData(T data) { this .data = data; return this ; } } |
说明:code为状态码、msg为提示信息、data为返回的数据
四:返回结果数据格式封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.example.demo.core.ret; /** * @Description: 将结果转换为封装后的对象 * @author * @date 2018/4/19 09:45 */ public class RetResponse { private final static String SUCCESS = "success" ; public static <T> RetResult<T> makeOKRsp() { return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS); } public static <T> RetResult<T> makeOKRsp(T data) { return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS).setData(data); } public static <T> RetResult<T> makeErrRsp(String message) { return new RetResult<T>().setCode(RetCode.FAIL).setMsg(SUCCESS); } public static <T> RetResult<T> makeRsp( int code, String msg) { return new RetResult<T>().setCode(code).setMsg(msg); } public static <T> RetResult<T> makeRsp( int code, String msg, T data) { return new RetResult<T>().setCode(code).setMsg(msg).setData(data); } } |
五:功能测试
1 2 3 4 5 | @RequestMapping ( "/selectById" ) public RetResult<UserInfo> selectById(Integer id){ UserInfo userInfo = userInfoService.selectById(id); return RetResponse.makeOKRsp(userInfo); } |
改造后请求返回数据格式
1 2 3 4 5 6 7 8 | { "code" : 200 , "msg" : "success" , "data" : { "id" : 1 , "userName" : "1" } } |
分类:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现