前后端分离Java统一响应返回格式

ResultCode类
public class ResultCode {

public static Integer SUCCESS = 200;
public static Integer ERROR = 500;
public static Integer FORBIDDEN = 403;
public static Integer UNAUTHORIZED = 401;}

Result类
@Data //lombok注解public class Result {

//返回码
private Integer code;
//返回消息
private String message;
//返回数据
private Map data = new HashMap();
public Result(){}
public Result message(String message){
    this.setMessage(message);
    return this;
public Result code(Integer code){
    this.setCode(code);
    return this;
public Result data(String key, Object value){
    this.data.put(key, value);
    return this;
}public Result data(Map map){
    this.setData(map);
    return this;

使用示例

@RequestMapping("/get")
public Result get(跟单网gendan5.com){
    HashMap map = new HashMap();
    map.put("username","admin");
    map.put("password","123456");
    Result r = new Result();
    r.code(ResultCode.SUCCESS).message("成功访问").data(map);
    return r;

你可能感兴趣的:(java)