gpt4 book ai didi

java - Android:表示接口(interface)的所有字段都是@Nullable

转载 作者:行者123 更新时间:2023-12-02 12:12:22 25 4
gpt4 key购买 nike

我正在使用 Retrofit 从 REST API 加载数据。

我知道这个 API 有时会返回部分结果:某些字段有时会丢失。

为了确保正确处理这些情况,我目前在定义 API 的接口(interface)中使用 @Nullable 注释字段:

class RedactedForecast implements Serializable {
@Nullable String date;
@Nullable String time;
@Nullable String overview_title;
@Nullable String overview;
@Nullable String forecast_title;
@Nullable String forecast;
}

但是,在任何地方添加此注释并不方便,因为结果对象很复杂。

有没有办法说一个类中的所有字段都可能为空?有没有更好的方法来处理这个问题?

最佳答案

However adding this annotation everywhere is not convenient since the result objects are complex.

解耦结构并将数据映射到部分对象以方便开发。

不幸的是,没有简单的方法来注释整个类,将某个注释应用于其所有字段。

如果你仍然依赖这个解决方案,我建议你看看库 Javassist ,为此提供了一个解决方案。 Here是用作实际存在问题的答案的示例。

关于java - Android:表示接口(interface)的所有字段都是@Nullable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com