How to use Firebase with Spring boot REST Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the spring boot plus firebase tutorial where we create an application or a rest api crud application where we use cloud firestore a database by firebase and google for our backend and we see how we can use a cloud database and see how it works and we can also connect our application with springboot and rest api so we can manipulate it with using rest apis so so let's get started with our tutorial so first let's talk about what is firebase and what is uh cloud firestore so cloud faster is basically a flexible scalable no sequel cloud database where it stores and syncs our data for client and server side development in our application we'll be doing service development where we ask our springboot server to actually get data from cloud firestore and then how we can access it create retrieve update or delete our data so cloud firestore is a flexible scalable databases for mobile web and server deployment from firebase and google cloud similar to what firebase has with real-time databases it keeps our data in sync across client applications through real-time listeners it also offers offline support for mobile and web so basically when we create uh update or delete data inside our application uh we will be seeing a dashboard where we can actually see our data get deleted live and all of them will be updated in real time so we'll see that how that happens and how we can connect our application a local application to a server which is given to us by firebase and google so some uh capabilities that we need to highlight when we're using firebase is the the fact that why it's so famous and why people actually use it for their apps so the first thing is flexibility uh it provides hierarchical infrastructure so uh data is stored in documents uh organizational collections and then we can make complex nested objects in sub collections and we can always query them very easily so it's very flexible to use our cloud firestore for our needs we have expressive querying so you can use queries to retrieve individual specific documents or retrieve all documents and this can include multiple chain filters combine filtering with sorting all sorts of fancy stuff which you want to do on your database next is realty updates again a fun thing to see so we can see it uses data synchronization to update data on any connected devices so if you have like 10 apps connected to your single cloud firestore database then you can see it updated on all of them live in real time it'll be fun to see how that works and how we can actually use it so yeah this is basically how it works so it's a cloud of state no secret database uh for all the platforms and it's available in native node.js java python unity so on and so forth we will be using the java arrest api uh library and framework to actually query our data and we'll develop a simple crud api so whenever we learn a new technology the best way to get started or the hello world of learning anything about web servers or web frameworks would be to create a cloud application in it it teaches how to interact with the database teaches how the basic functions work methods work and also gives us a simple current application at the end which we always create it will be fun to work with that and in the tutorials we'll see how everything connects together and how easy it is to actually connect our firebase flash store database with our local springboard application so uh first would uh i would highly recommend you to go through the documentation we cannot cover every single single thing in documentation uh in our videos we'll jump into the implementation directly so i really really encourage you to go to the description of the video and read enough about cloud firestore to be comfortable working with it so that we can have a very good and productive discussion whenever the video is on so yeah let's get started so first thing which we need to do is create a project in firebase uh but before that let's actually create our application a springboard application so that we have everything from application side ready in the next video we can directly continue with our firebase configurations so as as like any other online api or sdk we have to have a secret keys which we can use from given by firebase for our application so that you know we can authorize and authenticate each and every uh requested signs and also basically how all the libraries and maven dependencies ready so let's do that boilerplate stuff in this video and then the next video we can actually start building some stuff using our application so here if you can see the screen i have a very simple uh maven project which has a pom.xml nothing inside so far but just a pompet xml so first let's fill this up and let's actually have something inside here which we can use so to do that let me quickly get all the dependencies that we need so that we can then just talk about it and then focus more of our time on how do we actually uh set up firebase on its site so let me just get this done and once we get this done let's just quickly have maven pull them might get some errors or if it is not properly configured but i think my environment and parts are correct so not to worry in that aspect and yep i think everything looks pretty good smooth uh let the indexing and everything get done okay great so let's go through our dependencies so since you're working with the spring boot rest api project we will have spring bootstrap web to have basic necessities uh in terms of annotations which we need to use lombok is to make sure data looks good and again annotations will help us help our credit api journey smooth with all the annotations it provides next is the firebase admin dependency which is basically help us interact with firebase using their underlying apis and we'll see how easy it is to actually work with the firebase admin and see how it works lastly would be to have a spring boot application use a maven plugin to install all of our dependencies and just build our project properly so pretty simple boilerplate uh code here nothing crazy that's going on i would say the next thing would be to have a runner so if we have a runner it will just be easy to run our project from the next time so create a package and let the name of the package be com dot java dot fire base inside this package we can have our runner class we'll have all of our classes here but let's call it crud so we're just going to build a simple crud app right so let's just call it runner and inside this we'll just start our application so all we have to do here is copy a bunch of stuff nothing crazy all we are doing here is having a main method so object static void main string args and let's call this our springboard application entry point and have a very simple springboard application runner which basically runs our application so yeah this is a simple basic information which we need in our application to run it and we have our pom.xml also set now let's jump on to the firebase configuration side where we need to set up our application so uh what do you do go to console.firebase.google.com and if you don't see this page don't worry all you have to do is add a project so make sure you find that and from here will be the same steps which we do let's call this firebase tutorial db uh and let's see how it works so have a name for your project firebase project this can be disabled for now and google will basically create our database database and everything for for us so all you have to do is just wait it takes us a couple of seconds but if it's taking some more time then i'll probably wait for a few more uh minutes and then see if it's happening usually it's pretty smooth no problem here so yep we are good to go with our application and as you can see we have a firebase tutorial db ready uh up and ready to use so in the next video we'll see how we can actually set up get all the secret keys from firebase and set it up inside our local application so that when we run our application we connect to the correct database and also create a database and make sure that everything is smooth in terms of connecting so that in the later videos we can then start building our application and building our cloud endpoints for firebase so in this video we'll talk about how we can authenticate uh or authorize our local springboot application to connect to firebase and also connect to our database which we create on the firebase dashboard so in this video we'll mainly focus on setting up or connecting our local application using some runner starter on code or boilerplate template mainly to connect to firebase and also see how it actually gives us our secret keys and it's a very a fun little way of how they do it so before we jump to uh this the contents for this video let's just have a refresher on what we had done previously so we had set up our basic spring boot newman project where we have all of our dependencies ready and we also went on and created a project on firebase using control.firebase.google.com and we had a very simple project up and running so in this video let's actually go ahead with where we have stopped which is basically this particular dashboard i'm using a database which i've already created so it's easier to show how it works as you can see i've already done 12 reads and one right two writes here so we'll see how this looks like so let's go ahead and then jump into what's next to be done so now that you've created your firebase project we need to connect it to our local application and for that we definitely need our secret keys right a bunch of stuff bunch of keys which we need to connect to our local application so how do we get them so go to settings here project settings and go to search accounts so the accounts which we use for our application and here what you need to do is go to database secrets or you can either go here as well so hdk or this legacy so not supported anymore and separated used the edmund sdk so let's go ahead and do that here for any implementation we have uh the configuration ready and we can generate a private key which is going to basically give us this json file so when we click on generate new private key it gives us this particular json file which we then save inside our application and load it when we are setting up our runner so we are using java so this is going to be our java code for our application and also gives us the database url which is our current url and we can copy this and put it inside our java code so before that let's just create a go to generate new private key and let's do generate key and this will give us a service account key dot json so yeah here you go we have our json ready and all you have to do is just call it service account key even if you don't call it that it's fine but just to understand what it's doing call it doesn't save it inside your application so where do we actually save it so we save it here so go inside resources it's basically a resource inside our file and do a new file and call it service account e dot json and inside this we can put our application so let me just take out all our files and put it here so this is basically what's inside my service account key dot json it'll be different for you and this will uh deactivate until we upload the video it has a bunch of uh keys email client id tokens a bunch of search urls certification all of that inside ourselves key dot json now what we need to do is uh load that file inside our application and run time and then use that to actually authorize our application at runtime so that whenever we use our application can directly automatically go and authorize us to use firebase right so let's see how we do that and how do we actually work with this so first go inside a meetup main method and do a class loaded class loader equal to card runner dot class dot get class loaded so we get the current class order here and then we get the file from where we actually want the resource or we load the file so what we do is we create a file let's import the class sorry java i o and this is going to be a new file and all we are going to do is class loader dot get resource and where do we get the resource from so since it's going to be in the class path because everything which is under resources goes on the java class path we can all we can do is say to service account key dot json and it will read it for us now uh once we get this we need to return the absolute path for this particular file so what we do is we do a dot get file let me just check how this works available there so this is how it's going to look like so we have five input streams we have absolute path and let's add an exception to our signature file not found and yeah simple stuff so we have a service account here ready and we get our absolute path you can do a system dot uh outdoor printer link to see how we get the path and what where it is actually point to the next thing would be to copy basically this and paste it here so let's do that and once we do that we will have our application up and running and we'll go through what's happening in the code as well so here i have uh what we use for here so let's just add our method signatures and we are good to go so what's happening here so we use the firebase options class and we ask set the credentials from our service account which is a file and then we set our database url and we build it after we build our options we initialize our applications with those firebase options and that basically authorizes us to use firebase so this is what's happening and how we connect our project or database with our local app it's pretty straightforward pretty simple just copy or generate your private key which gives you this service account json file put it inside your resources folder and then use this boilerplate code to initialize your firebase application to authorize firebase so we have this done and ready now let's create our database which we can connect to our application so go to file store and here we have our database url so we basically have a database up and running we just need to add stuff to it right so here i have a collection inside my database and let's just create a new collection so let's call this crud user so basically somebody who's using this uh application and document id is going to be let's say user one and the field is going to be name and it's going to have a value let's say oh sorry yes uh the next thing you're going to have is uh perfection so let's say freelancer and then we can just do a same so this is uh us making a schema or a collection inside our nosql database it has a user one and it also has two fields inside our database so this is how we created a new schema inside our fire card main database and now we can connect our application and then basically get this or add stuff to this so this is our database ready and whenever we do a create update or delete from our local application we can see that updating at real time in our particular dashboard so in the next video we'll start creating our uh object our class our data and our dtos for our application where we can create services on top of it credit services create update lead services and then actually hit our firestore database and see how it works all of that in the next video so what did we learn in this particular video we learned how we can create or authorize our local application with firebase we had some boilerplate code which basically loads a resources file which has our all the secret keys and then we use firebase options to connect to our firebase application and then run it all this is done and then we create our firestore database which updates in real time create a new collection and then we can uh query our database using the crud user collection and then so on and so forth so we have our application ready so let's just do a run crowdrunner and that should initialize our firebase application and also run our springboot application and hopefully nothing breaks here so we have our tomcat server up and running and let's just quickly do a sanity check no errors so far and everything is being executed and yep looks great nothing broke so yeah let's talk about what we are going to do in this video so we have our local application and it has been authorized to hit firebase now we for our application first we need to do is have data which we need to you know create update or do some operations on so first let's create our data and then we can do a bunch of things with that data so go inside your package new java class call it crud so this is going to be a simple data which we can operate on let's have our setter and getters from lumbar so that it's easier to work with them the next thing which we need are a bunch of attributes for our data so here inside our data we just have two um fields we have the name and the profession and we have the document id right so let's uh do that so let's have all the three here so we have private string document id we have private string name and so this is basically the attributes which we have inside each document for our crud user schema inside our fire crud firebase cloud main database so this is how we can access our object in a serializable manner which gives us these three things whenever we want them now this is the data which you'll be working on where we update delete uh and uh retrieve this particular in json format data in json format so what's next now we need to have a controller which basically talks uh or gives out shoots out apis right so let's have a crud controller we'll be following the mvc architecture the model view controller so we have our controller we have our view which is basically json and model which is going to be firebase which is handling our database there so insider controller what are we doing so we are talking to a service which basically interacts with the firebase database for us so let's go ahead and first create that service as well so we have crud service and this service will basically contact firebase and get all the data from it or send data to it and it will be operated on using the cloud controller so let's first implement our controller so we call it rs controller and let's initialize our cloud service application our class first public service service and let's do and a simple constructor here called controller take card service and let this start service equal to cut source okay simple utilization of our cut service where we can use all the methods and such next would be to have our controllers ready right so no beams found so let's first actually create a bead for this so let's go ahead and make it a service which will indirectly make it a beam for us to use and this should go away if not then we look up it's gone now let's create our uh api endpoints to which we are going to contact firebase right so we have create update uh create retrieve update delete right card apis so let's add them uh sequentially post mapping uh we're gonna do a post to slash create and this will create our crud data it will return the correct data created or just it returns a string for now that our data has been created we can see the data being created on the dashboard and it will have a request body and this body is going to be cried in the cloud object form and this throws a bunch of stuff to close interrupted exception and also close execution exception so this is for firebase so we'll get into that in a minute and inside this uh all we need to do is return service dot create correct which we are going to make right now so this is not existing inside the service class but we will be making this in a minute let's do the same for the other endpoints so we have create we need retrieve so this is going to be get mapping not twice so we have get mapping and we do get and we also have the name so it's going to be a request parameter so instead of create we can do get crud and request body we do request param and this is going to give out accrued object so get card and this is going to be string and the name of our application basically our document id is what i would say better document id and we pass it on to this and we get our get endpoint done let's create the other endpoints so now the next thing which we have to do is create the update so create retrieve is done next is a put mapping so let's quickly write that as well so we have but mapping and this will update so let's do update again return a string because you want to see this happening inside the dashboard they crawled and again we want this little thing inside and not the document id so it will become cloud yep update is also done and last but not the least is delete so let's have our delete up and ready for it to use to delete um do wait yep everything looks good we need to delete by document id so let's do that and this is going to be it's going to be delete and we're deleting by document id okay awesome we have all the rest controller and points that we need to talk to our cloud service and next let's create all these services or the method definitions first before we move on the first is going to be public string create cut which takes a card and does a bunch of stuff right it basically returns but now it returns an empty string but it will return something later on uh next we want the same for delete and update as well so let's do update first so update and what's the problem here request is scroll right so already defined okay my delete card and this should make sure all the problems go away and yeah almost looks like it everything looks good delete takes a string so my pad yep and the last is going to be get so we do get string not in german and we are good to go so now we have our function definitions what they do pretty straightforward they don't do much for now or just simple enough to get it working and we have our crowd controller ready as well so hopefully the errors go away yep they did go away so yeah we have our controller ready uh usually i do a simple unit test to check whether a controller is running or not so the way i do that is basically have a test endpoint which does it for me so let me just show it to you right now so here i have a slash test endpoint which basically gives me that the test and input is working and gives me that application the same so let's actually you know see what's happening inside slash test so that we make sure that our application is up and running so let's run our application and hopefully we will have a simple app ready without any hassle so our server is running once it's done we can then use anything to run our application basically let's do a slash get right so it is on port 8080 so let's do it here let's do local radiati slash test right and we get the end point is working so that's good to know we have our application after running in this video we'll talk about how we can create a cut service and connect our local springboot application to firebase and do some operations on top of the data which we have stored in firebase and firestore so welcome to the third video fourth video where we finish almost finished building up our crud api using firebase and springboard and once we get done with this we will have a fully functional crud api application up and running which contacts firebase so let's get started and let's uh start implementing our current service where we actually use the firebase apis and connect to our database and do a bunch of stuff so let's uh start implementing so first we create uh our first we implement our create apis so let's start with that we have files store db file store equal to let's import the classes which we want so it says no fire store is available oh my bad i should spell it right yep great we have passed over adb faster and we use the first store client and since we have already configured our application using the service account key we don't have to worry about uh authorization anymore our app is automatically authorized so that's a great thing to need to know let's have some boilerplate code here collect shins api feature and this is going to be equal to db db faster dot collection so which collection which database are we trying to get our data from so this is basically our database inside faster so if you go inside that and if you see our collection so we have two collections here the first collection was made before the videos users and we are using cloud underscore users so let's call this cloud underscore user so inside this particular uh collection we want to create a document so we use dot document and here we create our document so let's actually create it so we do get name and we do a set and suppose so we have our crud name right here and now that we've actually set or committed to our database let's send back uh the time when we did this uh we get the result back so and we'll convert that to a string so return select change api future dot get so we don't get care update time dot to string and here we have created our uh created or implemented our create functionality so what's happening here so first we connect to our database using our credentials now it's happening behind the scenes when we initialize or we get the file store from our uh account the next thing that's happening is we are telling our using api features to actually connect to our card user and then just do a set of crud and that's basically as simple as it can get when it comes to implementing our api the next thing would be to implement get so once you have created it we need to get it so let's actually implement our get api and we'll see how that works out for us so how do we get it the first thing which we do is the same as create cards so we connect it here you can obviously abstract it out inside cut service but let's just use it uh for easier processes here so we do a document reference so basically look up inside our document and which document are we trying to reference right so we db faster dot collections again give it a collection for user underscore name and we do document and we want it by document id that's how we want our document so once we get our document reference uh the next thing we want to actually get it from the api so for that we'll be using api feature um snapshot document snapshot and this is going to be a future value which you'll get from document reference dot okay awesome so now we have a snapshot and from this snapshot we want our from the api feature we want our document snapshot so let's quickly write the code for that this goes away we have our document snapchat not a future if you want this is going to be our document and we are basically going to do what we did above so we do a future note as you can see a lot of abstractions that we you know have to deal with here add the exceptions and yep now once we get the document from firebase we need to convert it to a plain old java object so pojo right so let's do that now so let's have a crud and the next thing which we have to do is check if the document exists if document dot exists then what do we do we do and if it doesn't exist then we will return so that's or basically how it usually is so we first connect to our firebase um client and then we connect to our store client and then we get our document we get the document reference the snapshot and then the document snapshot back from the api feature we convert our document inside two objects from a pojo and then we send it back to the user so that's how we implement the grid crud api more information can definitely be found on the website in the description below and let's implement delete so once we get our data we can delete it so how do we read again pretty much the same as before but slight changes so let's talk about them so instead of uh doing a document we do a document uh id or name so id dot delete and as simple as that we are good to go and we don't have to do anything else here so this is just going to be uh right result spell that correctly and this is going to be api future right result which you get back right and the next thing which you have to send is basically uh the document id so successfully deleted space document id and yep we are good to go with delete as well so this is how we implement delete uh get and create so let's quickly see if we have something here so we have a document id as user underscore one so when we do a get uh after running this we should be able to get that back and it should be converted into a pojo giving us uh the it giving us it's giving us document id name and profession so we should get that back when we run it so let's run it and see what happens do we actually get it back or not so this is going to be user my pad again because that is what we our collection has and let's run the app and see what's happening let's open up postman as well so that we can do our queries and our api uh hit our apis so we have that here pretty interesting thing and let's wait for the server and post mine to be up and running it always says a few minutes but never actually takes a few minutes so that way uh you expect that you know you're expecting it to be minutes but they uh give it to a second then you get this dopamine hit that the postman is you know fast so that's how they get to you but anyway apart from that we have our server running we have postman setting up so it actually is taking minutes that is weird so we have some time till our server runs and until then postman hopefully should be up if not then we can obviously use uh where do we get the call right so we just use the browser for that but let's give postman some time the server is up and running so yep postman is also up and running and it did take some time to do this so what are we doing here so we're doing a get trunk right let's quickly see that inside our controller we're doing slash create okay now and here the key is going to be document id and the value is going to be what user underscore one user underscore and this when we do a get on this we should be getting back our data so document id is not present okay i get what's wrong here let me quickly fix that um so close this down go to inside and this is document id so what was the issue it give a bad request right and what is the question parameter number is not present it needs a string parameter which is not present so controller document id oh my bad i think i know what i did wrong yep i spelled it wrong so let's run this over again it should be up or because we didn't do any changes and all you have to do is just have to be id my bad and almost we are good we're done server is up to ascend and let's see what's up initially it does take some time and as you can see we have our onyx and freelancer professor here so perfect it was working and the next thing which we need to do is implement our update api and also test our application and see if it's working perfectly or not so in this video we will be implementing our update service and we will be testing our spring boot and firebase application and we'll be doing a full end-to-end test where we create update delete and retrieve from our data so first let's jump and quickly start with our update crud function so let's quickly jump into that so update so how are we updating we do file store bb files store equal to file store client dot get faster initialize our file store to an api future with write result which is basically going to be collections ap feature and this will be equal to bb5 dot collection which is going to be card user dot document dot d e l e delete great so looks good looks pretty straightforward and instead of delete all we have to do is update so for update it's basically going to be the same as create so if we do preferred users user singular and see we just do a get and set so it's override and updates whatever is missing and same as create we give out this so that it knows what it should expect add our method signatures to it and we have our update as ready as well okay so we have some discrepancy here which we need to fix so document id needs to be document id not something else everything else looks good card document id everything is looking great and okay the next thing all you have to do is install of that and all we have to do is run our application so let's run it and let's see it from the beginning where we do a create update delete and get so definitely write unit tests for application and we will talk about unit tests in the next playlist where we uh start from simply tests we move on to uh controller unit tests and then also mock uh see how marketer works and when do we actually have to mock our services and from simple unit tests we will jump on to integration tests and see how all of that works together so pretty interesting place coming future but for now let's test our crud runner so let's do a get a quickly do a get and see what's happening here take some time we did a get let's do a post and let's do a create have a body body is basically going to have document id it's going to be user 2 it's going to have name which is going to be another policy program and knowledge and the last thing is going to be professions refresh and we can call this as youtuber and yeah so let's do a post and see what's happening do a send and we get back the time and if you see firestore it has automatically updated programming knowledge document id is user 2 and this is all looking great so what's next we did a create we did uh let's do an update so let's call this youtuber plus software developer and all you have to do is make it a put update and hit send we get back this and as you can see we have youtuber software developer so put also works um create also works we did a get so let's do a get again so we do a get get no body params is going to be document id which is going to be user to hit send got nothing so it doesn't exist let's call this pro grammy knowledge it's saying and yep we have this so i think it's using the name and not the document id here so i need to check up on that but you know how it works and uh yeah the document id here is programming knowledge so uh something to change in a code for that let's quickly see what was happening so we do a get name and we okay so we're setting it by name and not by document id so that's what we did so if we do a get document id and then set our card that is where it actually uh does it properly so same thing which we did here we do the same thing for update as well so update card i should have get name it to get document id and that is how we update delete and create it so that's more or less how the card api works let's go through the beginning and see what we had implemented so we started off with our pom.xml where we employ added some dependencies like spring boot starter web test lombok and firebase which is basically how we connected to our database here we then uh went to project settings and that way we created our service account in private key in service accounts we took some boilerplate code here for the job authentication where we extract the resource and then implement it or integrate it with our application in java next was creating a firestore database we created a collection which was basically a schema in nosql called creduser and then we had some attributes added to it as document id name and profession now when we come back to the application we went to the crowdrunner and we added our boilerplate code to have the authorization done and connect to our database url initialize our firebase app and have our end points up and running so there are end points are create retrieve update delete and a test get endpoint to see if our end points are working properly or not we then use uh a crud pojo which basically is how our data looks like in this database and then we just mapped it to it so that we can see our application do stuff last was the card service where we implemented all of these and connected to our firestore firebase database application and that way we were successfully able to create update delete and have a fully fledged crud application with firebase and springboard up and ready to go so what next so we end our uh firebase tutorial here and playlist here but uh the next set of tutorials will include as i said unit testing where we unit start from what unit tests are why do we need them or how can we actually use them in interviews and also talk about how are they implemented in spring boot and also then move ahead with integration tests and explore the entire testing pyramid for our better understanding so thank you for watching i'll see you in the next video and playlist
Info
Channel: ProgrammingKnowledge
Views: 2,354
Rating: undefined out of 5
Keywords: Windows 10, How-to (Website Category), JAVA_HOME, Oracle, Javaprogramming, Javatutorial, Spring Boot, Spring Boot Tutorials, Learn Spring Boot, Getting Started with Spring Boot, Spring MVC, Spring Boot Actuator, Spring Boot Projec, Learn Spring Boot Tutorial, Spring Boot Full Course, Spring Boot Online Course, Spring Boot - Online Courses, Classes, Training, Tutorials, Free Spring Boot Tutorial, Go full stack Spring: Spring + RESTFUL API + Spring Boot, Hands On Spring Boot Course
Id: uC2wvK79mBs
Channel Id: undefined
Length: 49min 37sec (2977 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.