在Spring Boot项目中,通用返回对象是一种用于封装接口统一返回结果的模式,它能够提高接口的可读性、可维护性,并且规范了接口的返回格式。通常,开发人员使用一个统一的数据结构来包裹接口返回的数据,这就是通用返回对象。
一般每个团队都会有自己的接口规范,比如调用api接口返回的响应报文结构需要指定结构,就需要我们使用通用的返回对象了
提示:以下是本篇文章正文内容,下面案例可供参考
直接贴代码例子
1、首先在自己的项目下创建一个目录名为 response的包
2、创建三个class类 ResponseResult、ResponseResultCount、ResponseResultData
①ResponseResult类
@JsonIgnoreProperties(
ignoreUnknown = true
)
public class ResponseResult {
//"状态码(0成功,非0失败)"
private int code;
//"状态码说明"
private String msg = "ok";
public ResponseResult() {
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public boolean isSuccess() {
return 0 == this.code;
}
public static ResponseResult success() {
ResponseResult responseResult = new ResponseResult();
responseResult.setCode(0);
return responseResult;
}
public static ResponseResult fail(String msg) {
ResponseResult responseResult = new ResponseResult();
responseResult.setCode(-1);
responseResult.setMsg(msg);
return responseResult;
}
}
②ResponseResultCount
@JsonIgnoreProperties(
ignoreUnknown = true
)
public class ResponseResultCount<T> {
//"状态码(0成功,非0失败)"
private int code;
//"状态码说明"
private String msg = "ok";
//"响应数据"
private T data;
//"总行数"
private long count;
public ResponseResultCount() {
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public long getCount() {
return this.count;
}
public void setCount(long count) {
this.count = count;
}
public boolean isSuccess() {
return 0 == this.code;
}
public static <T> ResponseResultCount<T> success(T data, long count) {
ResponseResultCount<T> responseResultCount = new ResponseResultCount();
responseResultCount.setCode(0);
responseResultCount.setData(data);
responseResultCount.setCount(count);
return responseResultCount;
}
}
③ResponseResultData
@JsonIgnoreProperties(
ignoreUnknown = true
)
public class ResponseResultData<T> {
//"状态码(0成功,非0失败)"
private int code;
//"状态码说明"
private String msg = "ok";
//"响应数据"
private T data;
public ResponseResultData() {
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess() {
return 0 == this.code;
}
public static <T> ResponseResultData<T> success(T data) {
ResponseResultData<T> responseResultData = new ResponseResultData();
responseResultData.setCode(0);
responseResultData.setData(data);
return responseResultData;
}
public static <T> ResponseResultData<T> fail(String msg) {
ResponseResultData<T> responseResultData = new ResponseResultData();
responseResultData.setCode(-1);
responseResultData.setMsg(msg);
return responseResultData;
}
}
我这里定义了三个返回对象,分别对应不同的返回情况
当接口只需要返回是否调用是否成功的结果,而不需要返回业务数据时,就用该ResponseResult响应对象即可
例:
@PostMapping("/demo")
public Response demo(@RequestParam Map<String, Object> requestBody){
//业务场景处理
if(xxxxxx){
//出现异常或者业务需要返回错误
return ResponseUtil.error("自定义错误信息")
}
//若接口成功调用则返回成功
return ResponseUtill.success();
}
//该接口成功返回报文如下:
// {
// "code": 0,
// "msg": "",
// "success": true
// }
//接口返回错误信息如下,我这里失败统一返回-1了。大家可以根据情况自己增加错误码
// {
// "code": -1,
// "msg": "自定义错误信息",
// "success": false
//}
**
因为这个对象有一个count属性,一般接口需要返回数组列表数据的时候使用该响应对象
例
//获取用户列表
@GetMapping("/getUsers")
public ResponseResultCount<List<ReqTaskVO>> getReqTaskViews() {
List<User> users= demoService.getDataList(Map<String, String> param);
return ResponseResultCount.success(users,users.size());
}
// 该接口响应报文如下
{
"code": 0,
"msg": "ok",
"data": [],
"count": 0,
"success": true
}
这个对象和第二个有所不同的是没有count属性,也就不需要返回具体的个数,除了页面需要查看数据的个数除外,大部分情况都可适用
例
//根据用户id查询 用户详情信息
@GetMapping("/getUserInfo")
public ResponseResultData<User> getUserInfo(@RequestParam String userId) {
User user= userService.getUserInfo(userId);
return ResponseResultData.success(user);
}
//该接口的成功响应报文 如下
{
"code": 0,
"msg": "ok",
"data": {},
"success": true
}