cn.woodwhales.common.model.vo.RespVO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of woodwhales-common Show documentation
Show all versions of woodwhales-common Show documentation
https://github.com/woodwhales
package cn.woodwhales.common.model.vo;
import cn.woodwhales.common.model.enums.RespCodeEnum;
import cn.woodwhales.common.model.result.BaseRespResult;
import cn.woodwhales.common.model.result.OpResult;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.util.Objects;
import java.util.function.Function;
/**
* 通用响应视图
* @author woodwhales on 2020-08-25
*
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RespVO {
private Integer code;
private String msg;
private T data;
public static RespVO success() {
return build(RespCodeEnum.SUCCESS, null);
}
public static RespVO success(T data) {
return build(RespCodeEnum.SUCCESS, data);
}
public static RespVO error() {
return build(RespCodeEnum.ERROR, null);
}
public static RespVO error(BaseRespResult baseRespResult) {
return buildWithBaseRespResult(baseRespResult, null);
}
public static RespVO error(BaseRespResult baseRespResult, T data) {
return buildWithBaseRespResult(baseRespResult, data);
}
public static RespVO buildWithBaseRespResult(BaseRespResult baseRespResult) {
return buildWithBaseRespResult(baseRespResult, null);
}
public static RespVO errorWithErrorMsg(String errorMsg) {
return errorWithErrorMsg(RespCodeEnum.ERROR, errorMsg);
}
public static RespVO errorWithErrorMsg(BaseRespResult baseRespResult, String errorMsg) {
Objects.requireNonNull(baseRespResult);
return build(baseRespResult.getCode(), errorMsg, null);
}
public static RespVO buildWithBaseRespResult(BaseRespResult baseRespResult, T data) {
Objects.requireNonNull(baseRespResult);
return build(baseRespResult.getCode(), baseRespResult.getMessage(), data);
}
public static RespVO resp(OpResult opResult, @NotNull Function function) {
Objects.requireNonNull(function, "对象转换接口不允许为空");
if(opResult.isSuccessful()) {
return success(function.apply(opResult.getData()));
}
BaseRespResult baseRespResult = opResult.getBaseRespResult();
return error(baseRespResult, function.apply(opResult.getData()));
}
public static RespVO resp(OpResult opResult) {
if(opResult.isSuccessful()) {
return success(opResult.getData());
}
BaseRespResult baseRespResult = opResult.getBaseRespResult();
return error(baseRespResult, opResult.getData());
}
private static RespVO build(Integer code, String message, T data) {
RespVO respVO = new RespVO<>();
respVO.setCode(code);
respVO.setMsg(message);
respVO.setData(data);
return respVO;
}
private static RespVO build(RespCodeEnum respCodeEnum, T data) {
return buildWithBaseRespResult(respCodeEnum, data);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy