import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Result implements Serializable {
private static final long serialVersionUID = 8582093728816173504L;
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String msg;
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data = new HashMap<String, Object>();
public Result(String key, Object value){
this.setSuccess(true);
this.setCode(ResultEnum.SUCCESS.getCode());
this.setMsg(ResultEnum.SUCCESS.getMessage());
this.data.put(key, value);
}
public Result(String key, Object value, String msg){
this.setSuccess(true);
this.setCode(ResultEnum.SUCCESS.getCode());
this.setMsg(msg);
this.data.put(key, value);
}
public Result(Map<String, Object> map){
this.setSuccess(true);
this.setCode(ResultEnum.SUCCESS.getCode());
this.setMsg(ResultEnum.SUCCESS.getMessage());
this.setData(map);
}
public Result(Map<String, Object> map, String msg){
this.setSuccess(true);
this.setCode(ResultEnum.SUCCESS.getCode());
this.setMsg(msg);
this.setData(map);
}
}
import java.util.Map;
/**
* @ClassName: ResultUtils
* @Description:
* @Author: 88578
* @Date: 2022/6/10 23:36
*/
public class ResultUtils {
public static Result ok(String key, Object value){
return new Result(key, value);
}
public static Result okMsg(String key, Object value, String msg){
return new Result(key, value, msg);
}
public static Result ok(Map<String, Object> data){
return new Result(data);
}
public static Result okMsg(Map<String, Object> data, String msg){
return new Result(data, msg);
}
public static Result error(){
return Result.builder().success(false).code(ResultEnum.ERROR.getCode()).msg("失败").data(null).build();
}
public static Result errorMsg(String msg){
return Result.builder().success(false).code(ResultEnum.ERROR.getCode()).msg(msg).data(null).build();
}
}