SpringBoot 封装接口返回数据的统一结构
程序员文章站
2022-06-01 12:55:35
...
1. 封装代码
代码使用了Lombok
注解。
首先提供一个枚举,用于封装返回的提示码和提示信息。
package com.example.demo.result;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @auther wangbo
* @date 2021-01-10 15:59
*/
@Getter
@AllArgsConstructor
public enum ResultCode {
//成功提示码
SUCCESS(20000, "成功"),
//自定义失败信息
FAILURE(50000, "失败"),
//通用错误码 50001~50099
REQUEST_PARAM_ERROR(50001, "请求参数错误"),
//用户模块错误码 50100~50199
USER_HAVE_EXISTS(50101, "用户已存在"),
USER_NOT_EXISTS(50102, "用户不存在"),
USER_PASSWORD_ERROR(50103, "用户密码错误"),
USER_TYPE_ERROR(50104, "用户类型错误"),
USER_STATUS_ERROR(50105, "用户状态错误");
//商品模块错误码 50200~50299
//订单模块错误码 50300~50399
private Integer code;
private String message;
}
接下来提供一个返回类型,这里使用了泛型,用于对返回数据进行统一包装。
package com.example.demo.result;
import lombok.Data;
import java.io.Serializable;
/**
* @auther wangbo
* @date 2021-01-10 16:38
*/
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = -8037171286104362012L;
private Integer code;
private String message;
private T data;
/**
* 成功,无返回数据(静态方法)
*/
public static Result success() {
Result result = new Result();
result.setCode(ResultCode.SUCCESS.getCode());
result.setMessage(ResultCode.SUCCESS.getMessage());
return result;
}
/**
* 成功,有返回数据(非静态方法)
*/
public Result<T> success(T data){
this.code = ResultCode.SUCCESS.getCode();
this.message = ResultCode.SUCCESS.getMessage();
this.data = data;
return this;
}
/**
* 失败,自定义失败信息(静态方法)
*/
public static Result failure(String message) {
Result result = new Result();
result.setCode(ResultCode.FAILURE.getCode());
result.setMessage(message);
return result;
}
/**
* 错误,使用已定义枚举(静态方法)
*/
public static Result error(ResultCode resultCode) {
Result result = new Result();
result.setCode(resultCode.getCode());
result.setMessage(resultCode.getMessage());
return result;
}
}
2. 使用示例
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.result.Result;
import com.example.demo.result.ResultCode;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @auther wangbo
* @date 2021-01-10 17:48
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public Result<List<User>> list(){
List<User> list = userService.list();
Result<List<User>> result = new Result<>();
return result.success(list);
}
@GetMapping("/success")
public Result add(){
return Result.success();
}
@GetMapping("/failure")
public Result failure(){
return Result.failure("测试自定义失败信息");
}
@GetMapping("/error")
public Result error(){
return Result.error(ResultCode.REQUEST_PARAM_ERROR);
}
}
3. 结果示例
http://localhost:8080/user/list
{
"code":20000,
"message":"成功",
"data":[
{
"id":1,
"name":"姜晓东",
"age":32,
"email":"[email protected]",
"managerId":3,
"createTime":"2020-11-04T19:04:22",
"remark":null
},
{
"id":2,
"name":"侯波",
"age":32,
"email":"[email protected]",
"managerId":3,
"createTime":"2020-11-04T19:18:14",
"remark":null
},
{
"id":3,
"name":"赵睿",
"age":32,
"email":"@baomidou",
"managerId":2,
"createTime":"2020-11-04T19:20:12",
"remark":null
}
]
}
http://localhost:8080/user/success
{
"code":20000,
"message":"成功",
"data":null
}
http://localhost:8080/user/failure
{
"code":50000,
"message":"测试自定义失败信息",
"data":null
}
http://localhost:8080/user/error
{
"code":50001,
"message":"请求参数错误",
"data":null
}
下一篇: SQL:sqlite3数据库常用操作