How to use Cloud Firestore in Spring Boot.

Ruhshan Ahmed Abir
Javarevisited
Published in
3 min readDec 31, 2020

--

Photo by NOAA on Unsplash

Firestore is a managed, scalable document storage available in Firebase platform. Why I like it? You can start using it and get 20K reads and writes per day whithout paying a dime event without sharing your credit card info. 20K read writes are more than enough for experimenting and creating a small to medium application.

Firebase have already provided some feature rich client packages for different programming languages. There is also one for Java too, and its documentation is well written to get started with. In this post I want to share how I used some moden java and spring goodies in my project to configure and access firestore.

Firstly I created a firestore project in firebase console and retrieved access key to use in my application. Then I added the dependency firebase-admin in my spring boot project.

Then I created a package called firestore in my project and created some files inside it.

├── firestore
│ ├── AbstractFirestoreRepository.java
│ ├── DocumentId.java
│ └── FireStoreConfig.java

The FireStoreConfig.java contains a Bean which reads and uses the credential and returns a FireStore instance than can be used do access collections and documents. This is how my FireStoreConfig class looks like:

Here, I’ve taken the path of the downloaded credential JSON from application.properties. Though I prefer to save and retrieve credential related stuff from environment variable, for this case I was unable to find a way like that.

Each document we store in Firestore consists an unique Id. I created a annotation so that I can annotate a field in my entity class which will be used as this Id. I named this DocumentId and it looks like:

Finally I’ve written AbstractFirestoreRepository which is an abstract generic class. From this class I can create any number of concrete repositories providing a type. This abstract repository contains CRUD methods by implementing firestore apis in generic manner. Thus I don’t have to write same boilerplate code for every collection I create for my project. And it looks like this:

Here, I tried to keep the most methods self explanatory. All the public methods are just a reflection of how we save , get or delete documents in firestore. But I would like to add few words about the other methods which will help you better understand what I’m doing.

getParameterizedType: As this is a generic class that mean I can specify a type with it. Such as I can make a User repository like this :

public class UserRepo extends AbstractFirestoreRepository<User> {}

In some methods like get and retrieveAll I actually need the User.class object. This getParameterizedType helps to get the type specified as <T>.

getDocumentId: I’ve mentioned before to store a document in the firestore, we need a unique Id, this method checks each field in the object it receives for the DocumentId annotation. If found it returns this, if not it returns a UUID. As this method has protected modifier, it can be overridden in the child class according to need.

Now, by extending this method I can create as many repository I need in my project. A sample repository will look like this:

Here is the super method, first argument is the Firestore instance instantiated with the Bean in FireStoreConfiguration, and second argument is the the name of the Collection.

And, User class looks like this:

See, here id attribute is annotated with DocumentId, it will help to use the value of this field as the Id when creating the document in firestore.

That’s all for today. I really wanted to write a generic method for custom queries. But given the syntax for queries, and its heterogeneity, it felt each concrete repository should handle them by their own. But if you have any idea about that, or a better structure to integrate firestore in springboot, feel free to share with us.

--

--

Ruhshan Ahmed Abir
Javarevisited

Started with poetry, ended up with codes. Have a university degree on Biotechnology. Works and talks about Java, Python, JS. www.buymeacoffee.com/ruhshan