Firebase Tutorial | Google Firebase | Connect to Firebase With Java Rest API | Save Data to Firebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone in this tutorial we will quickly see how to connect to firebase file store database and persist data through rest calls and this is an extension to my previous video how to build simple web services if you haven't seen it I will add a link in the description if you wish not to see the video I will add a link to the code to start with let's dive in we will be using firebase 5 store database so I am at my firebase console if you are not sure about firebase just go to ways firebase google.com and it will open you the console to the account and you can click on the go to console here if you don't have a firebase account it will ask you to just create one with your gmail ID it's free and you can create a project here like add project it will ask you to add the project name is demo2 since they already have firebase demo 1 continue and I will everything continue select an account this is my account create project and the project is ready once you go to the project you see this database on the Left menu bar click on it and create a database they go we will start in the test mode and it will ask you the location you can choose whatever location is near to you I will be choosing a use central location so it created a database named Fi our demo to some number and in this database this is a no sequel database and not a relational database like my sequel or Oracle database ur will start a collection and we will give the name users that's it and we can do that specify the fields to the name name age location that just be simple just 50 H can be a number of what I will just leave it for string because the earlier code we had is a string so just leave it there if you see here it created a document ID with this and it created the field try to give add a document it says like if you don't do any document ID here it will auto generate for you so you can give ie while you are working through the code and not an auto-generated code so let's go back to our project on a clip so here is our previous project which had four end points you get used the details create the user update the user and delete the user we will now connect to the firebase database that we created and we will persist data retrieved data and update the data using these endpoints to have a connection to the firebase database so we need to add the firebase dependency to firebase admin dependency so so we'll pick the latest version 6.1 and this is all you need so once we add this dependency we will have all the firebase related classes that we need to use to connect the firebase available for us in our maven dependencies folder here there you go you see the firebase here it already imported 6.11 jar for us to use so we had our dependency added now let's go and create a file-based initialize class which will open the connection to the firebase i'll create a package called service package which i'll have a class called firebase initialize to head this class I will have the method pop and we will have the connection details here so in order to connect to the firebase you need a key which is unique to your account and that you can create on the firebase let's go back to the firebase console here and if you go to the Settings here project settings so you can see this service accounts here go here and it shows you what is the code that you need to connect to the database so you can copy this and paste it back but if you see here it has a path to the service account key Jason so you need to generate a a private key here let's create it and generate the key so make sure you are not saving that key in a public repository when you are doing a production kind of stuff or local stuff it should be fine enough so let me change the name of it to surveys account and I want this in my project so I will save it to my project here so let's go back to the project and let me refresh it so if you see the so a second adjacent file is already here now let's go back and copy this whole thing here so it says path to the service account so let me just keep this it means it is in the home directory and it will look for the service account key but our JSON file is has a different name we will have this service account or Jason and if you see this set credentials of Google credentials from streams so we say count which we defined above and the database URL so if you go back here to the database it has eight nine two 0s that's what I added here so it will be connected to the eight nine two 0s now go back to our project so we are back in the project all right you got everything we need here for the connecting you want this connection to happen when the application startup so how do you do that so first we need to define this as a service so that the spring boot recognizes eight as a service and then there is an other annotation called at post construct what he does is when you had this annotation it will know that it has to be run during the startup once we had this annotation here it will try to make a connection to firebase during the startup itself we need to add a try-catch here if something error comes up we will add a try-catch here to catch any exceptions that occurred during this operation all right we are done here and we have this initializing class ready to go now we need to have a service class where it actually uses this connection and try to pass this data into the five store database let's go ahead and create a new class firebase service class we will define this class also as a service class so that a spring recognizes it as service so we will have a function here to create the data it should be a public function so this function will take person object as input and returns the string we need to connect to the firebase database using the connections that we had in the firebase initialized class for that we will use firestore which is our database this will get the database with the connection that we have created earlier once we got this we need to get the collections from this database the collection.we Creator is users collection we need to queue the document ID if you don't give a document ID here as we seen earlier it will create an auto-generated ID I will create the document ID using the person's name we created the document ID and we will set the collection now and we are returning a string here so let's return the timestamp of when this collection is being created a before going if you see here it is unheralded exception roll this exception pack there you go these are the two exceptions that might be thrown while doing this operations with the Firestone go ahead and start the application see what's going on here let's go back to res demo controller here and the operation we wrote is for the create user we will return we will import the service out of wire the service we created now from here if you see we are calling the firebase service and the method in that to save the user data details the person which we will get it from the request body let's go ahead and start it starter I'm opening my postman here so it opened in localhost 8080 let's create user create user and I don't have to give any of these in the body I will create a user with named a Roger 40 let's see what happens I am doing a get request here instead of post let's see what happens there you go it says 200 okay 100 and the timestamp it created now let's go back to the files to a database and see if it actually persisted this data there you go if you see this it created a document with the document ID Roger and it has the field that we just passed no we had done creating the user operation now we will do a different operation like getting the user updating the user and deleting the user now I'll go back to my service class and it will have a couple of other methods we will have the same connection with the firestore database there you go we got the client for it and here if you see earlier we got the API future object but when you are trying to get the data you actually will get a document reference for that particular document name and then we need to retrieve the API future object from the document reference and the collection used is users the document name would be the name this is where you pass the document ID now we got the document reference here we need the document snapshot that we need to retrieve from the document reference so this will get you the APA future of the type document snapshot if you see here once we got the document snapshot all we need to extract that a document snapshot from the APA future object which is actually the document we wanted see this get will return the document snapshot all right now we have the document here what we will do is so now we need to convert this document object to the person object that we are supposed to return in a l'objet if the document exists we will assign that person to the object we received since if you see the person object here it has string name age and location I to convert a school snapshot class who try to convert that object that we received to the person class if the variables are of same type within it if not it might throw an error let's go ahead and return the person object if the document doesn't exist we would return let's go back to our controller class here the get user details we will get the name and we will return the firebase always get the details of name I we should add the throws declaration here so that it can throw an exception let's restart we will perform the get operation here to get the details and in the headers we are passing the name of we created this roser object earlier now we will try to retrieve that object there you go the one we created earlier is being written now we will quickly perform the update operation and delete operation as well go back to the fire service and we will have two more functions so the update option is similar to the save operation we'll just copy paste it here beauty is when you are trying to get the document with the name if the name exists it will try to update the document and if the name doesn't exist it will try to create the taco basically we can use the same but to show it I will just use it here or to update it so now since we had this let's go ahead and try to do the update operation we need to change this back to calling the firebase service actually let me rerun it go back to the point to the put operation with update and have the body now I'll change the age 50 Roger is getting old and location back to Florida for retirement see it returned the timestamp the user got updated let's go back to five store and see if it actually got updated there you go so the beauty of five stories updates are like really instant if how the windows side-by-side you should be able to see it the moment you clicked on the send request it will update here that's the beauty of it now we will do the final operation delete operation similar to that we will create a new function to delete we'll have the connection to the five store we'll get the APA future objet what we need to do is you need to get the collection like earlier which is the users color churn and get the document we want to delay using the name which is actually the document ID and just too late that's all you need to do and return document with ID has been to later and we will update our controller class to call this that's it let's restart will do the delete operation give the name Roger that we created boom talking to diary Roger has been deleted now let's go back to five stores there is no user with ID Roger it has been deleted so let's do all the four operations quickly to see what's happening here I will try to have this side-by-side for our convenience it created the user and let me get the user details with Roger they go you got the details here Roger 50 Florida now let me update the details let's make him a little bit older or you can completely retire there you go see it's very quick and let's delete the user there you go it has been deleted right the only document left is David which I don't want to delete it now we saw the basic crud operations create read update and delete operations using spring boot and connecting to the five store database I hope this helps thank you [Music]
Info
Channel: HackerHeap
Views: 34,529
Rating: undefined out of 5
Keywords: Firebase tutorial, Firebase, Google Firebase, Firestore Database, firebase developers, springboot, restful webservices, java rest api, how to use firestore, how to use firebase, firebase admin sdk, admin sdk, java developers, android developers, firebase, tutorial
Id: ScsID2yPB8k
Channel Id: undefined
Length: 17min 53sec (1073 seconds)
Published: Sat Dec 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.