当前位置:网站首页>Web项目Controller统一返回实体类
Web项目Controller统一返回实体类
2022-08-03 18:29:00 【Hejjon】
ResponseResult.java
package com.hejjon.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "统一返回实体类")
public class ResponseResult<T> {
public static final int OK_CODE = 200;
public static final int FAIL_CODE = 500;
@ApiModelProperty("状态码")
private int code;
@ApiModelProperty("信息")
private String message;
@ApiModelProperty("数据")
private T data;
private ResponseResult(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public static <T> ResponseResult<T> success() {
return new ResponseResult<>(OK_CODE, "成功", null);
}
public static <T> ResponseResult<T> success(T data) {
return new ResponseResult<T>(OK_CODE, "成功", data);
}
public static <T> ResponseResult<T> success(String msg, T data) {
return new ResponseResult<>(OK_CODE, msg, data);
}
public static <T> ResponseResult<T> fail() {
return new ResponseResult<>(FAIL_CODE, "失败", null);
}
public static <T> ResponseResult<T> fail(String msg) {
return new ResponseResult<T>(FAIL_CODE, msg, null);
}
public static <T> ResponseResult<T> fail(String msg, T data) {
return new ResponseResult<>(FAIL_CODE, msg, data);
}
}
使用方法
1. 返回单个实体类型
@GetMapping("/get/{id}")
@ApiOperation("根据主键获取用户信息")
public ResponseResult<BaseUser> get(@PathVariable("id") String id) {
if (StringUtils.isEmpty(id)) {
return ResponseResult.fail("id不可为空");
}
BaseUser user = baseUserService.getById(id);
return ResponseResult.success(user);
}
2. 返回实体集合
@PostMapping("/list")
@ApiOperation("全部学生列表")
public ResponseResult<List<BaseStudent>> list() {
List<BaseStudent> list = baseStudentService.list();
return ResponseResult.success(list);
}
3. 返回实体分页对象(Mybatis)
@ApiOperation("分页列表")
@PostMapping("/page")
public ResponseResult<Page<BaseStudent>> page(@RequestBody BaseQueryParam param) {
Page<BaseStudent> page = new Page<>(param.getPageNum(), param.getPageSize());
Page<BaseStudent> pagedata = baseStudentService.page(page);
return ResponseResult.success(pagedata);
}
边栏推荐
- Difference差分数组
- Online monitoring of UPS power supply and operating environment in the computer room, the solution is here
- dd命令:用于读取、转换并输出数据
- Oracle备份的几种方式
- @resource和@autowired的区别
- 域名抢注“卷”到了表情包?ENS逆势上涨的新推力
- MySQL database account management and optimization
- WEB 渗透之CSRF
- With the help of Kubernetes kubekey speed installation
- 谷歌浏览器安装插件教程步骤,开发用这2个插件工作效率倍增
猜你喜欢
LineSegmentTree线段树
Weekly recommended short video: In order to fill the gap of learning resources, the author specially wrote a book?
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
【HCIP】MPLS实验
MySQL——增删改查进阶
细胞不可渗透的荧光探针 锌离子荧光探针Zinquin 151606-29-0
Install porterLB
MySQL 啥时候用表锁,啥时候用行锁?这些你都应该知道吧
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
Flask框架——项目可安装化
随机推荐
cocos creater 3.x 插件安装方法
开篇-开启全新的.NET现代应用开发体验
【白话模电2】二极管特性和分类
动态接口比例性能测试实践
注意力机制的详细理解
ASA归因:如何评估关键词的投放价值
Chrome浏览器开发新截图工具,安全浏览器截图方法
WEB 渗透之RCE
PHP基础笔记-NO.2
实现博客营销有哪些技巧
MySQL如何 drop 大表
【Django-Docker】Sqlite3.db读取权限不够-20220803
OSError: [WinError 123] 文件名、目录名或卷标语法不正确
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
快手通过国际权威信息安全和隐私保护认证,安全能力达到国际领先水平
技术开发人员常用的安全浏览器
Install porterLB
fatal error: jni.h: No such file or directory
Mkke:为什么无法从Oracle 11g或12c升级到Oracle 23c?
Cyanine5.5 alkyne|Cy5.5 alkyne|1628790-37-3|Cy5.5-ALK