Java结果类Result

/**
 * @ClassName: CommonEnum
 * @Description:
 * @Author: 88578
 * @Date: 2022/6/10 23:26
 */
public interface CommonEnum {
    Integer getCode();
    String getMessage();
}
/**
 * @ClassName: ResultEnum
 * @Description:
 * @Author: 88578
 * @Date: 2022/6/10 23:25
 */
public enum ResultEnum implements CommonEnum{
    SUCCESS(200, "成功"),
    UNVALID(3000, null),
    UNAUTH_POST(3012, "失败"),
    MOBILE_UNAUTH(3060, "已投票"),
    MOBILE_AUTHED(3061, "手机号已验证"),
    MOBILE_UNVALID(3062, "手机号不合法"),
    MACHINE_UNVALID(3070, "提货柜编号不存在"),
    VERIFICATION_ERROR(4000, "验证码不匹配"),
    VERIFICATION_NULL(4003, "验证码过期或未发送"),
    CODE_NULL(4008, "code不存在"),
    CODE_ERROR(4009, "code过期"),
    OPENID_NULL(4010, "openId不存在"),
    OPENID_ERROR(4011, "openId错误"),
    MOBILE_BLANK(4025, "手机号为空"),
    MOBILE_INVALID(4026, "手机号格式不正确"),
    PARAMS_BLANK(4027, "参数为空"),
    ERROR(5000, null);

    private Integer code;
    private String message;

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return code;
    }


    @Override
    public String getMessage() {
        return message;
    }
}
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Result implements Serializable {
    private static final long serialVersionUID = 8582093728816173504L;

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    public Result(String key, Object value){
        this.setSuccess(true);
        this.setCode(ResultEnum.SUCCESS.getCode());
        this.setMsg(ResultEnum.SUCCESS.getMessage());
        this.data.put(key, value);
    }

    public Result(String key, Object value, String msg){
        this.setSuccess(true);
        this.setCode(ResultEnum.SUCCESS.getCode());
        this.setMsg(msg);
        this.data.put(key, value);
    }

    public Result(Map<String, Object> map){
        this.setSuccess(true);
        this.setCode(ResultEnum.SUCCESS.getCode());
        this.setMsg(ResultEnum.SUCCESS.getMessage());
        this.setData(map);
    }

    public Result(Map<String, Object> map, String msg){
        this.setSuccess(true);
        this.setCode(ResultEnum.SUCCESS.getCode());
        this.setMsg(msg);
        this.setData(map);
    }

}
import java.util.Map;

/**
 * @ClassName: ResultUtils
 * @Description:
 * @Author: 88578
 * @Date: 2022/6/10 23:36
 */
public class ResultUtils {

    public static Result ok(String key, Object value){
        return new Result(key, value);
    }

    public static Result okMsg(String key, Object value, String msg){
        return new Result(key, value, msg);
    }

    public static Result ok(Map<String, Object> data){
        return new Result(data);
    }

    public static Result okMsg(Map<String, Object> data, String msg){
        return new Result(data, msg);
    }

    public static Result error(){
        return Result.builder().success(false).code(ResultEnum.ERROR.getCode()).msg("失败").data(null).build();
    }

    public static Result errorMsg(String msg){
        return Result.builder().success(false).code(ResultEnum.ERROR.getCode()).msg(msg).data(null).build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值