Spring Boot - Build a CRUD REST API with MongoDB Atlas | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techy in this tutorial you will understand how to connect your spring boot project with mongodb Atlas and also will perform one crowd operation using mongodb Atlas okay all right basically mongodb Atlas is database as a service which offers you if you are using mongodb Atlas trial version then by default you will get 5 12 Mb of stories and you will also get the feature called monitoring and alerting then you can improve the performance of your query by using performance optimization tools and all the data is in the form of end-to-end encryption and the last feature is data visualization with Atlas charts okay when I say data visualization you can visualize your data in the form of charts and graph if you are using this mongodb Atlas okay and these all the features what we discussed is available any of the mongodb atlas even though you are using the trial version or Enterprise version but in future if you want to extend above features then you can upgrade to the large cluster and the main advantages here since this product is hosted to the cloud infrastructure you can easily scale up and scale down as per your need when I say scale up and scale down it means simply you can consider the configuration to access this mongodbia class is very simple directly go to the mongodb atlas user interface then play with your configuration do the changes whatever you need as part of your requirement okay so I found this specific mongodb Atlas product is really cool because it is giving option to you to choose the cloud infrastructure what you want to use whether you want to use the AWS cloud or Azure cloud or Google Cloud it is up to you okay so isn't it cool guys so without any further delay let's quickly demonstrate this in action let's get started thank you [Music] so before we start developing the code first you need to set up your mongodb Atlas cluster so for that go to the browser and then search for mongodb at class then the first link is the official page of mongodb Atlas so just click on that once you click on it will redirect you to the sign up page you can see here this is the sign up page if you already have the account you can click on this sign in or else if you want you can directly sign up using the Google okay so for Simplicity let me play with the sign of form so I'll give the first name as basant name something like last name hota company I will give Java Tech is my company name gmail.com then password you can set anything okay I'll just set something okay now agree the term and service create your Atlas account next immediately you will get a verification email to the email ID which you used as part of the registration then just go to that email ID and you need to verify verify your email address okay so let's put it to load you can see here I received this email ID so I will click on verify email then you can see here the email successfully verified click on the continue and it will redirect to this Atlas home page here you can see the option right you have option to set up your information so the first option what is your goal today so I just want to learn the mongodb you can choose appropriate to your requirement so for now I will choose the learn mongodb what type of application are you building I will choose micro Services okay what is your preferred language for now I have using Java now click on finish and if You observe here there is something called serverless mongodb Atlas dedicated mongodb air class and shared mongodb Atlas okay so these two are chargeable you need to give your debit card or credit card to activate it and but this is the free one okay so I am going with the free approach which is the third mongodb Atlas so just click on this create now you can see option here create a shared cluster so I am not going to use the serverless and dedicated because I want to trial the free mongodb Atlas which is the shared so I click on this next you can see here the option you found here choose your cloud provider and regime whether you want to use the AWS or Google cloud or Azure it is up to you so just click one of the cloud provider for now I will choose the AWS then you will have the option to choose the region but I will go with the default which is AWS Mumbai AP South so I will ignore it so this is the cluster tier so this is the default configuration so leave it we don't want to change anything then additional setting nothing I want to do for now this is again the default thing then next I'll change my cluster name okay this is also optional but let's keep a valid cluster name Java techy Okay now click on the create cluster so you need to validate this you know to choose this let me add it fine so click on next I don't know okay just choose this fine then it will create the cluster now it is asking about your security quick start how do you like to authenticate your connection I want to authenticate using username and password so give the username so I'll give the username as admin and password also will keep as admin okay you can give your username and password you can add any number of user to access this mongodb Atlas but for now let me create one user with name admin and password admin click on create user you can see here this is what your user now where would you like to connect from so my local environment then our entries to your IP access list give my current IP here finish on clothes go to the database it will take few second to create your DV instance you can see here this is your database deployment and this would make cluster name fine and since we are using the trial version it is asking me to upgrade but we'll go with the trial only and if you click on this connect this is where you will get your data source information okay now you will choose this connect your application this is what your data source URI or URL whatever you can consider using this your spring boot application can connect to the mongodb atlas which is hosted to the AWS Cloud infrastructure okay so you need to copy this for your future reference so let me copy this this is my URI okay or URL database URL and I want to create the database and collection through the application so we'll add as part of the document okay so we are done with our database setup in mongodb at class now just go to your IntelliJ IDEA then let's quickly create a new spring boot project click on file new project so click on next give the group edicom.javatiki then I will give that effect ID let's say spring boot Atlas fine I will choose the jediate weight and I'll keep the packages com.java techy now click on next then let's add the required dependency I will add long book I will add web dependency spring wave because we are going to perform the crowd operation and I want to expose the end point so that I can show you the crowd how it is working right then next I will use the dependency right spring data that's it click on the next click on the Finish so project imported successfully and there is a spelling mistake so let me correct it fine okay now what I need to do I will create the couple of packets so I will create a package called model or entity or document whatever is compatible for you so you can create that package let me name it model okay then next let me create a package called service then you can create a package called controller fine that's it right let me create a model new Java class I'll give the model name let's say task fine so let me Zoom this so I will add the couple of field here private string task ID private string description of that task next I can add something like private int severity of that task okay next you can add something like private string to whom this task is assigned assignee then you can add the story point of that task okay private string or in right int story point of the task so it is up to you you can play with some different element or document like employee order something like that but I use the task as a document since I am using the which is a nosql DB right I need to store this as a document so first I need to annotate here at the red document then I need to give my collection name it is up to you if you don't give any collection name name by default it will take your collection name as your class name so it's good practice to keep your collection name as tax okay since I use the lambo I can use the d88 data annotation all argument Constructor I want no argument Constructor that's it okay so since I need to Define one as a primary key I will choose this task ID as a primary key I just need to annotate your at the rate ID and make sure this ID should be input from org.spring framework dot data not from the Javas persistence okay fine so I am done with my entity or you can consider as a document with five field task ID description severity assign and story point now next let me create a repository so let me create a new package called Repository then I'll create a interface here let's say task Repository this should be interface now this interface I need to extends from repository okay so you can use the jpa but since this is the nosql we need to go with the repository or else you can extend from the crowd Repository so here the first argument should be your document or entity you can say task then next data type of the primary key which is string okay this is my repository so you are good with entity and repository now let me create the service class Java class let me name it task service let me Zoom this so I need to annotate this with annotation at the rate service then I need to inject repository inside this service right so I will use private task Repository and I will inject using Auto add next I will write the crowd operation when I say crowd operation it will be create read from the DV update in the DV and delete from the DV that is what the crown mean right create read update and delete so we'll perform these four operations so first let's go with the create I mean I want to save some task document to the mongodb atlas so I'll write a method public who will return me the task object which I am going to save okay save task or add task something like that then give the task object as a argument then simply since I injected the repo I can directly call repository dot save and give the entity when I say entity in nosql it will be the document right so just give that object and before save that since If You observe in the task I created this as a task ID but in the ID can't be Auto generated directly okay you need to write some additional logic but for now let me set some value for the task ID so what I will do I will do something like task dot set task ID and I will use something like uuid dot random uid dot convert it to the string then I will split it and I will get the first index some random alphanumeric value I will get okay and then I am going to save it once it will save it will return me the task object so I will simply return that similarly let's try the next operation which is read right so we want to rate the object or document from the mongodb atlas so what I will do I will write a method who will return me the list of task object from the mongodb so I'll write task then I just need to write a method something like fine all tasks okay then I can directly use the repository dot find all method okay so if I call the find all method it will give me all the task object or all the task document from the mongodb so I'll simply return that okay next you can also search a task document by any of the field okay so this is where we fetch all the task document from the mongodb now you can write a single method to fetch a single task document from mongodb by any fee when I say any field you can go to the task document and you can either Fetch with the task ID severity assign or story point it is up to you okay so first let's try this task ID then we'll try with the severity or any other field so let me go to the service then I will write a method here public who will return me the single task object so task by ID right I'll write get task by task ID and I need to give this task ID then the method is very simple in Repository you can simply return Repository dot find by is the prefix and then next based on which field you want to do the operation or you want to do the filter you need to give that field okay for now you want to fetch using the ID so I can use the find by ID and I need to pass this task ID it will return me the optional so I need to get it okay fine now let's say you want to search by severity okay so I will show you how you can write the method it is not specific to the mongodb any of the spring data implementation if you are using this is how you can design your method rather than writing the query you can design your method okay so let's say I'll write a method who will return me the task document and I want to get the task by severity okay so there may be a multiple task for the for a specific severity so I will return the list of tasks fine now here I need to give severity which is int now here you need to understand how we can write the method so that it will convert to the query okay when I say it will convert to the query you no need to write the any query if you are using any implementation of spring data so how I can do that I will write return Repository as I mentioned before find by should be your prefix then based on which field you want to do the filter this is what the field right just pass that name variable name makes your you need to give the correct value what you define in your document or entity now once you give that make sure to make it camel case so find by is the prefix and then based on which field you want to perform the operation you need to give that free so let's say you want to search with multiple field then give earn and then what is your next field you want to search let's say story point right you need to give like this make sure to make the first car as a capital then you can create this method and you can here also you can pass the story point but let's make it simple we'll try with one single field which is severity now I need to pass the severity here and this method is not exist in our repo right we need to create create this method so just create this method inside your repo that's it now spring data is smart enough to understand okay this guy want to find the object based on the return type and he want to perform the filter based on this particular field and this particular field is exist in this entity right so that is how he can able to execute this method as a select star from this table where severity equal to this okay now if you go and check the service we write our own method to use this prefix find by then based on the field now we might have a question hey can't I write the query for this particular scenario so yeah you can write the query so let me write another method let's say I'll I will write the same operation based on severity or let's try the different okay public who will determine the list of task okay get the task by what else field we have let's say assign okay so go to the service get asked by assigning then I'll give the assign name now next what you can do I could write the method the way I write here find by severity I can write a method find by assignee right but I want to write the query so what I can do I'll write something like this or I will directly write from the repo then I'll come back to the service so method will return me the list of tasks OK and you can give any name method here okay method name you can specify anything because you are not following the syntax of spring data we are going to follow or we are going to write our own query okay so I'll write something like get tax by assignee you can give this field now you can write the query here so make sure to annotate at the red query and then based on which field you want to filter just give that field with the index parameter so you can write something like this the field name is assignee right and this is my first parameter so you can format it I will write like this okay now let's say if you want to pass the second argument then just add a comma give the field name then specify the index parameter okay and if you want if you want to add further then specify the index parameter okay that's fine so we want to try with the single parameter fine so now go to the task service simply call this method return Repository dot get task by assigning pass this assignee as a argument fine so we have written the we tried the create read now let's write the update method so let me write a method public after update I am expecting to get the task object with the updated value so I will return the task now I will just Define update task okay now I'll just give the task object which I want to update task I'll better I'll keep the name as task request fine now to update anything first we need to get that value from the DV then we can apply the updated value right so I will write the step get the existing document from DB populate new value from request to existing object or you can type entity or you can type document okay so first let me get the existing object from the DB by task ID because task ID I'm going to pass as part of the request you can keep a separate parameter as well let's say string task ID but since I am giving the object as part of argument I will keep the input here okay so I don't want to keep another field so what I'll do I will simply call repo dot find by ID give the task ID I'll get it from the request dot get task ID it will return me the task object from the DV which is already exist with this ID so I will capture that so I'll name it existing task okay now whatever the new value I will pass as part of this request those value I will update in this object which is there in my DB okay so let's say existing task dot set ID I don't want to change because anyway if you want to perform any update operation you should not touch your ID because this is the unique field based on this field we want to perform the update operation okay so I will just add other field get it from request task request dot get description similarly existing task dot set severity get it from the request task request dot get severity existing task dot set what is that story pointer okay assignee get it from the request now the last field existing task dot set story point get it from the request fine so here I have only four field so I am directly getting from the request and setting to the DB but if you have multiple field better to use some wrapper mechanism or you can use the map struct or any other object map or anything you can use there okay so this is just for object or four attribute I want to set so I did it manually now there is no update method available in the crowd repository or or jpa or you can consider as part of the spring data there is no update operation directly so you need to call the save operation so what we did we get the existing object from the DB and we update that existing object which is there in my DB with the new value then I will save that new value okay return repository dot save give the existing object which we modified with the new value okay that's it about the update next if you will observe here we are done with the update next we will write the delete so for delete I'll write a method public will return me some string let's say delete task then I'll give the some some field okay I'll give int sorry string task ID fine then simply I will call Repository dot delete by ID and give the ID If You observe the return type of this method is void you can see here right so I will return some string here return task deleted from dashboard or something like that okay I will give the task ID here fine so you are done with the service we tried the crowd scenario create and read with multiple scenario then update and delete so we write the service now let's jump to the controller class and let's create it okay so I will just create a class now Java class let's say task controller then let me Zoom this I need to annotate here at the rate rest controller then I need to define the root URL I'll Define something like this then I will call each and every method from the service so I will just inject it because from the controller I want to use the service so I need to inject that otherwise I cannot access it I will just simply name it service inject using auto air fine then next write the endpoint so I'll write for create public will return me the task create task I need to give the task as a input from the postman so I will give task as your argument then I need to annotate here at the rate request body because I will pass this value from the postman so that that Json will be convert to this object using Junction databind then simply since I have the task object from the postman I can call return service dot R task and give the task object okay since here I am going to give the request and this is what I want to create the resource so I need to annotate here at the red post mapping okay and if you want you can also use the response status let's say create it okay next I'll write okay let's check the service we write for add task now let me write for find all tasks okay better I'll copy the syntax go to the controller copy this method name I'll change the method name gate tax I'll simply call the service method right service dot find all tasks okay so here I don't have any input simply I want to fetch it from the DB I will annotate here get mapping now let's go to the service the next method we want to get the task by ID so I'll simply copy this go to the task controller I will paste it I'll change the method name get task because this will give me the single task object so rather than call the repo from controller I will call the service service dot get asked by ID get asked by task ID and I'll give the task ID okay so again here I am giving one single parameter I will annotate your gate mapping and this parameter I want to pass as part of the request URL so I will use the path variable okay and I need to append that as part of the URL so I'll Define like this because I want to append the task ID as part of the request URL fine next let's go to the second method which is get task by severity so simply I will copy this method then go to the controller just paste it okay I will just keep the method name get task with or find task okay find task using severity and I will give this severity now here I'll call the service method service get tasked by this okay just pass the severity here also what I will do I will simply use the gate mapping and I also use the path variable okay so I'll just unite it here at the red path variable and this field I will pass as a severity I will just append this as a URL now you need to fire task slash Verity then give the severity it will give you the list of tasks okay now let's check the next method so the next method get tasked by assigning this is where we write our custom query okay so simply I will copy this method you can write it to save our time I am just doing the copy paste okay better let me try for one okay public will return me the list of task get task by severity not assign it right give the assignee value as a input then simply I will call return service dot get asked by not this one service get asked by assigning okay and give the assign a value now here this again I need to pass as part of the request variable or request URL so I will use the path variable annotation then this is also get mapping and you need to Define your url let's say this is assigning so just change the url assignee and give this value dynamically okay so we are done with the get and create now we'll write for the update and delete so I'll write a method public will update the task and return it to me so I will write a method update task or modify task okay then give the task as a request and since this is again I am giving the object I need to annotate here at the rate request body and since I want to update the resource in my DB I need to annotate here put mapping okay this is what the HTTP standard if you want to create use the post mapping if you want to update use the port mapping and then that's it I don't want to give any URL okay because this annotation is different input mapping and in post mapping here so there is no confusion you can access with this with the same URL okay because the HTTP method is different so I will simply call return service dot update task and give the task now next we'll write for the delete public it will return the string right delete task give the ID which is string task ID then simply I will call return service dot delete task give the task ID since I want to delete the resource I need to use the HTTP method here delete mapping okay and this I want to pass as part of the request URL I will use path variable and you need to give the URL here fine okay so if You observe here there is one interesting part here I use other delete mapping and I am giving this URL But If You observe here I am writing on gate mapping and I am giving the same URL so there is no confusion even you are giving the same URL the HTTP method is different here it is get here it is delete so there is no confusion from the controller he will understand based on your HTTP method okay so we are good so we write our controller service entity and repo but if you remember we created the account in mongodb Atlas which is there in cloud and we need to get the data source information right already I believe we copied it if not you can simply connect your application and copy this URL which is your mongodb database URL so you need to configure that otherwise how your application will connect to this data source right so what you can do go to the project and then simply create one yml file even you can use the application.properties but let me use the yml file application Dot yml okay then specify the data source URI and name of your database okay so I will use spring it's not Auto suggesting okay I will write it spring data then URI okay and you need to give this URI which we copied right or just we copied right that I can give here now make sure to change the this is what your username right and this is what your cluster name makes you to give your password for I said admin okay I will create the user I give the username as admin and password as admin so I am giving the same but if you keep your own username and password make sure to give that otherwise this is what the mongodb atlas URL which is hosted in AWS Cloud okay so you need to give it properly fine next simply just give the database name okay database which is Task you can give any name since my document or entity I created with task so it's good to keep the same name but you can give any name here okay that is fine we specify the URI where my mongodb is located this is what will work as a data source for your and this is what your name of your database fine now we are good with the classes and methods so we'll simply run our application just go to the main class just run it it will take few second it seems there is some exception so what is that okay okay let me check here the connect then connect to your application zero ev8 right so let's verify in the application.2 ml 0 v e eight and username admin admin okay we did a mistake here this should be mongodb not okay spring data mongodb then give the URI and database so I believe we are good let me rerun this so you can observe here right started on port 8080 and it connected to this okay there are some connection related stuff printed over the console um let's not focus on that now let's verify our endpoint okay so go to the controller class the URL is last task and first is create a task and give the task object so task ID will be Auto generated since we are using uuid in the service these are the four field you need to pass description severity assignee and story point so just go to the postman this is what my URL t a s k s and these are the field I need to give right description severity assignee and story point so let me send the request we are getting the response now let me add some different thing here okay for description let's say logging stuff and severity let's say three assign is story point is one send the request fine now let me add something like some different task okay what I can say let me add something like um add Crone expression to refresh okay let's say severity is 3 and I'll change the assign it to let's say someone else um anything Asic okay story point is 2. now let me send the request it is added right now if I'll go and verify in my database let me close this let me Zoom this so there is option called browse your collection okay so just click on this browse collection can you see here this is what the collection name we created right and this is what my database name and we are getting here task is my database this is what my collection and we added the three entry three object or three document we are able to see here right implemented security then severity whatever the field we added we can see here now let's simply do all the gate call so we are we are successfully did this right and since we added the created let me check what are the status you are getting we are getting 201 created correct now let's check the all the tasks from DV so the endpoint is same slash task right I will simply copy this directly I will search in my browser so that I can so in the correct format can you see here we are getting these three object so severity three right and one so let's check the next endpoint we are able to fetch all the endpoint next endpoint to get the task by task ID so let me copy one of the task ID then we'll filter that let's say this okay let me copy this now simply add that as part of URL we are able to see the value here this is the task ID this is where the description severity assigning and story point now let's check the next endpoint get the task by severity we'll get the list of tasks If You observe we have the two tasks added let me verify let me go to the cloud severe it is one here these two tasks having the same severity okay so I should get the list of tasks these two now let me check with that let's say severity let me verify the URL severity then severity count right give the URL severity then I will give three I should get two object I am able to let me Zoom this can you see here we are getting two object here both are having the same severity this is what we write our custom method right get task by severity from service and in repo we created our own method by following the spring data prefix find by is the prefix and based on the free fine next this is what we write our own query to fetch list of tasks by assigning so let's verify in the DV so there is two tasks assigned to this guy so I will filter with this assignee so what I will do I'll copy this name then simply go to the URL remove this add the URL give the name it seems there is some error so let's check the console Json reader was expecting a name but found end of the file and it is there on the service okay if I'll check the repo assign oh okay we did a mistake we missed one um controverses here we need to close it okay so while editing it like when I explained it the multi parameter so I wrongly removed it fine then we are good let me read on this so it started now go to the Chrome browser and Let me refresh this can you see here with the name assign this guy we are having two document and we are getting it and this particular method is executed using the query which we written in our repo class okay so we are good with create an all type of gate API or retrieving the document from the DV now let's try out the update okay which is put mapping so I need to give the task request I need to update something okay let's say go to the DV we'll update something okay instead of this logging stuff we'll add something like okay we will change the description and then what else we can change let's change this guy okay so first let me get this object I'll go to the browser and simply I will call this I'll get this object then I will simply copy this because I want to update this description so just go to the Postman and then I will just change it so make sure you need to give the valid ID and this ID should be non-editable user should not have permission to update the ID because this is what the unique field based on that field we are getting the object from the DV okay so I'll just add the or I'll just change the description enhance logging framework okay and let it be severity and assign it let it be or I will change the story point to uh let's say three okay then I need to change it to the put right I want to update now send the request it's getting update and we are getting the response now let's verify in the DV go to the DV simply I need to refresh it I believe I'll just click on different Tab and I will go back to my database go to the database browse collection since I zoom it I didn't search the refresh tab but there should be some refresh tab okay I didn't find here but it's fine can you see here this is what we changed right description we changed and story point we increased to the three initial it was one so update is also working as expected now the last one is the delete fine so what I will do I will just delete one object from the DV let's say I want to delete this first object copy the ID then go to the postman task then give the ID which you want to delete change the method type to delete this body is not required simply just delete it this task deleted from dashboard now if we'll just call the API to fetch all the object all the document from the DB you can see only 2 is present now okay first object or first document we simply deleted now the count is to same if you can verify in the DV Let me refresh this I guess so if you will scroll down there is only two document present and let me minimize this so that I can check other features so you can see here visualize your data you can visualize in different form um or in the form of charts so you can explore more if you want to play with this particular mongodb Atlas just create a cluster and play with its user interface here you can see something called real time metrics somewhere I search the graph of my data but I'm not getting it now there is something the request you triggered to this DV view monitoring and alerting everything you can do here okay this is what the monitoring tool to verify the memory which is and all the stuff specific to the mongodb atlas so this is just simple okay rather than install mongodb on my local machine I use the mongodb cloud version which is the mongodb at class okay which is hosted to the AWS Cloud if you remember I'll create the Orwell create the cluster I choose the cloud provider as AWS if you have account on Azure it's fine if you don't have account also it's fine if you want to choose Azure or Google Cloud you can play with them okay this is what the user interface there are so many features you can also set the triggers and you can also set the network accessibility you can make it private public you can make it said everything you can do okay so I would strongly suggest you to create one cluster in mongodb Atlas then play with this user interface you'll get lot of things to learn okay and this is the simple application we designed to perform a crowd operation create read update and delete using spring Boot and mongodb at class if you have any doubt do let me know in a comment section that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 47,522
Rating: undefined out of 5
Keywords: Spring Boot Mongo DB, spring boot mongo atlas, spring data mongo, javatechie, spring boot, mongo db
Id: qVNOw9TWwxo
Channel Id: undefined
Length: 50min 17sec (3017 seconds)
Published: Fri Oct 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.