import lombok.Builder;
import lombok.ToString;
@ToString
@Builder
public class CommonResult<T> {
private Integer code;
private String msg;
private T data;
public CommonResult(){
}
protected CommonResult(Integer code,String msg,T data){
this.code=code;
this.msg=msg;
this.data=data;
}
public static <T> CommonResult<T> success(){
return new CommonResult<T>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMessage(),null);
}
public static <T> CommonResult<T> success(T data){
return new CommonResult<T>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMessage(),data);
}
public static <T> CommonResult<T> success(T data, String msg) {
return new CommonResult<T>(ResultEnum.SUCCESS.getCode(),msg,data);
}
public static <T> CommonResult<T> fail(String msg) {
return new CommonResult<T>(ResultEnum.FAIL.getCode(),msg,null);
}
public static <T> CommonResult<T> fail(ResultEnum resultEnum) {
return new CommonResult<T>(resultEnum.getCode(),resultEnum.getMessage(),null);
}
public static <T> CommonResult<T> fail(ResultEnum resultEnum,T data) {
return new CommonResult<T>(resultEnum.getCode(),resultEnum.getMessage(),data);
}
public static <T> CommonResult<T> fail(DefinedException exception) {
return new CommonResult<T>(exception.getCode(),exception.getMessage(),null);
}
public Integer getCode(){
return code;
}
public void setCode(Integer code){
this.code=code;
}
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg=msg;
}
public T getData(){
return data;
}
public void setData(T data){
this.data=data;
}
}
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class DefinedException extends RuntimeException{
private Integer code;
private String message;
public DefinedException(Integer code,String message) {
this.message = message;
this.code = code;
}
public DefinedException(ExceptionEnum exceptionEnum) {
this.message = exceptionEnum.getMessage();
this.code = exceptionEnum.getCode();
}
}
public enum ExceptionEnum {
SYSTEM_EXCEPTION(300, "系统异常!"),
TOKEN_EXCEPTION(301,"token异常!"),
ExceptionEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
@Getter
@Setter
private Integer code;
@Getter
@Setter
private String message;
}
public enum ResultEnum {
SUCCESS(200, "请求成功!"),
FAIL(100, "请求失败!");
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
@Getter
@Setter
private Integer code;
@Getter
@Setter
private String message;
}