【问题标题】:How do I get a DocumentReference field from Firestore?如何从 Firestore 获取 DocumentReference 字段?
【发布时间】:2020-10-05 12:14:18
【问题描述】:

我正在尝试从 google Firestore 中检索一个对象,这是对象:

Firestore object

由于我添加了属性 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 我在请求数据时收到此错误

ERROR 7544 --- [nio-8080-exec-7] s.e.ErrorMvcAutoConfiguration$StaticView : 无法为请求 [request route] 和异常呈现错误页面 [Could not write JSON: Infinite recursion (StackOverflowError);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (通过引用链: com.google.cloud.firestore.FirestoreImpl["options"]->com.google.cloud.firestore.FirestoreOptions["服务"]->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["凭据 ["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":

【问题讨论】:

  • 您可能需要考虑尝试单独从快照中提取每个字段,而不是使用 toObject。
  • 刚刚试过了,还是一样的错误,谢谢你的帮助
  • 您能否显示获得单个 soat 字段的代码以及在这种情况下获得的错误。我不确定这会如何引发StackOverflowError
  • Car emp = doc.toObject(Car.class); 当我使用这部分代码时,它会从第一张图片中显示的 Firebase 对象中检索全部信息并使用 Car 类对其进行映射,所以我永远不会得到 @987654332 @单独。

标签: java firebase google-cloud-firestore


【解决方案1】:

基本上,您必须创建一个类来反序列化您的 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 时,您应该像这样获得对 Object 的正确引用。

{
    "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"
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2018-05-16
    相关资源
    最近更新 更多