当前位置:网站首页>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);
}
边栏推荐
- online 方式创建索引触发trigger怎么办?
- ROS仿真环境搭建
- PHP base notes - NO. 1
- [Dataset][VOC] Rat dataset voc format 3001 sheets
- 【Deliberately practice the view of the back tube】deliberately practice
- ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
- 超T动力 焕“芯”出发 | 中国重汽专属定制版WP14T产品闪耀登场
- 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
- 剑指Offer 56.数组中数字出现的次数
- 动态打印菱形
猜你喜欢
随机推荐
Jenkins CI平台(二)
【汇编语言02】第2章 寄存器——理论知识
cocos creater 3.x 插件安装方法
LyScript 内存交换与差异对比
【计网】二、物理层
China Hashpower Conference Ascension Kunpeng Ecological Forum was held; Kuaishou established an independent to B business department…
【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
15、学习MySQL NULL 值处理
阿里资深专家打造从零开始学架构,含阿里内部技术栈PPT、PFD实战
EasyNTS上云网关断电重启后设备离线是什么原因?
excel写入不完全sheet.append方法(openpyxl)
基于ck+redash构建MySQL慢日志+审计日志展示平台
201712-3 CCF Crontab满分题解
ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
tree命令:以树的形式列出目录中的文件
[Dataset][VOC] Rat dataset voc format 3001 sheets
2021年数据泄露成本报告解读
Share 14 JS functions you must know
Flask框架——项目可安装化
借助kubekey极速安装Kubernetes