Java ResponseDto<T>

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class ResponseDto<T> {

    @ApiModelProperty(value = "是否成功,true=成功,false=失败", position = 1)
    private boolean success = true;

    @ApiModelProperty(value = "描述", position = 2)
    private String msg = "操作成功";

    @ApiModelProperty(value = "消息体", position = 3)
    private T data;

    public ResponseDto() {

    }

    public ResponseDto(boolean success) {
        this.success = success;
    }

    public ResponseDto(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    public ResponseDto(boolean success, String msg, T data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public ResponseDto(T data) {
        this.data = data;
    }

    /**
     * 成功,并返回消息体
     * @param data
     * @return
     */
    public static <T>ResponseDto<T> ok(T data) {
        return new ResponseDto(data);
    }

    /**
     * 失败,返回消息内容
     * @param msg
     * @return
     */
    public static <T>ResponseDto<T> fail(String msg) {
        return new ResponseDto(false, msg);
    }
}

 

posted @ 2023-12-12 09:42  都是城市惹的祸  阅读(99)  评论(0)    收藏  举报