当前位置:网站首页>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);
}边栏推荐
- 借助kubekey极速安装Kubernetes
- Big guy, who is free to help me to see what the problem is, I just read MySQL source print, and I just came into contact with flink.
- dd命令:用于读取、转换并输出数据
- xxl-job 实现email发送警告的代码解析(一行一行代码解读)
- With the help of Kubernetes kubekey speed installation
- 87.(cesium之家)cesium热力图(贴地形)
- Share 14 JS functions you must know
- 在线监控机房内的UPS电源及运行环境,解决方案来了
- 使用range-based for循环的注意事项
- 【HCIP】MPLS实验
猜你喜欢

6000 字+,帮你搞懂互联网架构演变历程!

rhel8.3 系统下修改有线网卡配置信息实现联网

Online monitoring of UPS power supply and operating environment in the computer room, the solution is here

Postgresql 备份大小情况!

使用安全浏览器将网页保存为pdf的方法步骤

基于ck+redash构建MySQL慢日志+审计日志展示平台

CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes), problem: (D) Magical Array

pytest接口自动化测试框架 | Jenkins集成初探

多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG

阿里资深专家打造从零开始学架构,含阿里内部技术栈PPT、PFD实战
随机推荐
Unable to start SinkRunner: { policy:org.apache.flume
cdc抽取mysql整个实例的binlog,有没有方案通过配置的方式将这些库表拆开分发到kafka
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
MVCC多版本并发控制的理解
PHP基础笔记-NO.2
云渲染的优势与劣势
高数---级数
开篇-开启全新的.NET现代应用开发体验
Higher mathematics - chapter ten infinite series - constant term series
Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
使用range-based for循环的注意事项
VsCode预览Geojson数据
不要小看 WebSocket!长连接、有状态、双向、全双工都是王炸技能
es6新增-async函数(异步编程的最终解决方案)
【Deliberately practice the view of the back tube】deliberately practice
excel写入不完全sheet.append方法(openpyxl)
深度学习常用公式与命令总结(更新中)
2021年数据泄露成本报告解读
[Azure Event Hub] Create Event Hub Consume Client + Custom Event Position with Azure AD Authentication