JAVA 接口返回JSON格式转换类

 

 使用了Lombok插件

Result.java  

package com.utils;

import com.jetsum.business.common.constant.Constant;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;


@Data   
public class Result<T> {
    private Integer code;
    private String msg;
    private String message;
    private T data;


     public Result ok(T t){
        this.setCode(1000);
        this.setMsg("成功");
        this.setMessage("成功");
        this.setData(t);
        return this;
    }

    public Result fail(String msg){
        this.setCode(1001);
        this.setMsg(msg);
        this.setMessage(msg);
        return this;
    }
public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.setMessage(msg);
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.setMessage(msg);
    }
}

 

 

使用方式

return new Result().ok(data);

 

posted @ 2020-06-24 14:47  yvioo  阅读(1111)  评论(0编辑  收藏  举报