后端(10)-springboot接口数据模型返回额外字段的问题

49 阅读1分钟

问题

我的数据模型基类中只有三个参数:

public class ApiResponse<T> {
    private int code;
    private String message;
    private T data;   
}

但是接口返回却返回了四个参数:

{
    "code": 200,
    "message": "Success",
    "data": {
        "user": "{}",
        "token": "eyrWw"
    },
    "success": true
}

从哪儿增加了一个success的boolean字段呢?

原因

原因就是在ApiResponse中我增加了一个工具方法boolean isSuccess():

 public boolean isSuccess() {
     return getCode()==ResponseConstant.Code.SUCCESS;
 }

编译器会默认把他解析成一个success字段,所以返回就增加了一个success字段。

解决

移除掉这个方法,放到工具类中即可。

PS:不要在Springboot接口返回数据模型中定义字段外增加其他模版方法,避免造成莫名其妙增加字段的情况。