0

I am new to GCP firestore. I am using FirestoreReactiveRepository in my spring boot application for CRUD operations. I created a POJO class with some properties which I want to save in my firestore document. I am using lastModified property with @ServerTimestamp annotation in my POJO class so firestore can read this annotation and automatically populate this property with server time. But @ServerTimestamp is not working and I am getting a null value in my document for the last_modified date key.

Here is the POJO class code: User.java

public class User {

    @DocumentId
    private String documentId;
    @NotNull
    private String name;
    @Min(1)
    private int age;
    @NotNull
    private String gender;
    @ServerTimestamp
    private Date lastModified;

    public User() {
    }

    public String getDocumentId() {
       return documentId;
    }

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

    public String getName() {
       return name;
    }

    public void setName(String name) {
       this.name = name;
    }

    @PropertyName("person_age")
    public int getAge() {
       return age;
    }

    @PropertyName("person_age")
    public void setAge(int age) {
       this.age = age;
    }

    @PropertyName("person_gender")
    public String getGender() {
       return gender;
    }

    @PropertyName("person_gender")
    public void setGender(String gender) {
       this.gender = gender;
    }

    @PropertyName("last_modified")
    public Date getLastModified() {
       return lastModified;
    }

    @PropertyName("last_modified")
    public void setLastModified(Date lastModified) {
       this.lastModified = lastModified;
    }

 }

UserController.java
@RestController
public class UserController {

    @Autowired
    private UserRepo userRepo;

    @PostMapping("/add")
    public ResponseEntity<String> addUser(@RequestBody @Valid User user) {
        System.out.println(user.toString());
        String documentId = user.getName();
        user.setDocumentId(documentId.toLowerCase().replace(" ","_"));
        this.userRepo.save(user).block();
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

I don't know what mistake I am doing here. How can I use @ServerTimestamp annotation properly so it can populate last_modified property with the server time automatically? Thanks in advance.

Aakash Giri
  • 53
  • 1
  • 8
  • Does this answer your question? [How to add a Timestamp in Firestore with Android?](https://stackoverflow.com/questions/48474957/how-to-add-a-timestamp-in-firestore-with-android) – Lluís Muñoz Dec 30 '21 at 09:41
  • @LluísMuñoz If we are working with a normal Firestore instance for example like this db.collection("cities").document("LA").set(docData); only then "@ServerTimestamp" working with POJO class but If we are using FirestoreReactiveRepository to communicate with firestore then, in this case, "@ServerTimestamp" doesn't work. – Aakash Giri Dec 31 '21 at 16:44

0 Answers0