Firestore - 究竟要放哪个@Exclude注释?

Flo*_*her 3 java android firebase google-cloud-firestore

我很困惑在哪里放置@Exclude我不想在我的Cloud Firestore数据库中的字段的注释.

仅仅将它放在getter方法上就足够了吗?它还有什么作用将它添加到setter方法或变量声明?

在我的示例中,我不想存储文档ID,因为这将是多余的:

public void loadNotes(View v) {
    notebookRef.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    String data = "";

                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Note note = documentSnapshot.toObject(Note.class);
                        note.setDocumentId(documentSnapshot.getId());
                        String title = note.getTitle();
                        String description = note.getDescription();

                        data += "\nTitle: " + title + "Description: " + description;
                    }

                    textViewData.setText(data);
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

型号类:

public class Note {
private String documentId;
private String title;
private String description;

public Note() {
    //public no arg constructor necessary
}

public Note(String title, String description) {
    this.title = title;
    this.description = description;
}

@Exclude
public String getDocumentId() {
    return documentId;
}

public void setDocumentId(String documentId) {
    this.documentId = documentId;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}
Run Code Online (Sandbox Code Playgroud)

}

Ale*_*amo 11

因为您正在使用类中private的字段的修饰符,Note要从Cloud Firestore数据库中排除属性,您应该将@Exclude注释放在相应的getter之前.

@Exclude
public String getDocumentId() {return documentId;}
Run Code Online (Sandbox Code Playgroud)

仅仅将它放在getter方法上就足够了吗?

是的,这就够了.如果您已经public为字段使用了修饰符,要忽略属性,您应该@Exclude在属性之前放置注释:

@Exclude 
public String documentId;
Run Code Online (Sandbox Code Playgroud)

它还有什么作用将它添加到setter方法或变量声明?

没有效果.

  • 如果您使用 Kotlin,请使用 `@get:` 在属性上添加注释,例如 `@get:Exclude val level: String` (8认同)