簡體   English   中英

如何從 Firestore 獲取 DocumentReference 字段?

[英]How do I get a DocumentReference field from Firestore?

我試圖從谷歌 Firestore 檢索一個對象,這是對象:

Firestore 對象

由於我添加了屬性 soat 我得到了錯誤,我使用這個類來反序列化對象:

@Data
public class Car {
    private String plate;
    private String model;
    private long km;
    private boolean available;
    private DocumentReference soat;   
}

這段代碼從 Firestore 中檢索所有 Car 對象

     @Override
    public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
                Car emp = doc.toObject(Car.class);
                empList.add(emp);
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
        return empList;
    }

由於我添加了屬性 soat 我在請求數據時收到此錯誤

錯誤 7544 --- [nio-8080-exec-7] seErrorMvcAutoConfiguration$StaticView:無法為請求 [請求路由] 和異常呈現錯誤頁面 [無法寫入 JSON:無限遞歸 (StackOverflowError); 嵌套異常是 com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)(通過參考鏈:com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions[" service"]->com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options" ]->com.google.cloud.firestore.FirestoreOptions["service"]->com.google.cloud.firestore.FirestoreImpl["options"]...->com.google.cloud.firestore.FirestoreOptions["credentia ["modulus"])] 因為響應已經提交。因此,

以前我無法正確發出請求,所以我將此添加到我的 application.properties

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

編輯:剛剛嘗試了這種方法,但仍然出現相同的錯誤

    @Override
    public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {
               
                 String plate=doc.getData().get("plate").toString();
                 String model=doc.getData().get("model").toString();
                 long km=Long.parseLong(doc.getData().get("km").toString());
                 boolean available=Boolean.parseBoolean(doc.getData().get("available").toString());
                 DocumentReference soat=(DocumentReference)doc.getData().get("soat");

                Car emp = new Car();
                emp.setAvailable(available);
                emp.setKm(km);
                emp.setModel(model);
                emp.setPlate(plate);
                emp.setSoat(soat);

                empList.add(emp);
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
        return empList;
    }

- - - -編輯 - - - -
我已將此添加到我的 application.properties

spring.jackson.serialization.fail-on-self-references=false

現在我從 Firestore 電話中得到了這個

[{"plate":"HEO628","model":"2014","km":75000,"available":true,"soat":{"path":"SOATS/HEO628","parent":{"parent":null,"id":"SOATS","path":"SOATS","firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":{"firestore":

基本上,您必須創建一個類來反序列化您的 DatabaseReference,在這種情況下,類名將是SOAT並使用它來存儲您對數據庫的引用。

@Data
public class Car {
    private String plate;
    private String model;
    private long km;
    private boolean available;
    private SOAT soat;  
    private Insurance insurance;
    private Techno techno;
}

然后,當從 FireBase 檢索信息時,您必須發出以下請求以指定要映射的對象類型。

public List<Car> findAll() {
        List<Car> empList = new ArrayList<Car>();
        CollectionReference car = fb.getFirestore().collection("Cars");
        ApiFuture<QuerySnapshot> querySnapshot = car.get();
        try {
            for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {

                String plate = doc.getData().get("plate").toString();
                String model = doc.getData().get("model").toString();
                long km = Long.parseLong(doc.getData().get("km").toString());
                boolean available = Boolean.parseBoolean(doc.getData().get("available").toString());
                DocumentSnapshot soatref = fb.getFirestore().collection("SOATS").document(plate).get().get();
                DocumentSnapshot technoref = fb.getFirestore().collection("Technos").document(plate).get().get();
                DocumentSnapshot insuranceref = fb.getFirestore().collection("Insurances").document(plate).get().get();
                SOAT soat = soatref.toObject(SOAT.class);
                Techno techno = technoref.toObject(Techno.class);
                Insurance insurance = insuranceref.toObject(Insurance.class);

                Car emp = new Car();
                emp.setAvailable(available);
                emp.setKm(km);
                emp.setModel(model);
                emp.setPlate(plate);
                emp.setSoat(soat);
                emp.setInsurance(insurance);
                emp.setTechno(techno);

                empList.add(emp);
            }
        } catch (Exception e) {

        }
        return empList;
    }

然后當你調用你的 API 時,你會像這樣得到一個對對象的正確引用。

{
    "plate": "HEO628",
    "model": "2014",
    "km": 75000,
    "available": true,
    "soat": {
      "expeditionDate": "2020-09-29T06:12:12.000+00:00",
      "carPlate": "HEO628"
    },
    "insurance": {
      "expeditionDate": "2020-10-01T11:12:12.000+00:00",
      "carPlate": "HEO628"
    },
    "techno": {
      "expeditionDate": "2020-09-08T17:00:00.000+00:00",
      "carPlate": "HEO628"
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM