Java REST API with Spring Boot Tutorial | REST API CRUD Implementation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
get mapping create a new cloud by  spring initializer to delete quest   hello everyone very warm welcome to think  constructing i am isha today i have got an   extremely important session for you that is  session on rest api with spring booth in this   session we will start with some key concepts of  rest api followed by some important concepts of   spring boot then we will start building the spring  boot basic application and will convert that   application to rest api for a particular scenario  so i'm sure this session is going to be very   very useful for you so please follow along  and watch the complete session so that you   don't miss upon anything and i also would like to  mention that this is one area which is very widely   used in the software industry so for all the java  programmers architects or leaders this session is   going to be very helpful all right so let us start  with the key concept discussions of rest api okay   so let's have a look on some important points of  rest api which you should know before starting   to build the application all right so rest apis  are also called as restful apis these terms are   used interchangeably okay full pump for the rest  api is it's a representational state transfer and   point to be noted here it's not a protocol or any  standard it's rather the architectural constraints   okay and apis are application programming  interface so the combination of rest and api   is the rest api so for implementing rest apis the  api developer should implement or should follow   these set of architectural constraints which are  provided by rest and then only that particular api   will be called as rest api all right so some more  key points for rest apis which you should know   rest apis work over http protocol so that means  whatever operations are supported by http should   be supported by rest apis for example get request  okay and it delivers information using any of the   following format the most popular format is json  format other than that it can deliver information   using text html xml javascript and much more okay  so i'll show you demonstration also wherein you   can you know see and relate more stuffs now we'll  have a look on some key points for springboot   which you should know before starting to build  the application so here are some key points   related to spring boot why spring boot is  so popular so famous because with the help   of spring boot you can easily create a standalone  production grade spring based application okay so   to create a spring-based application has become  much more easier with the help of a spring boot   it comes already with the embedded tomcat jetty  and one more server which is not mentioned here   that is under two all right it provides starter  dependencies already it automatically configures   spring and third party library so no need for  any developer to go ahead and you know do all the   basic configuration in so much of detail rather  it comes already with the starter packages starter   dependencies starter libraries and embedded  servers so much many more things are already   provided so to begin with developers should know  how to write the core logic and the core concepts   of springboot that much will be sufficient okay so  it makes developer life easy once developer really   understands all these structure architecture of a  springboard in detail okay and it provides future   production ready features such as health checks  matrix etc so these kind of services are very   very important when any application is deployed  in the production so any application whether it   is working fine or not all those health check  metrics are really important and those things   are already provided by springboard you just  need to you know configure plug and play those   things you need to build up those services  integrate deploy and things will be done   no code generation no xml configuration so all  those kind of stuffs are not at all needed here so   thanks to spring.io website i have taken couple of  points from there and these are really very good   important to know and essential points any spring  boot developer should know okay now i will show   you certain key annotations which i will be using  while building the spring boot application now   okay so i'll just uh tell you about all these  annotations so first one the foremost important   annotation for a spring boot is spring boot  application any spring boot application starter   point will have this annotation and this  annotation is a combination of enable auto   configuration component scan and configuration so  this is the combination of these three annotations   okay then we have rest controller annotation which  will indicate the spring route application that   this particular class is the controller class and  all the related rest stuffs will go in here okay   then we have request mapping to indicate the  context path get mapping post mapping boot   mapping and delete mapping all these annotations  will be using in our demonstration so watch this   session till the end so that you don't mess up  on any things i have showed you all the initials   which we will be using in our demonstration now i  will show you which service we are going to make   so we will be making cloud vendor api with  the help of uh springboard and we will make   rest api for this okay so this will be our model  class wherein cloud vendor model class we will   be building first and it will have properties as  vendor id vendor name vendor address and vendor   phone number okay so four properties we will  be building for this demonstration and what my   cloud vendor service will look like it will be  a cloud vendor information service that means   this service whenever any will anyone will  call this service via get in order to get   any cloud vendor information it should be able to  give or post will also be supported in order to   create a new cloud vendor information  put will be supported in order to update   any cloud vendor information and delete will be  supported just in case the created cloud vendor   information is not needed so in this demonstration  i will be creating a cloud vendor information   service which will be supporting all these  four operations get post put and delete there will not be any db interaction i'll be  creating rather for demonstration purposes i will   be creating some hard-coded values to show the  functionality of all these four operations so that   means using a springboard how can you create crud  operations of rest api and how you can interact   so this is the agenda for the demonstration which  i'll start now okay so first i'll show you how you   can create a basic spring boot application so i'll  be showing it via spring initializer all right so what am i doing i'm building a basic  spring application first with the help of   spring initializer all right there are other ways  also like you can directly add maven dependency   uh to your application and convert it to the  springboot application okay so here is a spring   initializer here you can see whether you want  to use maven as a build tool or grader so like   in my case i will proceed with the maven project  language java and in springboot whichever version   you would like to have okay so let's say i'll  go ahead with the default version that is 2.7.1   okay you can give project metadata like in my case  i will give group id as com dot think constructive okay and in artifact id whichever you would  want to say so let's say i want to give rest   demo all right description and package  name will be auto taken description if   you want to modify you can go ahead and do that  i would like to have packaging as jar and java   different versions it is providing me so let's say  if you have java 8 in your system you can proceed   with that or if you want to have 11 17 or 18  whichever you can have that let's say in my case   i'll proceed with java 18 okay and since i target  or i want to build rest api using a spring so i   should add the dependency here for building the  rest api all right because spring boot by default   will not come with the rest api dependency  i need to add that and where will i get that   restful api dependencies i will be getting inside  spring web so i should be adding spring web   dependency all right so i have selected that and  now let's say generate it will generate a basic   springboard project for me i've got you can notice  i've got rest demo zip here okay i can just save   this zip and extract in any java editor so like in  my case i use intellij idea editor you can use any   java editor of your choice it will be totally  supported all right so here is the intellij id   editor and the project dress demo demo which we  have just now created with the help of a spring   initializer i have extracted and opened here okay  so the project structure what we are getting here   we are getting src main java typical structural  folder with one java class which is rest demo   application all right so rest demo application  let us just open and have a look at it   this is my res demo application and this is  the main method and inside this you can see   a spring application dot run method is called  okay and here if you notice you are getting   at springboard application annotation which we  know is now the combination of three annotations   we have got all the essential stuffs of  basic springboot application here let us   now quickly check the pom.xml also what  dependencies are we getting here so what   am i getting i am getting spring boot starter  parent so which is my spring boot dependency   and what what as we have added we have added  java version as java 18 we can see it now here   and we have added spring boot web dependency  because we want to create rest apis using   spring boot so all the dependencies are also  in place okay now first let us execute and see   the results what are we getting here and then  we'll start creating rest apis all right so run what am i getting here my spring boot application  has started and let's now spend a minute here   on the logs what you can see here tomcat servers  started on port 88d http with context path   blank okay and next what are we getting started  address demo application in these many minutes so   that means my application is successfully started  and the server which is being used is tomcat   and the default port of tomcat is 8080 which  is being used here okay so my springboot   application is successfully up and running  if you notice we didn't add any tomcat here   all right it is automatically picked up because  this is a springboard and a springboard brings   tomcat embedded in itself all right now we should  check whether my application is actually working   on this port 8080 or not how do we do that  i'll just switch to the browser window again   and here i'll just say local host enter so it in it is not telling me that there  is no service running or nothing on 80 rather it   is taking me to some error page because we haven't  coded anything all right so by default application   is redirected to error because it it doesn't have  anything to serve to us all right now let's start   making the rest apis okay first what are we  supposed to create the model which is my cloud   vendor okay i'll just show that slide again see  cloud vendor api we are supposed to make so first   we should have cloud vendor with these properties  how should we have that we'll be creating a model   class for that and then we will be creating cloud  vendor information service all right here i'll be   adding a model class inside model folder and i'll  just give the name to this is cloud vendor okay so i've got a class here called cloud vendor  what i'll do i'll create the properties which   we have just now discussed vendor phone number all  right these four attributes we need and we should   now generate the constructor and getter setter for  this okay so i can directly say generate from here   and first let me just generate the constructor  with all the four parameters all right let me fix the indentation yeah okay we should have we should also have  an empty constructor so i'll just say generate constructors none okay so i have  got an empty constructor in place   i have got constructor with arguments in place  and what else now i want i would want to have   the getter end setter for all these four  all right so i'll just say getter and setter   i'll select all because i want getter in  setter for all the four attributes there you go   okay let me just fix the  positioning of all these stuffs all right so i've got my cloud vendor  model class with all four attributes   it's related constructors and all the getter  and setter methods okay so now the next step is   i need to create a rest api we should be serving  all four crud operations for this cloud vendor   okay so let me go ahead and add a controller  class to that how do i do that i'll just say new   java class i want to create it inside controller  because to have a good package structure folder   structure is always very very helpful all  right so you just say controller dot cloud   api service okay i'll create this class and what  do i do now first thing how spring will know that   this is the controller class i should annotate  it with rest controller all right also i want   to have a context path as slash cloud vendor okay  so what do i do i need to put another annotation called request mapping and inside  this i should give cloud value okay now first method we will be creating is for  getting the value so get request i will be   creating okay so let's say i will create cloud  vendor because that is a model class so i want the   i want to return the complete object and  what do i do i will just say get cloud vendor   details all right and what i  want to pass inside this is a string vendor id okay because  on the basis of certain vendor id   i should get the complete vendor details all right   oh there is a typo i'll just fix it so it's cloud  vendor so what do i do now i should return because   i said we will be building an application with  some hard-coded values so i'll just say return new cloud vendor and it should have some values  so let's say um vendor id i'll create the vendor   id as [Music] c1 okay the next parameter was  vendor name so i'll just say vendor name as cv and just give vendor name as let's say   vendor one all right then vendor address let's  say address 1 and phone number and just say vendor phone number xxx okay so what are we returning i'll just shift  it to next line so it is more readable   okay so we are returning cloud vendor object with  these values okay so this is the get method we   have created how spring springboard will know  that this is the specifically get mapping or   get api we should put an annotation called get  mapping here all right and in this get mapping what value we are expecting  we are expecting vendor id okay so we have just now created a cloud  api service i think cloud vendor api service   is a better name because that is more explanatory  so let me just refactor it i'll just say refactor   and rename all right i'll put cloud vendor api service yeah this  looks better all right so cloud   vendor api service we have created with  one method as get cloud vendor details   which should be serving the get request because  we have put an annotation called get mapping   mapping here okay so let us bring up the  spring boot application i'll just say run my application is now up and running again  at port 8080 and now let's go ahead and see   when we say localhost 8080 slash cloud  vendor slash sum id what do we get   i switch back to browser window so here is  my browser window i'll just put local host 8080 slash context path slash 7 okay let us see  what do we get so we have got we have got the   details whatever we have returned from our get  cloud vendor details method all right so my get   method get api is created my service is listening  to porti and with the help of this context path it   is able to return me the desired value okay  i'll now show you you how can you test this   with the help of postman also okay so there is a  tool called postman which is very very important   in the api or web services world for testing any  rest api postman tool is very important to know   what you need to do all you need to just say plus  create new collection okay i'm just saying rest   demo one okay so i've created a collection and  inside this i can just say add request okay request one and here i can say the way we have  given in our browser window all right so i'll   just give the same url here method is selected  as get and will send the request whatever got   i have received the same response which we have  got in the browser window okay why am i showing   you this tool because i will further going  ahead and showing you the post put and delete   which cannot be tested from the browser window  for that you should be using this postman tool   there are many other tools but this is the most  popular and simple to use tool all right so i'll   just terminate the server now i'll go ahead and  create a post mapping also which will help me   to create cloud vendor details so next i'll be  creating the create cloud vendor details for   that i have just taken a cloud vendor object and  here let us return this cloud vendor object itself here let us return this cloud vendor object okay   why am i doing this because now i will be creating  a create cloud vendor details will be filling   in details in this object itself and the same  value should be returned with the help of this   get cloud vendor details okay so what do i  do i'll just create a public method we should   return a string value whether it is successful  or not all right and i will just say create cloud vendor details okay and what should it accept  it should accept the cloud vendor object why because because the cloud vendor  object we will be receiving in the post request   as a request body itself all right so for   that matter i should be putting a  request body annotation also here okay since it is a create request so i  should put post mapping annotation here   i'll fill that it is whatever in the in this  cloud vendor object i am getting where do i   feel i will fill it in this cloud vendor object  all right so i'll just say this dot cloud vendor   is equal to cloud vendor all right and what do  i need to return i will return a success message okay so your post mapping is created let  us go ahead start the server and test it so my server is started i'll go to postman so  here i'll change the method to post url will   be like this okay and in the body i will just  say raw data and here i will add the object which   i want to send in the request so let's  say i want to create a vendor names   vendor ids c2 vendor name as vendor  2 vendor address as address 2 and   vendor phone number let's say xxx i will just send  this request and let's see what response do we get we have got unsupported media type  why do we get this response because by   default spring boot rest api support json because  they will produce json and they will consume json   all right and by mistake it was selected as text  here so let us change it to json and again try   sending the request cool so we have got http 200  okay here sorry we have got http 200 ok code here   fine and the same message cloud vendor created  successfully all right now let us go ahead and   test its get okay so let me now create a separate  request for get that will be better to handle okay see two let me send the request now   what have we got this time vendor ideas c2  vendor name vendor address and phone number   which we have just now created all right so this  completes one cycle creation of the vendor details   and getting the vendor details all right now  let us switch back to the editor and create   put api that means if i want to update this vendor  detail how do i do that okay and i'll now create   the vendor update api okay and just copy  this thing and we'll do the required changes   so it was a create cloud vendor for  creation if i want to update i'll just say update cloud vendor details  okay and we'll annotate it with   put because put in http is for updation so  i'll put the annotation as put mapping okay   and whatever cloud vendor details are supposed to  be updated should be passed here as request body   and those details will be updated here so this  code would remain same i will just change the   message here cloud vendor updated successfully  okay now we will again bring up the server   test this put mapping will create will update  and we'll extract the details all right [Music] so my application is up and running now i will go  back to postman here currently if i will try to   get anything it will return me blank because we  haven't yet created anything all right so we'll   create with the help of this post cloud vendor  let us keep this request body same c2 vendor to   address two phone number xxx will again create  the vendor cloud vendor created successfully   now we will try to extract the information we have  received it now let us change this vendor name   from vendor to to vendor to update it let's say  okay how do we do that i'll just add a new request   and we'll use the method as put okay let me  just take the url because url would remain same okay and here in the body with the help  of raw then text as json i will keep the   request body here so let me just copy this   paste it here and what do i want i want to change  the vendor name from vendor to to vendor to update   it okay so that i get i clearly see the difference  and now i will send the request whatever i got i   have got cloud vendor updated successfully http  status 200 okay fine this is what we wanted   now let us try to query or get the request  for this c2 let's see what do we get so this is a get request c2 vendor earlier  we were getting vendor 2 in vendor name   now ideally we should get vendor 2 updated let  me just say send see whatever i got vendor to   update it everything else remains same because  we did not change anything else all right so   i hope this part is clear let us now move to the  last part of this session that is creating delete   apis all right so i'll go back to the editor  now and now i will create the deletion request   for this cloud vendor details how do i do that  i'll just copy paste and do the required changes   okay so i want to delete so i will make my method  name as let's say delete cloud vendor details   instead of put mapping i will say delete  mapping okay what do i want to delete i   just need to pass the vendor id here  normally what happens basis on the id   you you should be deleting or taking these  kind of actions okay so i'll just say string   vendor id okay i'll just pass the vendor id in the  query parameter like this i will just say vendor   id okay so whatever vendor id i'll  be giving here will be captured here   okay so now for deleting this in this example  since it's a hard coded values i will just   set this this dot cloud vendor to null and will  change this response message which is cloud vendor   deleted successfully this time all right so now  what i'll do i will bring up the application   create the vendor detail we will get and check  whether it is created and then we will delete   and then we will get and check whether it is  deleted or not all right so i hope this entire   cycle is clear so you can see my application is  up and running at port 8080 i'll open postman   tool and here now this time with vendor id c3  vendor name vendor 3 vendor address has address   3 and phone number xxx okay or let us change it to  y by y just to see the differences so i will first   create vendor with this details send the request  i have got 200 ok with the created successful   message ok now let us query this whether we  are able to get this vendor detail or not so i've got vendor c3 with all the   respected details all right so now my next  thing is i want to delete this in the vendor   detail i will add another request i  will just say method as delete okay i will copy and paste the same url here  and we'll give query parameter as c3   all right and we'll send the  request let's see what happens   cloud vendor deleted successfully with http status  code okay i have got all right now let us go back   and query this c3 let's see whether we now get  this information no or not all right so this is a   get method this is c3 earlier we were getting  all this now let me send this request again   i didn't get any value http status code  you can notice i am still getting 200 okay   because request is successful but it doesn't have  any data so it has given me a blank response body   all right that means vendor details are deleted  okay so here we have created all these four   methods i hope this part is very very clear if you  have any doubts any question feel free to ask your   doubts in the comment sections i'll try to revert  to you as soon as possible all right i hope this   session is clear to you i would really suggest you  go ahead and try out this complete demonstration   along with this video so this is what we  plan to create and we created this model   class this entire service with get post put put  delete or in short crud operations successfully   we have seen tested all this demonstration with  the help of postman also i hope these things are   very very clear to you so just to wrap up what  all we have covered in this session we started   with the rest api a key important points followed  by spring boost key points we created a basic   spring boot application with the help of a spring  initializer further enhanced it to the rest apis   for this cloud vendor api application so we  created the model class and the entire cloud   vendor information service thank you everyone for  watching the session i hope you found it useful if   you haven't yet tried all those examples i would  suggest go ahead and use and follow along all   those examples that will be helpful for you if you  want me to put this code in the git repo and share   the link please mention in the comment section  i will provide the github repo link to you guys   also i would like to mention please comment and  tell me what next you want me to talk about i   will try to get back to you with that session  as soon as possible leave your thoughts in the   comment section and please share this session with  more and more people so that we grow together more   stronger as a community all right thank you once  again see you in the next session bye for now [Music]
Info
Channel: Think Constructive
Views: 97,689
Rating: undefined out of 5
Keywords: rest api with spring boot, how to use spring boot, java spring boot, rest api, rest api spring boot, rest api using spring boot, spring boot, spring boot 2, spring boot crud, spring boot initializr, spring boot java, spring boot rest, spring boot rest api, spring boot rest api crud, spring boot rest api tutorial, spring boot tutorial, spring rest api using spring boot, thinkconstructive, api testing, java, spring boot rest api project, how to create rest api using spring boot
Id: iBGkJln9BPo
Channel Id: undefined
Length: 36min 27sec (2187 seconds)
Published: Fri Jul 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.