SpringBoot封装接口返回值对象

发布时间:2024年01月07日

SpringBoot封装接口返回值对象

Result.java

public class Result {
    private int code;
    private String message;
    private Object result;
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Object getResult() {
        return result;
    }
    public void setResult(Object result) {
        this.result = result;
    }
    public Result(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.result = data;
    }
}

ResultEnum.java

public enum ResultEnum {
    SUCCESS(200),
    FAIL(400);

    public int code;

    ResultEnum(int code) {
        this.code = code;
    }
}

ResultFactory.java

package com.seven.config;

import com.seven.enums.ResultEnum;
import com.seven.model.Result;

public class ResultFactory {

    public static Result SUCCESS_RESULT = new Result(200,"SUCCESS",null);
    public static Result FAIL_RESULT = new Result(400,"FAIL",null);

    public static Result buildSuccessResult(Object data) {
        return buildResult(ResultEnum.SUCCESS, "成功", data);
    }

    public static Result buildFailResult(String message) {
        return buildResult(ResultEnum.FAIL, message, null);
    }

    public static Result buildResult(ResultEnum resultEnum, String message, Object data) {
        return buildResult(resultEnum.code, message, data);
    }

    public static Result buildResult(int resultCode, String message, Object data) {
        return new Result(resultCode, message, data);
    }
}
文章来源:https://blog.csdn.net/xieweikun_7/article/details/135434463
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。