java统一结果集封装

2021-03-01 04:28

阅读:665

标签:pack   ring   iba   amp   ret   返回   封装   alibaba   err   

统一返回结果集封装


package com.example.springbootlearn.common;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: fjh
 * @create: 2021-02-26 11:34
 * 

* 统一结果返回 */ @Data public class R implements Serializable { private String code; private String msg; private T data; static JSONObject emptyJson = new JSONObject(); public static R ok() { return ok(ResultEnum.SUCCESS.code, ResultEnum.SUCCESS.msg); } public static R ok(String msg) { return ok(ResultEnum.SUCCESS.code, msg); } public static R ok(T data) { return ok(ResultEnum.SUCCESS.code, ResultEnum.SUCCESS.msg, data); } public static R ok(String code, String msg) { return ok(code, msg, emptyJson); } public static R ok(String code, String msg, T data) { return info(code, msg, data); } public static R error() { return error(ResultEnum.FAILURE.code, ResultEnum.FAILURE.msg); } public static R error(String msg) { return error(ResultEnum.FAILURE.code, msg); } public static R error(T data) { return error(ResultEnum.FAILURE.code, ResultEnum.FAILURE.msg, data); } public static R error(String code, String msg) { return error(code,msg,emptyJson); } public static R error(String code, String msg, T data) { return info(code,msg,data); } public static R info(String code, String msg, T data) { R r = new R(); r.setCode(code); r.setMsg(msg); r.setData(data); if (null == data) { r.setData(emptyJson); } else if (data instanceof String) { List list = new ArrayList(); list.add(data.toString()); r.setData(list); } return r; } public static enum ResultEnum { SUCCESS("200", "操作成功"), FAILURE("-1", "操作失败"); public String code; public String msg; ResultEnum(String code, String msg) { this.code = code; this.msg = msg; } } public R() { } public R(String code, String msg) { this.code = code; this.msg = msg; } public R(String code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } }

java统一结果集封装

标签:pack   ring   iba   amp   ret   返回   封装   alibaba   err   

原文地址:https://www.cnblogs.com/fjh0512/p/14452226.html


评论


亲,登录后才可以留言!