September 14, 2021

Google Firebase Firestore Spring boot Integration sample code

1. Create a new Cloud Firestore database in your GCP project

2. If you are authenticated in the Cloud SDK, your credentials will be automatically found by the Spring Boot Starter for Google Cloud Firestore.

3. Pom dependency

<!-- https://mvnrepository.com/artifact/com.google.cloud/spring-cloud-gcp-starter -->
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter</artifactId>
    <version>2.0.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-firestore -->
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-firestore</artifactId>
    <version>3.0.1</version>
</dependency>



4. Repository (writing data)

@Autowired
Firestore firestore;

User data = new User("javabelazy",
Arrays.asList(
new Phone(12345, PhoneType.CELL),
new Phone(54321, PhoneType.WORK)));

WriteResult writeResult = this.firestore.document("users/id").set(data).get();

5. Application.properties

spring.cloud.gcp.firestore.project-id =abcdefg

6. Git URL

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments