1、业务需求
当查询不到数据时不给前端返回null,而是保持swagger文档的输出结果结构并带默认值
改造前
{
"status": false,
"data": null,
"code": 404,
"message": "未找到资源"
}
改造后
{
"status": false,
"data": {
"dwrs": "/",
"spjk": 0,
"dbjc": false,
"xhsb": 0L,
"gzsb": []
},
"code": 404,
"message": "未找到资源"
}
2、封装工具类
为什么要使用泛型,是为了迁就swagger的输出文档ApiModel
/**
* @Description
* @Date 2022/4/6 9:31
* @Author YuLe
**/
public class BeanInit<T> {
/**
* 给泛型对象赋默认值
*
* @param old
* @param <T>
* @return
*/
public static <T> T getDefault(T old) {
try {
Field[] fields = old.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
// 得到field的class及类型全路径
String fieldClazz = field.getType().toString();
//System.out.println("字段名称"+field.getName()+" 字段类型" + fieldClazz);
//Object value = field.get(old);
//System.out.println("字段值" + value);
if (fieldClazz.contains("String")) {
//字符类型默认 /
field.set(old, "/");
} else if (fieldClazz.contains("List")) {
//数组对象默认 []
field.set(old, new ArrayList<>());
} else if (fieldClazz.contains("Double")) {
//double对象默认 0L
field.set(old, 0L);
} else if (fieldClazz.contains("boolean")) {
//布尔对象默认 false
field.set(old, false);
} else {
//数值对象默认 0
field.set(old, 0);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return old;
}
public static void main(String[] args) {
XqcrkjlVo newBean = getDefault(new XqcrkjlVo());
System.out.println(newBean.toString());
}
}
3、使用方法
//返回对象的情况
@GetMapping("/ssajdt")
@ApiOperation(value = "实时案件动态")
public Rest<SsajdtVo> ssajdt() {
return Rest.emptyObj(BeanInit.getDefault(new SsajdtVo()));
}
//返回列表的情况
@GetMapping("/mjxtfx")
@ApiOperation(value = "门禁系统分析", notes = "海创园的设备状态统计,无数据")
public Rest<List<KeyValOfIntVo>> mjxtfx() {
return Rest.emptyArray();
}
附上统一返回实体类
/**
* @Description 统一返回报文
* @Date 2021/6/24 21:42
* @Author YuLe
**/
@ApiModel(value = "标准报文")
public class Rest<T> {
@ApiModelProperty(value = "是否成功")
private boolean status = true;
@ApiModelProperty(value = "正常业务对象")
private T data;
@ApiModelProperty(value = "响应编号")
private Integer code;
@ApiModelProperty(value = "错误信息")
private String message;
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* 系统异常
*
* @param msg
* @param <T>
* @return
*/
public static <T> Rest<T> fail(String msg) {
Rest<T> rest = new Rest<>();
rest.setCode(500);
rest.setStatus(false);
rest.setMessage(msg);
return rest;
}
/**
* 返给前端空数组
*
* @return
*/
public static <T> Rest<List<T>> emptyArray() {
Rest<List<T>> rest = new Rest<>();
rest.setCode(404);
rest.setStatus(false);
rest.setMessage("未找到资源");
rest.setData(new ArrayList<T>());
return rest;
}
/**
* 返回默认的空对象
*
* @param <T>
* @return
*/
public static <T> Rest<T> emptyObj(T defaultObj) {
Rest<T> rest = new Rest<>();
rest.setCode(404);
rest.setStatus(false);
rest.setMessage("未找到资源");
rest.setData(defaultObj);
return rest;
}
/**
* 治理平台返回的异常
*
* @param msg
* @param <T>
* @return
*/
public static <T> Rest<T> fail(Integer code, String msg) {
Rest<T> rest = new Rest<>();
rest.setStatus(false);
rest.setCode(code);
rest.setMessage(msg);
return rest;
}
/**
* 可预见的异常
*
* @param me
* @param <T>
* @return
*/
public static <T> Rest<T> fail(ExceptionEnum me) {
Rest<T> rest = new Rest<>();
rest.setCode(me.getResultCode());
rest.setStatus(false);
rest.setMessage(me.getResultMsg());
return rest;
}
/**
* 成功报文
* @param data
* @param <T>
* @return
*/
public static <T> Rest<T> success(T data) {
Rest<T> rest = new Rest<>();
rest.setCode(200);
rest.setData(data);
rest.setStatus(true);
return rest;
}
}
|