一个简单的数据类型返回

package com.jiayue.ipfcst.common.core.web.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.jiayue.ipfcst.common.core.constant.enums.ResponseEnum;
import com.jiayue.ipfcst.common.core.exception.BusinessException;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**

  • 数据格式返回统一

*/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseVO implements Serializable {

private static final long serialVersionUID = 1L;

/**
 * 状态码
 */
@Getter
@Setter
private Integer code;

/**
 * 描述
 */
@Getter
@Setter
private String message;

/**
 * 数据
 */
@Getter
@Setter
private T data;

public static <T> ResponseVO<T> success() {
    return restResult(ResponseEnum.SUCCESS.getCode(), null, null);
}

public static <T> ResponseVO<T> success(T data) {
    return restResult(ResponseEnum.SUCCESS.getCode(), data, ResponseEnum.SUCCESS.getMessage());
}

public static <T> ResponseVO<T> success(T data, String message) {
    return restResult(ResponseEnum.SUCCESS.getCode(), data, message);
}

public static <T> ResponseVO<T> fail() {
    return restResult(ResponseEnum.FAILED.getCode(), null, null);
}

public static <T> ResponseVO<T> fail(T data) {
    return restResult(ResponseEnum.FAILED.getCode(), data, ResponseEnum.FAILED.getMessage());
}

public static <T> ResponseVO<T> fail(T data, String msg) {
    return restResult(ResponseEnum.FAILED.getCode(), data, msg);
}

public static ResponseVO fail(BusinessException exception) {
    return restResult(ResponseEnum.FAILED.getCode(), null, exception.getMessage());
}

public static ResponseVO error(Throwable exception) {
    return restResult(ResponseEnum.ERROR.getCode(), null, exception.getMessage());
}

private static <T> ResponseVO<T> restResult(Integer code, T data, String message) {
    ResponseVO<T> apiResult = new ResponseVO<>();
    apiResult.setCode(code);
    apiResult.setData(data);
    apiResult.setMessage(message);
    return apiResult;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值