SpringBoot封装接口返回值对象

SpringBoot封装接口返回值对象

  • Result.java
  • ResultEnum.java
  • ResultFactory.java

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);
    }
}

你可能感兴趣的:(Springboot框架搭建,spring,boot,java,后端,接口,返回值封装)