首页 > 编程语言 >定义java 返回集合

定义java 返回集合

时间:2024-10-19 14:44:45浏览次数:6  
标签:code return 定义 ResultsCode static Result 集合 java public

1、创建Result类

package com.flow.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public int code;

    private String msg;

    private T data;

    private final Map<String, Object> mapdata = new HashMap<String, Object>();

    public void Result() {
        this.code = ResultsCode.SUCCESS.code;
        this.msg = ResultsCode.SUCCESS.message;
    }

    public Result<T> setCode(ResultsCode resultsCode) {
        this.code = resultsCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Result<T> putDataValue(String key, Object value) {
        mapdata.put(key, value);
        this.data = (T) mapdata;
        return this;
    }

    @Override
    public String toString() {
        return "{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

2、创建一个ResultsCode枚举类型

public enum ResultsCode {

    SUCCESS(200, "成功"),

    FAIL(-1, "失败"),

    ERROR(500, "服务器异常"),

    UNAUTHORIZED(401, "未认证(签名错误)"),

    FORBIDDEN(403, "禁止访问"),

    NOT_FOUND(404, "接口不存在"),

    AUTH_ERROR(-10000, "鉴权登陆失败,请重新登录!");

    public int code;
    public String message;

    ResultsCode(int code, String message) {
        this.code = code;
        this.message = message;
    }
}

3、创建ResponseData类

package com.flow.utils;

import com.flow.pojo.Result;
import com.flow.pojo.ResultsCode;

import static com.flow.pojo.ResultsCode.AUTH_ERROR;

public class ResponseData {
    /****
     * 成功响应Msg中的信息
     **/
    private final static String SUCCESS = "success";

    /****
     * 失败响应Msg中的信息
     **/
    private final static String FAILED = "failed";

    private final static String FORBIDDEN = "forbidden";


    public static <T> Result<T> success(T data) {
        return new Result<T>().setCode(ResultsCode.SUCCESS).setMsg(SUCCESS).setData(data);
    }

    public static Result success() {
        return new Result().setCode(ResultsCode.SUCCESS).setMsg(SUCCESS);
    }

    public static Result forbidden() {
        return new Result().setCode(ResultsCode.FORBIDDEN).setMsg(FORBIDDEN);
    }

    public static Result authError() {
        return error(AUTH_ERROR);
    }

    public static <T> Result<T> error(ResultsCode resultsCode) {
        return new Result<T>().setCode(resultsCode.code).setMsg(resultsCode.message);
    }


    public static <T> Result<T> error(String mesage) {
        return new Result<T>().setMsg(mesage).setCode(ResultsCode.FAIL);
    }

    public static <T> Result<T> error(String mesage, ResultsCode resultsCode) {
        return new Result<T>().setMsg(mesage).setCode(resultsCode.code);
    }

    public static <T> Result<T> error() {
        return new Result<T>().setCode(ResultsCode.FAIL).setMsg(FAILED);
    }

    public static <T> Result<T> Forbidden() {
        return new Result<T>().setCode(ResultsCode.FORBIDDEN).setMsg(FORBIDDEN);
    }
}

4、测试 返回结果类型是Result

@GetMapping("/flow_list")
public Result list_flow_process() {
   List<FlowProcess> list = flowProcessService.list();
   return ResponseData.success(list);
}

返回结果类型

标签:code,return,定义,ResultsCode,static,Result,集合,java,public
From: https://www.cnblogs.com/pao1pao/p/18475868

相关文章