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.