当前位置:网站首页>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);
}边栏推荐
猜你喜欢
随机推荐
使用range-based for循环的注意事项
【美丽天天秒】链动2+1模式开发
MPLS的简单应用于实验
6000 字+,帮你搞懂互联网架构演变历程!
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
pydev debugger: warning: trying to add breakpoint to file that does not exist: /tmp/xxx
Jenkins CI平台(二)
你想知道的 Watch App 开发
谷歌浏览器安装插件教程步骤,开发用这2个插件工作效率倍增
有人知道flink sql 使用tableEnv.executeSql执行后,怎么获取到任务运行的
Weekly recommended short video: In order to fill the gap of learning resources, the author specially wrote a book?
借助kubekey极速安装Kubernetes
【Deliberately practice the view of the back tube】deliberately practice
online 方式创建索引触发trigger怎么办?
MySQL——增删改查进阶
cdc抽取mysql整个实例的binlog,有没有方案通过配置的方式将这些库表拆开分发到kafka
Atomic Wallet已支持TRC20-USDT
MySQL如何 drop 大表
2022/08/02------Ugly number
云图说丨初识华为云微服务引擎CSE









