一个关于@JsonIgnore的问题
版本:2.13.5
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><optional>true</optional></dependency>
代码:
@Data
public class BigGreenCommonResult<T> implements Serializable {private Boolean success;private Integer code;private T data;private String msg;public static <T> BigGreenCommonResult<T> success(T data) {BigGreenCommonResult<T> result = new BigGreenCommonResult<>();result.code = GlobalErrorCodeConstants.SUCCESS.getCode();result.data = data;result.msg = "";result.success=true;return result;}public static boolean isSuccess(Integer code) {return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());}@JsonIgnore // 避免 jackson 序列化public boolean isSuccess() {return isSuccess(code);}
}
如上代码中,将注解@JsonIgnore作用在isSuccess()方法上时,会导致前端数据返回时不显示success属性,如下图所示
当将@JsonIgnore注解从isSuccess()方法拿掉时,则可以显示success响应的属性,如下图所示
尝试重写getSuccess()和setSuccess()方法,并在方法上面加上@JsonIgnore注解时得到的效果和加在isSuccess()上面的方法一致,初步判断@JsonIgnore会将isxxx()类型的方法转换为get或者set,这不符合逻辑呢,或者是一个bug