Rest API | Web Service Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you have been in this web void of web development void you might have heard about REST API right it doesn't matter which technology are walking with maybe you're working on Java asp.net maybe a PHP Ruby you must have heard this term police list full API so what exactly this rest means right is it very different from the web voice all oh is it very magical things so we discuss about that in this video and if you want to implement that we have the entire C's on that so let's let's get started welcome back aliens I'm Naveen ready from Toys for learnings and let's get started with REST API soon when i was when i was learning android for the first time so now this example makes sense with android so what I was learning I don't for the first time you know I was thinking about making some applications because learning something is different right you can learn syntax of Android you can learn basic programs but then if you want to be efficient in that you should make at least one app now that one app maybe you can mimic some other app example you can mimic a calculator you can mimic a Facebook app so I thought about licks mimic apps so I opened my mobile phone and I saw different apps I've been able in my mobile phone so one was calculator I thought everyone can do that let's do something else I scroll down I thought about Facebook but that will be a very big thing for me so I went for something but less weather he put app now if you ever open our better he put app some it looks something like this why do you have a different region you can select and you can you will get the weather report of that the temperature the humidity and all those things so I thought about let's mimic this app but then okay so if you talk about this app you have lots of data right and this data will be coming from hold on from where you will get this data so you can give static data right you can just assign the data there and it will never change example the temperature in Mumbai is 28 degrees Celsius you can set it that way the temperature in Delhi maybe 20 or 31 degree Celsius you can set it that way so I just prominent royde but then weather keep changes changing right according to the time so let's say it is 6 p.m. now so again the time the available change the climate or the temperature will change if it is too well known of course the temperatures will be different so how can you change this data now of course right your mobile phone doesn't have a sense or even if you have a sensor you can sense the current right right if I'm sitting in an AC room so of course I will get a current temperature I will not get temperature outside of my house or I will not get a temptation of deli so how can i how can I get all these details so the idea is what you have to do is if you want all those reports what you have to do is all over the Mumbai you have to put sensor so just about the building just put sensors are weather sensor about around the globe everywhere you have to put sensor because whenever you don't have a sense of course you will not have a data so if you want data about Danny you have to put Delhi's Danny in Delhi you have to put a sensor you have to go to us you have to go to you new you have to get the New York climate and you have to put your sensors everywhere and that will cost you billions of dollars of course right you will not do that so that means you need these data from somewhere but question arise from there so what happens is luckily we are so fortunate that we already have some web services available on the internet when I say web services it simply means that there is some servers or some cloud somewhere in the world and what you are doing you are trying to access that cloud right imagine how mobile phone here and that's my app that's right not actually my app it is already app in my mobile phone and on this side you have a server right now this server will give you a data but then question arise how can you generate this D time in which technology you will use here so basically this is a normal web server you can take any dynamic web server not a static service the name servers where you can generate data so this is a dynamic server and that's your mobile phone so what you will do is you will requite your server now this server belongs to some company maybe maybe there are different differents different providers of weather reports right so what you can do is they these people who who has all these sensors or they have the data they are ready to give you the data right and what you have to do is you just have to call them so there is a API Sony's API now so API is something which will connect you and that server right so how would you know that how many services that server provide and how a Sabha will know who are you right so if you want to access that data we have to use somewhere API somewhere in between but if you keep that API part aside let's talk about server what do you think how would you generate data now on some Java voice I will take a Java example here so you have to create a servlet right and that's how we do that's how we generate data right so you have to get a servlet a client will send you a request and then this servlet will give you the data within question arise what servlet returns now if you think of a normal boy okay now if you're not familiar with Java that's fine you can imagine a PS PHP application you can imagine a ruby application on a SD application all these applications that return something normally that something is webpage now when I say web page it will be a HTML page now we do the crystal from a browser so many let's say if your browser that makes sense right because browser is a blank thing and you will get the entire web page but hold out if you talk about my application I already have a design right I don't want design I just want data so when I request to this servlet now servlet should not return HTML page because I don't want HTML page right and even if I get HTML page how will I pass it so what are the ask is hey hey sublet I don't want I did that I mean I don't want a piece I want data something will say okay you want data I will give you data in maybe two formats you will get data either the adjacent format all XML format now you might be thinking why Jason why XML it's because whenever you would return some data data should have some structure right you cannot just randomly send any data so when you send data it will be a complex do you have to follow a particular structure now if you follow XML in XML we have this hierarchical structure right so in XML we have this hierarchical structure where you can have a city as a country name inside the country will be having city name inside city you'll be having different or different values right we have humidity level we have temperature so we'll be having all these values right so you can written XML format but the problem is there are lots of people they don't like XML especially I don't like XML but Jason is something you can you can you can actually work well because if you know JavaScript in JavaScript we create adding objects so those are basically your JSON objects right so what we can do is we can instead of using XML we can also use JSON now again you can use XML can use Jason right so your sublet returns you XML all just in format and your job is done right you are getting data but the only thing is when we talk about this data what we've talked about this servlet it's a it creates lots of overhead right right we have a server technology when you have to create a post request I mean you have to like do we do get do post a request object response object a bulky thing right so to replace this we have a simple solution and that simple solution is using a REST API in a technology maybe Java PHP so this says if you want return some data what if you can simply create an object of that on the server side and just return the object itself not object but the values of the object right so you have an object you want to send this state of an object well you don't want to send the object but a state of an object imagine if I want to send a whether you put something like this you have a city name then you have a temperature then you have humidity level so you will send all this data to the client side right so if you can create an object of this you can simply send an object in JSON format or XML format right so that will be converting an object into a state right so that is called as representational state you are transferring this state right so you're sending the state of an object that's why two colors rest which is representational state transfer so what you're sending is not object but the state of an object now that state of an object can be sent using JSON or XML so that's the first thing then what if so I mean thinking about this web world right what we do doesn't matter which website you're working with technische with what requirement you have we do one simple thing we do code operations why'd we recreate that we create the resource we read the resource we update the resource and we delete the resource now okay now if you observed I said resource right now why this source is because in this world of technology we be warned something on the server right so we example I have if I want to add two numbers whatever so here what you say hey so I just add these two numbers for me to plus two it will give me four so when you send a request so if you send a normal HTTP request of it looks something like this right we have sadp colon double slash okay WOL is forgot comm list let's say for my website and then dot-com if you want to add two numbers you will say add and then you will give a question mark which will send the values so you will say question mark and then two I mean a query parameters right you have to send question mark and then values instead of that what you can send you what if you can send the value something like this okay you shall going for add what will make more sense is you're fetching some data example if you go to my website we have this awesome feature you can get your you can get questions right so if I say I should EP I mean the website name slash questions slash Java now this will make more sense instead of saying pushes question mark subject equal to Java right it looks more like a resource so what you're fetching is actually a resource right so it's you're fetching shop in shop thinking that or you have a server when you are doing some processing is stop thinking like that what you can think about is you are fetching data as it is as that client you don't even know what is happening on the server what you know is you are getting your resource back and to get to do this to do that see our UD which is create read update and delete we can actually use rest I mean HTTP methods right in HTTP we have post method we have put method we have what we happens we have get method and we have delete right so using this four methods of HTTP we can achieve our cooperation and that's how your REST API works again before this we were also having soap request which is which is so soap web services but nowadays everyone uses restful api is in fact all the companies they are moving from soap to rest but then question arise how can you implement rest so we have two choice I mean we have multiple choice almost one of the famous implementation for the rest is Jersey and the second one which I love is putting implementation of Jersey are spreading implementation of restful api so in this course we'll be we'll be focusing on both we'll see some examples on Jersey and most of the example we'll see on spring rest so I hope you like this video you've got something about the REST API again we will talk about the compliment implementation in the subsequent videos we have talked about REST API which is also restful web services and it is booming everywhere so what exactly we are going to start with I mean what type of example they are going to start again will be going for a complex example once you go ahead but initially we'll be talking about aliens so if you want to understand REST API to understand the overall picture let's talk about REST API with an example which is one of my favorite example called as aliens I mean see we all are aliens right of course you can treat yourself as human in this case I will never say alien you can imagine yourself as human so this alien it is someone who who belong to this software world or the virtual world what you say and everyone in this virtual world maybe they are a technical in one of the field may be database field or programming field or may be testing field or the hacking field so all these are the aliens right and every field so let's say if I'm a programmer if I'm a Java programmer so I'll be having some points in that example when I start with Java as a fresher I'll be having maybe two or three points the moment I get experience I get ten points with T points maybe as experience of six years now I can say I'm 70 I got 60 points out of hundred in Java maybe you might be having more or less doesn't matter so we all are aliens so we have a name we have an ID and we have points so if you want to create a class of this this is how your class will look like right you'll be hanging a class a simple class but it's alien in which you'll be having three variables i l an ID then you have a name and a points right so if I create object of the first alien let's say the first alien name is mommy itself I will say then ID 1 0 1 or name naveen and then the points would be like say 16 so that's my value let's say if you have one more object so now you have two or three objects right now if you want to send this data on the client side you have to convert this data into XML or JSON again how will you do that that's a different thing will will be same thing in the practical session but I want to send this data in XML format even before sending this data we have to accept the request right and to accept the request first you have to get the application now question arise how will you create an application for REST API again - the prerequisite for this would be you have to understand Marron because using mavin you'll be building this project and luckily I have a I have a video on that as well on YouTube so first you have to understand how mavin works a second we have to create a jersey project now why Jersey it's because the rest is just a concept right and to implement that we need some implementation you can use Jersey or we can use spring as we discussed in the earlier video so you can use one of this now if you are using Jersey of course you have to use Jersey libraries you have to create a project structure which will be working with Jersey now you might be thinking okay we have to create that structure huh don't worry we have a map in video right and in the indian oven what you can do is you can ask mavin hey mavin can you just give me a jersey project now we'll say okay are you got your jersey coaching there so don't have to worry about the project creation it will be taken care by a mavin now once you got your project there will be some project structure there will be some dependencies which you have to add no you don't have to add any dependencies right because Josie will be given by maven and but then we have to create some resources right as we are discussed in rushed API we have to create a resource a lien is already source right but then it is simply a POJO class right I mean what is pojo it's a plain old java object so this is a plain old java object what we want is when you send a request there should be someone there who will respond who will send your response now how will you do that again if you're thinking in servlet technology in Soviet we have to use you have to get a class then we have to extend our class with the help of HTTP servlet do we have to do that here not exactly we don't have to do all those things here in fact I believe that REST API is much simpler than sliding server technology and you might be knowing about cyber technology even if you don't touch ok because anyway learning rest is easier than learning sublet so if you know the rest api think there so using rest api we can send a request and you will get the response so there should be someone who will request it will accept a request and you will send your response so once we have seen the theory about REST API let's try to implement that but then before implementation of course we should be knowing which tool we are going to use right so in this video we'll discuss about the tools we require so welcome back aliens I'm Naveen ready and let's get started so what we are going to do now is we'll try to understand what our tools were required and as you can see I have already using already opened eclipse here now question arise which are the available options you have again you can go with any one any IDE which you prefer maybe if you are in love with NetBeans if you are in love with IntelliJ you can go with that the same example which we are doing here will work there as well right so we have Eclipse we have NetBeans we have IntelliJ so if you can just just go to the website so just search for Eclipse download it will take you to the website and you can download the Eclipse from here right so if you can search for Eclipse IDE for Java EE developers and yeah so you have to make sure that when you download eclipse you have to make sure that you are going for Java EE version it's because we talked about neon itself neon has multiple version as you can see we have Java developers we have Java EE developers the difference here would be since we want to work with REST API which is a part of web development so we need some servers right and to add those servers this Java EE developers version will give you the option so we'll go with this one which is Java EE developers and depend upon which platform you're working with may be Windows Mac or Linux you can download particular version now once you download it you just have to unzip it and you are good to go so once you open eclipse this is how it looks like so this is how your Eclipse looks like now once you have you know once you have downloaded the Eclipse and when you open the Eclipse it will ask you for the workspace you can mention any folder which you like I just mentioned the workspace as YouTube and I'm good to go and then we have to also make sure that you add a server now again I have a separate video on this on YouTube to search for telesco Eclipse and Tomcat so it will give you the video where you can watch how do you connect this Tomcat with Eclipse but still if you want to check it out so what what I will do is I will just delete this part I mean okay what you can do is it will not give you this option there so it will not be having this Tomcat 8 by default so you have to click on this there will be option of no sub available click here so just click on that so same thing I can get here right click new and server now most of the time you know you don't get this server tab here so for that you have to make sure that you are in Java EE perspective if you're not there just click on this button which is perspective and select Java EE now luckily for my machine is by default when your machine also it will be by default but in case if it is not there just make sure that you are selecting a Java EE perspective and then go to window show view there is our option of servers now when you click on this servers it will give you this window ok the current window which you are working with so right click and say new click on server and now out of all these servers you can see when you open that it will give you a list of servers here and depend upon whatever plugins you have installed in Eclipse is not the new Eclipse I'm using I'm using this Eclipse from a long time so I'm getting option of Red Hat and all those other things so if I open a click Apache you can see we can have Tomcat 8 version but unfortunately ok I mean OH before doing all those things have to download Tomcat as well so you have to go to Google and download Tomcat and then you can select and say next it will ask you for the Browse where is the folder just select that and you're good to go ok so once you've got Tomcat once you've got Eclipse it's time to create a project now to work with this project what we'll what we'll be doing is we'll be creating a maven project now for this of course you should be knowing about maven and if you have not seen that make sure that you have seen my maven video which is there on youtube so search one telesco maven you will get some idea how to get a maven project but if you already know that that's great so say new and go for others and let's say I want to go for a maven project and say next we'll say next now which type of what type of archetype I have to use here no mavin is famous for that those archetypes right in fact if you want to create defend the kind of project mavin will say okay you want a project I will give you the project don't worry so we'll go to free now I know that I want to build a jersey project now why Jersey because to implement rest we have different implementations right and Jersey is using his jersey projects or not the implementation again we'll be doing this in two steps I mean first we'll focus on Jersey and then we'll also see how to do the rest api using spring using spring MVC so here what I will do is I will search for Jersey and you can see we have lots of arc types already available for Jersey okay as you can see we are getting lots of option here but it is possible that this options are not available in your machine okay it is possible so in that scenario what you can do is okay when you search for this it may happen that your mavin is not able to interact with the internet okay it may happen in that scenario what you can do is first you have to make sure go to eclipse go to preference and in fact how it was not working for me so I did some changes so go to mavin click on Navin here and you have to select this for text so you have to make this for tech service a download archetype source download archetype documentation download a positive index I mean this is the main thing and then update mavin but you don't start up so select all these things click on OK and you have to make sure that those four tix are there and now create a project it should be working so I will say again a mavin project I will say mavin project and then I was saying next and I know I mean you know right we have to search for Jersey here ok we'll search for Jersey and ok so it is coming very fast on my machine because I've already done this in my machine if you are doing this for the first time it may take some time so I want to go for the web project right so I will say Jersey QuickStart web project web app and click on next now it will ask you for the group ID as you know mavin so we'll say comm dot that is go and the artefact ID for me is let's say demo demo West so that's my web application which I'm up where I'm walking here once you do that I just click on OK and your job is done can you see that you're got your jersey project and if I expand that not not this our tab if I expand this project we got SRC and we got a Java folder inside the job folder we have comm we have that is Co okay this this but it is still in building phase as you can see they're still getting this project and this project is coming from the internet as you can see it is downloading everything here okay so it is downloading the dependencies whatever we need and that's the beauty about Navin you know if you are doing this in a normal project you have to add all the Java files by yourself you have to create you have to do some settings by yourself right thanks to mavin it will give you the project so yeah that's how you create a project in the next video we'll talk about how we can write our first code in Jersey and let's see just do some settings there so that's it guys that's our project building and as you can see the project is already built here I was supposed to stop this video but then it is already build let's expand this Java resources as you can see okay let's have a look on these things ok so let's expand this and okay where's that so if I expanding it it just taking some time because it is downloading the source code as we have checked everything so let me just click on that once again it is taking some time as you can see by the time it gets open you can focus on your palm XML file which is the most important file for your for any any maven project right so if I double click that and my machine is working waste low now as you can see because if you're downloading lots of things from the internet and it makes a system very slow and luckily I got a good machine otherwise if we are working on normal machine it does take lots of time ok so yeah so we will talk about this we'll see how this works well so as you can see we got this project here it's working slow let me just check it out here what's going on behind this again ok so it's taking some time so what I will do is I will stop the video here and the next video will discuss how this thing exactly works so now let's try to understand this project structure now if you remember in the last video we have talked about how to create a jersey project and you have done that right you can see we have we got this project here now this is a simple web project based on mavin and again you if you open this for maximal file will you beginning beginning all the charges you just dependency there now the most important thing is let's try to understand this structure here so if you expand this SRC folder and if you expand this meta mean and if you have ever worked on servlet js before you know this structure right so we have this jar folder where you'll be having all your jar files of course alright when you talk about char place when I talk about your modular classes everything will be built belong to this Java folder and all your pages example JC pages then you'll be having your JavaScript pages will be belong to this this webs web web app and if you expand this web app we all we already have our index dot JSP now you might be thinking this is a this is a rest project right where you will return data in a text format or JSON format why do you even need this index dot JSP I guess when you create this project is in a Jersey format they give you this page just to test your application if you are if everything is working or fine or not this is not exactly what you will be using so this is about just this this is just a test file for you so if you open that you can see it says the resources you will find a way where you will find it my resources and it belongs to Web API again those are things are there and this is only for testing ok and then it prints project Jersey website and all those things but if you can see we are getting an exception we are getting some error here it says this super class Java X dot servlet dot HD p dot servlet I should be serve that is not is not found the problem here is whenever you create a web web project you have to make sure that you have Tomcat assigned on any web server assigned here so in the target runtime you have to take this you have to say check mark which is apache tomcat 8.0 target connected and now if you click on OK you can see the error is gone so you have to make sure that you are assigning some web server to your project so that it will understand that it will get that HTTP servlet class now everything same skirt so let's let's try to run this application know before running I have just one more thing if you open this Java folder inside they have palm folder we have the entire package list you write week we got demo test and inside that we have a file a Java file called as my resources now inside this my resource you can see we have a method let's get it okay so we have get it which has two things which is Android get either it produces what it means that we will talk about this later in detail time in we don't want that let I mean let's keep it there so the thing is if I done this application if I if I click on this button as you can see you will getting dual beginning this page when you click on this web resource you will be getting something right so even before clicking on that what I will do is I will just focus here I want to I want to print get got it doesn't matter where I print it maybe in the console or maybe in the page so let's try to understand that how that works now to run this application what I will do is I will right-click this I will say run as an on server so I already have a tomcat server in the running State so if I click on that it will restart my server of course and if you run this okay now you got this page right that means everything is working now that's and that's what you say a delusion now if you if you open this console you can see we have an error doesn't matter if you got the output but still because we got an error right the error is injection manager factory was not found again due to some update they have changed they have made some changes so this thing is not working in the new version so what I will do is I will do my I will make some changes here so in my pom excel file if you just go down you can see we have Jersey version which is 2 point 2 6 - B 0 6 you have to make sure that you you make it as B 0 3 ok it is working in this version I guess they some problem with the new version so if I say save it will change my resources if I expand this Java resources and if I expand libraries inside which you have mam and dependencies you can see the Jersey now changed to 0 3 I guess it will work now let's run this once again ok let me just add Matt out to restart my why is Misawa but CEO and let's the right leg and restart I hope it would work this time so it's running it's running as you can see there is no problem now there is no error and let's get back there and if I say enter okay you got the same output in fact I don't want this output to be to be used in the internet browser I hate in the internet browser of Eclipse I want to use my own browser I will use Firefox so just go to window go to web browser and click on Firefox now if you go to Firefox again next time when you run this it will it will open the in Firefox but this time I will open the Firefox 1 myself and I will say enter what was that ignore that let me just copy this I get that this is some number I've copied from somewhere let's say enter not here what's happening ok so if I go to browser I can see we are getting this page now of course when you click on visit will call for this but let me just type it by myself I will say my resource and if I say enter oh we got an error we I was expecting a got it right which is a text which we should have printed maybe it is spending on console let's go back to console let's saved if you got if you got got it there it's not there so that that message is not able to find the resource why it is not able to find the resource to understand that let me just go to one of the most important file in your project which is if you go to web and if you have web.xml file now this is the most important file as you can see it says to work with just see this is the class which you have to use this is your servlet container so whenever you send any request this is the class which will handle your request for Jersey but hold on it will accept only those requests which are coming with this URL which is Web API slash that means when you request for web my resource you have to also mention Web API slash my resource and now when you run this can you see that we got got it right so that's how your rest thing works again this is a simple text ok this is not a page this is a simple text again you can type it here in the address bar or what you could work what we could have done we can simply say this source our justice source it sends the request of the same same thing and that's why I have not clicked on that before I wanted to show you how to access your resources with the help of Web API again if you can change that from your ul pattern if you remove this it will work perfectly fine if you remove from here you don't have to put web web api there right so that's how that's how your first web service work so let me just repeat now we have not done any changes yet we are working with this same project which it just gives you because I want to I want to make I want to make you understand what basics of Jersey here so that we can continue in our easy pace okay so what is happening here so when you send a request for my resource this is a path which is linking so when you say when you go when you go to your browser or what was that let me remove that yeah so when you go to your browser when you send my resource request the request goes to web.xml file and it sends the request to the server container which says I have to search for this path with my resource and when is once you get this path connected of course you have to you have to use this class inside which you have only one method colors get it and on that we are mentioning get now it is this get get specifies whenever you send a get request this is the method I want XS is it a get request as of now by default all the requests are get right so to specify its a get request we are saying get now what is produced again we will talk about this produced in in the next video but then we are printing got it right which is returning and it is going on the on the page so yeah that's it of this is our first Jersey project so that's it from this video in the next video we'll do some changes and we'll make our own stuff now let's try to create our own resource now if you can see what we have done here is we have used my resource right and when you request for my resource we are getting got it but to make you I mean if you want to create your own resource now what I will do is first of all I will create a modular object because see it's always better to return an object format right because that's what rest actually means which is representational state transfer so you are transferring the state of an object so for that you need object and to get an object first of all we have to get a class right so let's get a class here and we'll go for the alien because I love aliens and I'll click on finish okay well okay just if you don't know what this alien means I call all my IT Khidr trainees or students or colleagues as aliens because we are we we live in virtual world right we are not we don't live in real world so yeah so we have a alien here and then I got let let's let me just use some variables here so again we'll go for some typical examples so we'll go for Drive it a string name every alien will be having a name and every alien will be having some points maybe okay when I say points it means for a particular so let's say if I'm I'm good at Java some as a li I mean I'm an alien I'll be having some points right maybe 50 points out of hundred maybe eighty points out of hundred maybe ten points out of hundred so every every alien will have a name and a point and for this what I will do is I will say getters and setters will say so's generate getters and setters let's select all and click on OK so we got our class here right this is our simple POJO what you can call it as now what i want is when i like twist for alien so let's say if if anyone says hey hey I want a lian's I want to return the objects of aliens maybe one or two objects whatever you want to return initially will return only one and let's see how to return multiple multiple objects and if you want to return that in which format you'll be returning that that's a touch one of the question so what I will do here is let me create our own resource again normally when you work on sahabat application maybe some other frame look like spring we always create a control right because normally we follow MVC pattern there now here we are not following MBC Python what we are trying to achieve is when you send a request you want a response it as a JSON format or XML format right now we can call it as my own resource so we can do it here itself but it's sure of touching the I mean stuff changing the existing code whatever do is I will create my own resource and now it's not that difficult to do that right we're clear the new resource will say new class and I will call this as alien resources again you kid we can go with alien controller or maybe only maybe some other other name but then this makes more sense why it you should always get a project which I mean you should always use a name in the project which actually makes some sense so I will say alien resource which will return the alien object now since you want it to work with requests of course you have to use one of our favorite annotations now if you remember in the other example where we have talked about this one which is your my resource so we have worked on my resource and my resources first of all it means close all this extra thing so this - source having an annotation which is at path right this defines in which on I mean at what request you have to call this so I would say at path I will say control space so whenever I send this request with a URL okay so I will say whenever I call for aliens I want to call this resource right but again we need some method to work with it right so I will get a method here okay let me just say method name as okay so we say meta name okay I'll say public I will say method name as maybe it will return okay okay will say get alien but then it will return an alien object right so we say alien and the method name would be okay the method name would be get a lien now this gate alien will give you an idea a single alien right okay so we have to create in an object will say alien a one again you can go with any name doesn't matter will say new alien and let's assign some values there so I will say a one dot set name I will assign the name of myself I was saying evene and I will say a one dot set points I will assign the points to myself let's say I am assigning Oh point sixty out of hundred okay so I want to return this object and the way you can do that is by simply saying return a 1 and that's it your job is done here but hold on how this method will be called is it it will it be called automatically I don't think so but still let's run this code and let's see what's the error we are getting because that's how you when it is understand what's going wrong right so let me start my restart my server and let's get back to my browser which is Firefox here and instead of calling resources this time I will be calling slash aliens that's not a duds of magical capacities and if I say enter oh we got in at all it says for not for and anything in on the console nothing much as you can see we are not getting any any error here but here is the issue it is giving you a warning it says this so says alien is it has no resources no sub methods neither services ok it's simply saying that whenever you use resources you have to make sure that you create a method which returns something so here what we'll do is I will say this will be called whenever you request for get request so when you send a get request then only you should call this aliens ok that sounds good and I will do one more thing just to verify if this this is getting called or not so I will say get alien called just to make sure that it is getting called right so you can also maintain a log file but let's say this println will do let's run this application once more I will say click on this and run run and run on server enter enter ok now if I go back to a browser and I will request for the same thing aliens ok this time we got a defender I can you see that we are not getting for not for anymore we are getting 500 that means when you go back here it says get alien call that's perfect everything is working perfectly but then it says message body right are not found okay so what you do is whenever you return something you have to specify what type of data you're returning so you have to show your intention right so I will say at the rate produces okay other H produces you have to mention what it produced so we'll say produces and end this we will mention a bracket square bracket now there are some constants we can use we can say media or you can simply say a application Jason so we have different or not Jason application underscore XML but that belongs to a class which is called as media type media type dot it is application XML so we'll say application underscore X or what's happening XML right so affirmation media type underscored OTT Exim application XML lets me input the package so that's your media type okay now I guess it will return what we are expecting here it says application underscore oh I've made a mistake here it should be in capital letters is it capital yes it is capital so let's go back to my XML let's see oh this one yeah I and this should work now let's try okay when you say it should work sometime it doesn't work okay because of something is missing let's try this and let's run this application let's get back to our browser which is here and the same request Oh again we are getting 500 it says get called now what is wrong the thing is whenever you send a request it is it is I mean it is ready to get converted but then you know when you talk about conversion this object will be returned in XML format right so you're the alien should know that which is the root element because in XML we have this hierarchy structure right we have root element within we have element inside that anyway we have an event inside that so what I want is this alien should be the root element and then this name and point should be elements inside that and to achieve that we have to use one of the annotation called as ad rich XML root is that XML root element so we have to make sure that we are using this annotation because I want to return our XML file okay now I think it should work let me shut this server from a long time I'm saying this will work this will work but let's try once again then go back to my browser and let's say enter oh can you see that it is working we got our XML data and you can see it is so beautiful we got this alien name alien tag here inside this is your loot element by the way and then we are saying names and name and points right so this is how you create your own resource and I mean can you see that it's not that difficult I don't know why people think this REST API is damn difficult you can see it is so simple it is easier than servlets I guess you know we just have to get a method and just return the resource just written the create an object and return it again you might be thinking we are creating our own resource the own object what if you want to fetch from database we can do that very easy you just write a JDBC code and fetch it from there or maybe you can use hibernate or having it framework here return the OP they get get the object from the database and return it and it's it's that simple okay no nothing nothing to do extra but maybe in future we'll talk about how do we merge spring JPA and rest together so by the time this is this is how it works let's let's keep it simple now we have just created in an alien class on top of that we have it an adder eight XML root element to define that we are returning this in XML format and we are creating this alien resource where you are creating a method calls get alien with that with annotation get add get because we are sending or get request ID produces to mention that what we are producing it and we got this object we are you turning that object and everything is so simple so that's it from this video in the next video we'll discuss how do we return multiple objects right and yeah that's it so in the last video we have seen how to return this list right in fact let me run this code once again just to show you what we have done in the last session so let me just run it as run on server let's send next and click on finish so by the time it runs in running state let me just say okay them all rest we have to mention a web api remember right and then we have to request for the aliens I will say aliens on it you can see we are getting one alien what are expecting now is it should return multiple aliens right so let's say if I create one more object here so the way we have Naveen here and let me just copy the address paste it here and this time we'll go for a two and let's assign this is a 2 and a 2 let's set it let's go for a different name let's say the name is r2 here and let's say the value is let's say 70 so we got Naveen sixty point or 370 points and now I want to return a list I want to return a list so what I will do here is I will say these stuff alien I will say aliens equals you and I will say new at least or maybe I can simply say a days dot as list we do we do have this two objects rights we can say a 1 comma a 2 and we got them at least let's let's me import the package for list and let me just do that what's happening just building automatically let's say control shift oh and yeah we got our least right now once we got at least let's return all these so instead of returning a one ok I should be taking this window somewhere down right it will be better at least I will not be getting that pop up every time ok so instead of returning a li a one I want to return a aliens right so I want return a list of aliens but the problem is here also we have to mention it is a least of alien right and now everything is set up I think it will work now okay so have to restart my server let me just do that and I don't have a server tab here let's say oh it's getting started automatically let's go back to Firefox and now if I say aliens now that makes sense right you can see we are fetching aliens and we got aliens regard alien only got aliens we got the first alien as naveen second alien as RT and that's the beauty about xml right so we are only seeing now how to fetch list of aliens but what about if you want to fetch only one one object now maybe I don't want to fetch all the aliens and by the name before this would be get aliens okay so let's say I don't want to fetch the are all the aliens I want to be very specific now I want to fetch the first alien with name Naveen I want to fetch a alien with name RT something like this if I say aliens / Naveen it should return Naveen if I say RT should return RT but it's not working now how exactly do we work that we'll see in the coming video we'll also see how to create a resource I want to create a new resource and there's one more thing you know normally what happens is you get this data from the database and as you can see we are creating our own resource here statically so it will also see how to create a separate class which will give me this object maybe a demo what you say I dummy database or maybe a mock database we can call it as so yeah we'll see that in the coming videos the last session we have talked about resources how do you fetch resources here right so we have we were returning the least of aliens right so we credit these two aliens here and the moment someone sends you our request for aliens you will return least of aliens where you will return these two aliens right and then we have also discussed I mean we have talked about this if normally when you return data of course the data will be coming from database right what we are doing here is we are returning data by ourselves by creating the static resource and that's not a good practice now so what we should be doing here is we should be creating a separate class which will be dealing with database and of course we will we have to fetch data from database but and in fact we can mock it up right we can create a separate class which will return data to us again we can imagine that that class is connecting with database I mean we don't we don't care about that we are not concerned about which database you are working with what we are concerned is this data should be coming from database and to mock it up what I will do is I will create a separate class now and I've named it class as okay let us create a class and we can call this class as something alien so we'll say alien alien what Oh normally if you are coming from web development we normally refer to our class name as doubt right so a class which deals with database is called as dau but then in REST API instead of calling it as dau we can use a good name called as repository again you can imagine you can you can still use dau that's fine you can use any class that doesn't matter but then repository sounds good because you might be fetching data from database may be from from some files may be from network you are fetching it up from a repository right again having said that you can use any class name doesn't matter now so we are the class name which I am using here is a lien repository and this alien repository will work with certain methods now again there is one more reason why I have taken this name as in repository will and you will understand this once we start with spring rest so after after this josie will be talking about spring rest where we will implement the same thing there this Eden repository will make more sense right so time in let's say we have a class in repository you can take any class name with the way you want okay so we had this class right and this class will work with a database now my talk about resource as a user you can fetch all the resource you can fetch all the aliens in the database you can save a new alien maybe you want to add a new alien object maybe you want to fetch one particular alien maybe you want to update one particular alien so you will do all those operations with this alien repository right so this in a repository no need to have all the methods right so what I will do is I will first of all let me create a array okay I may be at least and I will I will use that list will be accessed by all the objects okay now how do I do that so what I will do is I will simply create a list of objects here so I will say list of aliens and I will name this alien as okay let me just name this as aliens equals you okay what next no now we have to say new at least we have to get the audit as well right in fact we can create this object somewhere else so let's let's say we are declaring the object object here now once you have declared it we need a constructor where you will create the object of it so I will say public alien repository where you will create an object of that so we say alien repository and now here we will create an object of aliens right and that's how you do it that's the one of the advantage of using constructor so we can enjoy our variables and then so we have issue is our reference variable now which is aliens in this case now I want to have some static data okay of course you can imagine this data is coming from database but then we don't really I mean we are not really creating a database here alright so let's mock it up so what I will do is the thing which you are doing here these aliens we will cut this part and let's put it here in the inner repository right so we already have two aliens here and let's add these two aliens in our in our body so it's the aliens dot add and let's add a one and let's say aliens dot add it will say a two now what will happen is if you want to fetch this aliens of course you'll be reading these to aliens or maybe you want to add some more aliens you can do that right it is available for you okay that sounds good now once you've got this constructor where you're adding these values now we need a method which will return all the aliens so what I will do is I will say public which will return you the list of alien see in this video we are going to only talk about this suppository how do I create all the methods so I will say aliens not get aliens which will return you the number of a list of aliens and now we can we can simply return whatever alien object you have so we'll say aliens return alien right so whenever someone asks you for gate aliens you were returned this aliens here right is that simple then you need a method which will okay so you need a method now which will return only okay maybe you want to fetch written only one object maybe it is possible right if someone says hey I don't want all the aliens I want an alien object with ID 1 0 2 that's possible again you will be seeing that how do you send our equation declined okay let me know I mean just to let you know okay first of all let me remove this thing here okay so first of all when you talk about this list here when you talk about requests so requests will be for any type right a request will be for only aliens which will return all the aliens maybe in future we'll be working on how to return one object by ID so let's say if I specify idea 1 0 2 you will it in that particular object maybe you want to delete alien so you will do all those things with the help of this repository again how to implement that in this resource that we'll see later but time in let's focus on this repository so maybe you want to fetch one particular object so in that case what you will do is you will say public again this will make more sense after some time when you talk about how do you how do you use that resource so we'll say public alien and we'll say this is gate alien if you want only one alien and the thing is if you want only one alien how do you specify which alien you want so of course in this method you have to specify a primary key which is ID in this case I hope it's yeah it's ID now based on the alien ID you will fetch that polar object right how do we do that here how do we implement that concept here so whatever you do is I will say I want any object of so it's a alien a 1 equal to but how do I return that particular alien from the least okay that's a that's a difficult thing now let's say buddyfight will say it is null or maybe let's say let's say null and let's return that a 1 but then how do you fetch that particular object from a list so what you have to do is you have to say we have to run a for loop we'll say alien a : and we will refer this this is this is to the alien list which is aliens and you will run to the loop and you will check if if the alien a dot get ID matches with is equal to ID in that case return a right so now what will happen is it doesn't matter what we are returning here in fact we don't even need this thing but yeah we have to return something right we'll say new alien we don't want to return a null value because that will generate a null point exception if you are not able to find an alien it will return a blank data for alien but at least you are not hit a null so what you are returning is yeah so we have four in fact you know what return null will be a good thing because in future we'll be looking at that as well so if you send a particular ID we will return that ID and we will check if the ID matches with the ID which you which is there in the list will return that particular object otherwise we return null right this for one ID this one alien maybe you want to have some more maybe you want to have methods like delete update so you can you can write this all those things here so again will be I will be completing this and then we will see how to work with this source but then how do you how do you work with at least this resource so what I will do here is I will say let me create an object of alien resource here so I will say alien repository not the resource in repository and we'll say this is repo which works the database and we'll say new in a repository ok that's how you create the object and then from here instead of saying return aliens will say repo dot aliens right so now you're fetching data from database now it feels that you are fetching from the different database right so this if you look at this alien resource now we don't know from where you will get the data but we are getting data that's important right not to make it work what I will do is I will i click my application I hope there's no other server running on this this machine lets it run as a non run on server now some time it does happen that I might be having some other server working and let's let's see let's say click on finish and I cannot see any error there okay everything seems good so let's say that send a request for web app Web API and let's say aliens and you got your data now trust me this data is coming from the depository because we there is no other way you can detect on this side so yeah this data is coming from the depository which is here so well again we'll see how do you do other stuff as well they are producing the resource because in the next video we'll talk about how do you produce some more resources and then how to update delete all the stuff on the way in the last video we have seen how do you fetch data and then we have also created this repository right in which you are saying we want if you want to fetch depositories if you want to fetch all the data but now my concern is how do I create a new resource let's say if you have this two objects that's fine maybe I want to get my own resource there and we are assuming that this data are coming from database maybe you want to add data in your database how do we do that how do we add data in a database so for that of course you have to send a request right and when you send a request eigen will not be using this okay so when you send a request of course there should be a method here with who will accept your request now personalized for whatever request you you specify that you are creating a new resource so what I'll be doing is I'll be creating a method here which is public which returns maybe if you want to return you can return let's return I need an object and let's say this is this the name is create an alien ok and now this alien takes a alien object and that's the amazing part about this right you can directly send data from the client and you can accept it here let's name it alien I mean a one and let's return the same object okay let's return the alien object itself but then I want to add this data in database right so we'll write that code here okay now when I say database I'm talking about repository again we have a fake database okay we have to feel that guilty every time we work with this so this should work with not get requests because when he was a send detail we had as we have discussed in the theory video whenever you send data of anybody create a new resource on the server we always use a post request so post is basically basically for creating a resource right so gate is for fetching the recording the resource post is for creating a resource put is for updating a resource and till it is for needing the resource so I want to use post because I'm creating a new resource here and then I will say at the rate okay but then okay now I want to change this link what I do is I want to say create on the event when i say alien slash alien so i want to change my URL what you only have now I want to say aliens / alien for that you are on now so aliens are already here and will again go for the path variable because I want to create a new resource here and I will say that resource is in this case I will say alien ok I mean the path is alien and let's say ok everything is working fine now it's it sounds good so we are accepting it we are fetching an alien and everything seems good now let's accept that data so first of all I will printed it down on a one let's see before adding that into database let's see if it is working I want to I want to print that data here ok it should work right so what I will do is I will say repo dot there should be a mythical has create on the repository where you which will accept your a one it will add data into repository ok but hold on we don't have any method inside this in a repository so we can directly click that either here and we can create a method there so we'll do that we'll say create method agent right ok so we got this method which will accept your value ok so how we should create that thing here it's very easy we got this alien a1 here right now we'll fetch our list which is aliens and we'll say dot add and let's add whatever alien object we have so we'll simply say aliens dot add a one our job is done ok again you can imagine the data is going into debate habit database because we are not doing that yet but it is going in database we can imagine that the important part here is not to work with database the important thing is how do you handle rest requests right now we have done all these things but the question arise how do you send this data now when you are fetching data we are directly using the address bar right if you can see my Firefox when I say enter I am getting the data how about sending data how do I do that we are not getting any form here that's not our intention right we are not making a website here or we are not creating or design for that how do you send this data normally when you would talk about sending data fetching data for the REST API we need others we need some client application which will do this when you make a mobile application when you use all these things in mobile application you our application becomes a client when we use angularjs to work with this data we use angularjs as a client but unfortunately I don't have anything here we don't have a mobile application we don't have our angularjs script so we have to use a test we have to use a tool using which you can do all those things but which tool we have to use now luckily we have lots of add-on tools available on the market so example if you open okay let me just go back to my Eclipse okay so if you right click this and if you click on this new and okay so if you go here there is there are lots of plugins I believe but example if I click on apps I do have lots of plugins are installed in my system as you can see we have hang out but one of the most one important one is this one which is called as postman now postman is one of the tool which I used to do it it works at the client it was a rich client for me ok this is what I was doing in the somewhere else let's close this so this is how your postman looks like but then how exactly it works how do we send requests using this how do you fetch this parsing that's that's a different point right again this the same and on plugins are available for for your Firefox as well but then I'm using Chrome so let's use postman there but then how to install a postman how to use postman and how do that how that resource will be created that we'll discuss in the upcoming videos it's time to explore what is postman now as you can see postman is a tool here which is an app of Google Chrome again for Firefox also we do have this type of tools so I'm using postman here Watch for Chrome now you might be thinking what's the advantage of this so what I will do is I will just copy this link and let me just post I mean paste that in postman and as you can see if I say ok where is that oh we already got the a fist I've copied the wrong link or what okay we'll say copy and let's say this part and paste as you can see I'm copying the list to the moment I say click on send you got your response right so the same response which you got on the client on the browser you got the same response here now you might be thinking what's new in this we can do that on browser as well right why you have to install a separate app it's because if you expand this list you can see we can send multiple requests we can send a get request we can send a post request we can send put request batch delete copy so we can use all these options of HTTP which is not possible in browser in browser we normally use get and post only right otherwise if you want to use extra top features like put and delete which we are going to in this course of course we need some client right again we can create our own client if you if you want to explore that we can create our own Java client but then we don't want to do that as of now we are focusing on inbuilt tool so we got a postman where you can send the request if you want to send a post request so simply click on post and say send you don't even change you don't have to touch to your URL if you want to do that you can but then not required so you can you can fetch data in JSON format you can fetch data in XML format the way you want right but then how do we how do you find this tool and where to drop somewhere to download this so download this what if do is you have to go to chrome and you have to open a new window and whatever you do here is in this chrome you can see when you open a new tab there's an option of apps so you have to click on this apps and okay from there if you don't find Porsche made here you have to create a new you have to click on this web resource and in this web the source you have to search for which app you want again if you simply search for a rest client there are multiple apps available as you can see if I search for those I don't know how many how many options we'll be getting now you can see we have lots of rest client available we have just yet another rest client okay we can be you can try this all these things which is available but then we will be using postman so we'll say search for postman search for this and okay so this is the fault or not this one this one so this is the app so we do not have to focus on exchange in we have to focus on apps so in this apps this is the postman this is how it looks like so I have already added into the Chrome so you have to click on this add to Chrome where you will get the option and you have to click on that and you can see how many reviews we have for this it's 8286 reviews right this is a very very very famous a client is not just which I prefer the lots of people they do prefer this type of this client here so if I click on this then lots of description available you can read it out if you want and you can also read it on certain conditions if you want but then once you install this your postman will be up and running I guess you have to restart your you have to restart your browser if you after installing this it will it to notify that but then after doing that when you open when you open your post my this is how it looks like and how to use this so you will even understand how to use this in the entire course but then if you just to go to go for a quick glimpse you have to select on this great request paste your URL here and click on send it will send a get request if you want to send a post request just click on this post and then when you click on this send you will it will send a post request but then when you send a post request you have to also send the detail right how do you send a data here to the server and that's an idea we asked we have to specify the body now inside this body you can enter your key value pair whatever you do you want to send or you can type your own XML or you can you can you can type in XML or JSON format you can if you want if you are sending XML click on XML and type XML if you are sending Jason type jason and you can send it so this is how we can use postman and so it once we understood how to use postman how to install postman the next video we'll see how do we send that data to this server so that this this method will be called ok so that's it from this video guys I hope you understood about the postman and we'll talk about this thing in the next video til this point we have seen how to create a get request and a post request now you can achieve a get request from your browser right you can directly go and you can paste this link the problem starts when you have a Porsche case because this is where you have to mention the post because see when you talk about your browser we have your browser supports get requests your browser support post requests but maybe in future will be working on put request maybe delete request and that's an REO we have to use some sort of client which will send all this up of request how can you can use JavaScript here but then we don't want to write any of the code so I will be using a tool versus postman again we have seen how to use this tool and how these tools is made I mean how to download this tool and how to make it open so let's try to run this code so what I will do is I will first of all run this code just to make sure that everything is working fine so I will click on this and click on run button so the non server again it will take some time to start the server and sort of the things okay so as you can see our server started and this is your request but again I don't want to use a web request here I mean I don't want to use browser I want to use postman and in here I will send a request for aliens this is a get request I would say get request and send as you can see we got the data but now this time I want to I want to go for a post request right now let me did miss verify with above wrote about because I'm sending so you can see I'm sending a XML request now if you want to send a post request again we have to use our XML right because if you can see we have an alien where we have it an XML root element so that means we are expecting XML resource let me go back to my postman and I want to create a new object so well I will just copy this so that I will be having some sort of template with me so we'll copy this and then now what I will do is I will go for a post request I will say post but this time you have to send a request could - for aliens - Hailie aliens / alien and if I click on send you can see we got an error it says unsupported Mir type and then all those things that for that you have to mentioned the header whatever data you are sending so the data you're sending is not Jason but XML because you are supporting only XML and even when you send send now again it says bad requests because see you are sending a request but you're not sending data all right you should be sending data as well so in that case you have to go to body and you have to mention XML and that page of XML what I have to send now is I'll be sending the ID as one zero five then I'll be using a name here called as harsh and let me just use a points as let's say 40 points now with this value let me send a request and you can see we are getting this this data back and if you can verify on the server Oh on the server side we are getting this weird output because I forgot to put to string method here my bad because to print the values we should be having two similar as well right because otherwise it will print the hash code so let me say John don't generate to string and click on ok now as you can see we got our two string with our there and let's get back first of all you have to read on the code oh it's already done let's go - postman once again and click on send now you can see we got our data which is coming from the client right again you can write any piece of code here and you can save this data in a database the important thing is now you know how to send a post request now just to make a quick recap what we have done if I open that and is the postman so if I open a postman here as you can see we have we are sending data so you have to make sure that when you send or the post data on the server it is a trick post from here you have to mention the URL again you have to mention the alien as well then you have to say click send but before sending you have to also send the data right so I'm using a raw data here in raw data you have to type your XML and then you can specify what about data you are sending so you're sending an XML type data not d jason and in headers you have to also mention what about data you are sending so that's XML and click on send you can see you are sending your data back to the server so that's how you send a post request but then how do we use a maybe you maybe you want to do something else now maybe you want to send a put request how to data how to delete data again will be saying everything in the subsequent tutorials til this point we have seen how to create a resource how to fetch all the resources when I say all the users I'm talking about the aliens right as you can see in the code we have we do have two aliens here we got it in one and a two and we are returning those values we are also upgrading a new alien but the dice you can see we have created one more method here which is public alien gate alien and we are trying to return this object again we if you find that object example since we have two aliens here we got one zero one Naveen and one zero to R T if I pass an ID as one zero one I need to return the object of our T which one zero one if I if I pass one zero two of course I have to return one zero object now otherwise if you know if it doesn't match it will return the empty object so that will not get any exception now that sounds good right but then we have we don't have any resource here which accepts one which returns one alien as you can see if remember in postman we have seen if I run this if I X if I fetch all the aliens it will it will fetch all the aliens it is working I don't want to fetch all the aliens what I want is I would specify if I'm fetching an alien whose ID will say ID is equal to 1 0 1 now that this is this is how we execute in server technology right what we do is we specify question mark and then we say ID equal to 1 0 1 so that ID represents your that ID represents your query parameter now this doesn't look good right in this JPI what you can do is you can say alien slash 1 0 1 now the send an equation for 1 0 1 if I click on send I want data for that particular alien and as you can see it is not working we are getting exception here which is one of our favorite is for not for in this situation what we to do is we have to create one moment get method of course we have to use gate which returns XML which will be having get alien names and let's copy this so that I will not have to type that once again and this time I don't want to return a list of aliens I want to return a alien and will say get alien which takes a ID will do that in some time and let's return let's return rapo dot get alien when you pass an ID mean I will pass ones either one just to verify this working or not right but we have to make sure one we have to check one more thing when you say get requests you can see we have two methods here who do is you can get requests and both this will work for aliens what you just specify is inside your aliens we have to also specify alien so I specify alien here so I will say get request no not this one I don't want to touch this one now here we have to yeah we have to use one more annotation which is ad path in this ad path we have to mention the path so we'll say if I send a request for alien slash 1 0 1 so if I send a request for alien / 1 0 1 after it turns 1 0 1 right this sounds good right I mean there's nothing wrong here let's get back to our our postman now if I send a request now can you see that we got an object now this is for 1 0 1 awesome but then if I send a request for 1 0 2 this is where the problem starts because if I click on send oh we can you see that we got an error which is for 0 for how to solve this issue now of course you something might have struct your mind that we can create this method once again this time we'll change the path from 1 0 to 1 to 1 0 2 and that perfectly makes sense you can create one more one more method here but imagine if you have 50 aliens in your database or maybe you have 100 aliens is it a good idea to create a 50 hundred methods not exactly right so this once in a one might change maybe I want to go for ones here - so in this case what I will do is I will say ID ok but then this way even this will not work how can you replace this ID bit with a number now if you want to replace this number of if you want to replace this this ID with a number you have to use a curly brackets now this curly brackets means this ID is just a placeholder which means you can replace this ID with anything you want example if you write one zero to this ID becomes 1 0 2 if you say 1 0 1 this ID becomes 1 0 1 but then how to store that value because if you want to use that ID here of course it should be a java variable right in this case I will say int ID and here also I will specify an ID so what do our data I'm getting here the same data need to be assigned in this ID but how we can do that for that we have to use one mole imitation called as at I mean at path paradigm and this you have to mention whenever you get whenever you get a request and if that request has a ID in that that ID becomes this idea min this ID which is here in the curly brackets that represent this ID here and then the value from this will be assigned to this particular ID and that makes sense right this is this is how it works let's run this this 25 it is working for one single one let's wait for one for one zero one first and click on send a request oh it's working let's go with once you're to send oh it's working can you see that it's working for both the variables about the values so for a particular ID you can mention or you can change the value to 1 0 1 once you do maybe you can you can have any other value let's say if I go for 1 0 5 I know we don't even have that let's click on send and you can see we gods blank values that doesn't matter what matters is whatever data you have in the database you're getting that particular data right if this is what we want it so yeah that's how we work with this thing in fact or you instead of sending a new object you can return null and here you can check if it is null you can send a error or something like that what time it is working right we wish take the ders ok so this looks awesome now think about this we are living in a world where we don't actually use eczema I mean weed we do use XML but Excel is not the only format using which you can send response maybe you want to send a response in JSON format and we have not seen just yet right as you can see in this code when you when you whenever you fetch data you are getting a XML XML data what if you want to send a JSON request I want I want I want to say I'm expecting a JSON data now I'm asking my server hey server you are returning XML that's great but I want Jason the moment you say Jason or not here you have to specify okay it is fetching in JSON format what's wrong you can see that it is not giving you Jason so even if even if you specify oh we have to specify the accept parameters it accept parameter applications large Jason okay so that you can see we are getting error it says for not sex not acceptable so this is not accepting adjacent data right what we want is I want to return a JSON request now okay so you might be saying okay shut off using XML we will be using JSON but then if you specify JSON there your application will only return JSON it will not be tanks ml so I want to have what you say is something called as content negotiation so I want this to I am what my client and a server to go for the content negotiation so when a client says hey server I want XML server should say okay I will give you XML if a client says hey server I want Jason your server will say ok take Jason I mean whatever format your client asks for your so return the ID particular data that's important how can we do that and first of all even if you specify Jason will it work we'll see that in the upcoming video so that's it from this video now let's see how do we return a JSON format because if you till this point we have returned XML format right and we were happy with it but then most of the people nowadays they are using JSON so how do you return a JSON value now you might be thinking it's we have to do lots of stuff not exactly we don't have to do much about much about this what you what we are doing if you see this produce annotation this videos actually takes an array of eczema I mean the area of media type we are specifying only one right if you want to specify multiple media type what you can say is in fact first of all let's I don't want to use XML I just want to fetch Jason okay we'll see how to do both but then time let's focus on Jason so I'm not producing an XML I'm producing a Jason now would it work if I'm accepting if I'm sending a Jason and if I go back to my table if I go back to my postman let's verify I'm sending a Jason request and we got an error see there is no error with the response okay the problem is with the way the server itself on the server side we are getting error which is 500 so when you say 500 it means of this there's something wrong with the server and you can see it says message body right or not found of media type J's application Jason now whenever you want to send a response for Jason format your Jersey says hey hey user I mean a developer as I just see I normally work with XML because that's what is default in Jersey if you want to send Jason then you have to use external dependencies oh now that's the thing we have to download some external dependencies for that that's lots of work right but don't worry Jesse also says I have done done it for you don't worry what you do is in fact if you go to the pom excel file if you just go down and down you can see we have dependencies for just a container we have a dependency for this Jersey V R we have one more dependency which is called as Jersey medium oxy and then is in commented state if you can just uncomment it do you will be getting this depend see in your in your moment in your memory dependencies where you can find it here if you go to library if you go to mavin and you can see we don't have marks as we got it I guess we have seen this file okay so you can see we got media Moxie this is what we want and this always also restarted we just have to add that but our dependency and if you send a request okay it's sending it's sending waiting for the response and can you see that we got adjacent response and Jaison response looks much much cool I mean cooler than what you say XML right so this looks good so this is how you return a JSON but what if I want to send a request for XML now example if I go to my get request if I against of go for a get request here and if I send a request so this time I'm sending a request for Jason but if I send a request for XML and if I say send you can see we are getting a de which is four not six so you have to mention whatever data you are sending so for that you have to again go back and you have to say make it XML but that's an issue right what if you want to provide both so depend upon user some people will say I want XML some people reserve which I want Jason what you can do is you can just put them in put them in an array and in this adder you can specify two types one is Jason and the second media type would be second media type dot it will be XML so that depend upon what user asked for if user asked for Jason if users for XML will return whatever user asked right this is what you call as Kuantan ago she ation you are so this client and a server they are negotiating the content whatever content they want let's go back to the postman and now I'm sending an example request it works now let me send a JSON request it is Jason and send it works right so this is accepting both now can we do that with our this thing I mean this this here let's try let's go with the gate aliens I mean for all the aliens less produced both Jason an XML and let's see if it is working or not let me just go back to my okay is it so unloaded I don't think so you're done let's go back to my postman and if I send I want to request for aliens I want okay first of all let me just go with Jason if I send requests it works can you say that we got a Jason so we got an added here we got a decent re and if I specify XML if I specify XML you can say sand it works I can you can do the same thing for the create alien instead of sending a data in XML format you can send data in JSON format and I would I would leave it to leave it up to you so you have to do it you have to complete this one okay so yeah that's how you use JSON format that's how be you that's how we use this Jason I mean Jersey media moxie so that's it from this video where you return the Jason format and for the further and further to further videos I'll be focusing more on Jason less on XML because I love Jason so once we have talked about how do you send adjacent requests and I mean how do you fete JSON data from this server and if you can observe we are fetching all the values we are even fetching one particular alien and then we are the one creating a new object now the thing is till this point whenever we talked about repository we were having this via repository right where you are storing data in a static way can you see that we are calling this object by ourselves now logically what we should be doing is we should be storing this data in database right so when you fetch when you fetch data this data should be coming from database and when you save data example when I say create this should be stored inside database and unfortunately I have not written any code for that now let's make it real I mean why to work with a fake database here or a fake repository let's work with a deal database so that you can actually feel that thing you know when you save it in a database when you fetch it and all those things now question arise which DBMS Co we're going to work here first of all we have to think about two things the first one is which DBMS you are going to work and the second one are we going to use any framework for that the first one the first question is which DBMS again you can choose any DBMS which you like you can go for MySQL so it means write it here you can go for MySQL you can go for Porsche Chris and we can go for Oracle it's your choice you know the code will not change I mean of course the database the code will change but then your resources will not get affected this will be same for all different DBMS now which one I will choose zero so I will go for my scale because I already I have already configured - well my system everything is working there you can use postcodes you can use a vehicle which which one is which you prefer the only thing is as DBMS changes you have to change some settings again if you have low and JDBC before you know what I'm talking about okay so that's the first thing so I'll be using masks you'll now will be using it will I be using any framework here like hibernate or maybe some atom tool or not exactly you can if you want you can implement that will will be to purely using JDBC here no fancy stuff let's use very BC because we are focusing more on REST API not the autumn toll if you once you learn about REST API I do have a video on hyper tonight just watched those videos also and you can combine by yourself I would love to see your implementation for that but time in let's focus on JDBC again I'm not I will not be using any connection pool in this in this example we'll be going with a simple jetty basic concept because I don't want to confuse you with Jerry visit stuff here so let's keep it simple so first of all I will be using Maskell and I'll be using JDBC now to understand how okay I mean I'm assuming that you know JDBC beforehand but in case okay if you are not familiar with JDBC I would suggest you to watch my JDBC video on YouTube so even if you search for telesco JDBC it will give you a JDBC video and so do such this on YouTube and once you watch that video you will be having enough understanding to watch I mean to continue with this series and if you know Teddy B C that's fine let's continue with this now how do we implement JDBC so we have to implement those seven steps right so I will make it quick so first step would be I want to import Java dot SQL dot star in fact the amazing thing is we will replace this by our own stuff in future at I mean let's say is a java java or SQL dot star then i also need a mysql connector of course right when you talk about your database we need to use a mask well connected right now question arise from where I will get a mask will connect or now to do that we have to go to our favorite website is MySQL I mean the maven repository so we'll go to my mind repository this is a place where you will get your mask your connector so let's search for Moscow connector the first link click on that and just go down this works perfectly fine five point one point three eight I'm using this from a long time so I look I already have or what you say a local Java file for this so it will not take much time to get downloaded as you can see it is it is done in my dependencies against you can you can find my scale connector now now once we have done with that let's get back to repository and let's continue with the steps we need we need object of connection of course I will say connection con initially we'll set it as null now I also need to do some more things let's not use this array as of now because we don't need any any add elaichi all right let's remove all this stuff in the constructor I want to utilize my own object so it's a con equals u driver manager dog gate connection and you can pass those three parameters to the beautiful parameters again you can do in your own way okay there is not nothing that you have to follow my step for JDBC if you know something better just go with that go ahead with that what's a JDBC : MySQL : and the local host well you just local host then the port number for mask ul is three three zero six oh that's X and then the database name Oh which database I'm going to use because we have not yet created database you're right so let me open my mask you'll I was already working on some database here let me remove all this thing say create database and we'll say this is for rest data or there's DB again you can name it anything you want to let me just create a database here and let me create a table for that so as a create table what will say table name as alien and this alien will have alien ID we'll keep it as ID itself so we say ID as and comma name as well can let's say to the size of 20 provided it's not my name okay because my name is very big you know if you don't know that's that's fine okay so I say ain't ID ain't named Val care and I also need a points which off type ain't I got my table which is that I got my table and if Isis next are from alien now you can see Oh what's wrong I have to first say this then I have to say there's and then after says Alex oh what's happening what's wrong it says cannot create a table rest DB because database exists is it let me refresh and we okay we got this database and we have to say at rest you soon as DB and now is it working here that's fine let's get this table and let scratch the alien as you can see it is empty again you could have insert this data from here but just to make it real let's get back to our code and let's insert data from here not from here actually from the user so currency my database name is rest DB right once we caught our database we have to mention the you username so let's say username equal to forth for Maskull username is root and for my skill my password is password zero I know we should not be putting that as a password that's fine once we got this three you are the three thing let's specify username and the password right so once we have assigned all these values and I can see this editor let me say ctrl shift o it will import all the packages what's wrong why Oh okay so it says they might there's an unhandled exception so of course many ways anybody do these things you have to make sure that you use try-catch we'll say try catch exception exception e and in case if there's an error well playing system dot out dot println error we can also maintain a log file we can also do the print race but then let's system dot out dot println is fine for for me at least okay so once we got this connection object we have to also say class dot for them if you want to load your driver so I will say class dot for name and we have to mention the driver name which is comm dot minus QL JDBC driver as I've already mentioned I'm assuming that you know gdb see beforehand so if it if you still don't know it just go back to my go back to YouTube and search for tennis coach JDBC and we watch that video okay once we got these two steps now we have to replace this thing which is your laced your gate alien with a Java query right you have to fire this query to get this elements but then we'll be doing that in the next video let's go ahead so what we have done is we have simply created this constructor where you have assigned these two things right we have loaded a driver and then we got the connection object the next thing would be when I say I want to fetch all the elements this this is not what I want to return I don't want to return something which is not defined right so let's create a list of values now so I will say a list of aliens and I will name this as aliens and that's what I want to return here so I will say add a list and we'll say add the least again if we can use this hibernate framework here this will make our work very easy right but then since we are not using that let's use normal JDBC and if you know how do you fetch values of course we have to fire a query some say string SQL when I will fire the query and the query would be it's very simple so this is Alex tar from alien and okay once we do that let's fire this query now the way you can do that is by creating a statement object so we'll say statement s T equal to corn dot create statement okay now once you got a statement object let's import this treatment object or statement interface what's wrong with this so statement from Java or SQL and we got an error now it says it might throw an exception so again we have to use a try and catch block here so I will say this will be included in the try block and it will be getting over inside the catch block so let's assign there let's say exception e exception e and let's print error whatever error you'll be getting there let's bring that here so we'll say e okay now once we got the statement object it's time to execute it so we'll say st dot execute query now as you know if anybody execute a query it will return you or these are set so what I will do is I will say result set I will say R is equal to s T dot execute query and we have to input this package yes which is from Java to rescue L now once we got results set we we have to run a while loop because we don't have one object right we'll be having multiple objects so we'll say RS dot next so it depends upon how many values you have it will iterate to all the values and every time you get a value let's quit I in an object will say alien a equal to new alien and we'll set the values so we'll say a dot set in I D and this would be coming from the from the database so we'll say set in an ID will say RS dot get ID for the from the first column will say a dot set name it will be second column artist dot get string with it - we'll say a dot set points which is RS dot get and it will be from three right so we got this object and we have assigned the value now we'll assign this value in on these so it's the aliens dot add and let's pass a so doesn't matter how many of those you have if we have 50 or so it will add your 50 aliens and once you do that you will get all the aliens now this is what this is how you fetch the values right maybe you want to fetch only one value now so in this scenario I don't want to return all the values I want to return only one value I can't we can do the same thing but this time we will not return aliens my means multiple Indians will return only one alien right so for that we can even fetch this we can copy this code and let's paste it here again we can call it as code reuse that's what we are doing here right so we'll say instead of having Selleck starred in stuff yeah we have to say so next are where the ID is equal to whatever idea you have defined here maybe if your ID is 1 0 1 it will fetch 1 0 1 right okay so same thing the only thing is we don't have to use a while loop here we have to use a if because we will not be running a loop here because we don't have I mean when you run this code it will do don't return only one value so we'll say if Una's dot next we'll set all this ok we'll create an object and will not add it will return the same object and do it on the same object of course we have to create it outside somewhere here so that we can actually turn it and at the end we'll say return return a right that's the only change you have to do right so we have created this we have five this query and you will you will get a result set we just have to fetch you have to say artist dot next and then you can fetch your values and you can return that particular object it's that simple right and now you have to create right so for create again we can use the same stuff we can just paste it not this one we can just copy this code in fact the cache block as well okay just to him just to make it fast I'm just copying all this thing you can you can do your stuff there okay so the query is okay now when you say in when you create a new thing we have to create a string SQL equality of dimension a query here we'll say create or not does not create or we have to say insert insert into alien will say values now question arise for values we have to specify each and every value right now instead of using a normal statement I'll be using a pre patch statement here so that I can use question marks which is placeholder in this case and instead of saying statement let me go for prepared statement and this would be prepare statement right let me import the package for this it is imported from Java to SQL package anything else yeah so we have to pass the query here itself we don't pass the query inside execute query we pass the query here itself and we don't say execute query when you insert we only say st dot execute update where is that execute update right so we have to say execute update and we don't actually use if-else here because we are not fetching data we are inserting data right but where's my values so I'll just say st dot set and the first question mark will be replaced by a1 dot get ID st dot set string the second question mark will be replaced by a1 dot get name and st dot set int the third question mark will be replaced by a1 dot get points that's how you assign the values and that's how you insert a data now you think it will work let's try let's try this the finally we arrange something which we'll be saving into database right and this would be exciting let's go back here and we start my server I guess it is done or do it now okay let me just yeah so I guess this run is done let me just go back to my post man just 25 is working and here I will say I want to fetch all the values first of all if I send the quest you can see we are getting blank because of course we don't have anything in database let's insert some data so is it post and I will go to body and I would say if I want to send a request for alien and the value is once it once you know I mean 1 0 1 harsh 40 and if I click on sand you see it worked but then is it inserting data into database let's verify now that 2 ways of verifying it the first way is you can go to my skull workbench and you can verify there you can sisters excellent stuff LOM alien can you see that we have our data here and there's one more way of verifying it let's go back to my postman and let's send a get request for all the aliens and you got your alien likewise we can insert some more data just verify it let's let's say alien and I want to send a post request and also send to or doesn't want to send a body so we say hush I mean simply say Naveen and let's add it as 70 right click on send now we got 17 I mean a database let's go for 3 and let's say this is arthi and let's say this is okay let's nip number 60 and send let's verify how can you verify that says the next are from I mean let's fetch all the value from database will say well send a get request and send you can see we got all the data here how can you can send a post you can send a JSON request as well you will get your data back so finally we are working with database and rest and the subsequent tutorials will we'll see how to update it and delete it all so now we have database we have all the request response things now if you go back to your postman and if you tried to send data so let's say what do I do now is let let's create a new resource and do that we have to use a URL which is alien right and then we have to send data as well now to send it data we have to go to go to body and you have to insert a new object right let me go for 1:04 and we said this is my UNK and let's say my ongue now let's say if you have this data now see I'm sending this data in XML format right what if you want to send this data in JSON format is it possible so what do we do here is I will just convert this into Jason because I want to send Jason not it not XML so of course this will not work so we have to send DITA something like this we have to say in double quotes you have to mention the ID then you will give a colon the idea I want to send is 1 0 4 right then oh I forgot to put a comma there and the second thing I have to send is name and let's say the name is my UNK and we'll give a comma and then we really have a points here and the points would be later 90 so I want to send this data now at the moment I click on sand ok so you can see it is getting stored so in this which is getting stored it is working right so that means you can send a request in JSON format or you can send a request in an XML format right now how exactly is working so what do you what if I do okay what if I change this thing now let's I don't want to I mean I want to specify something example if you want to specify whatever data you want to accept you can also use consumes so you can say consumes and now produces something which will send from server to client that is produced so from this server I am sending data to client edges produces consumes is something where your client sends the data and your your server will build accepting it so we'll say consumes and so it only consumes let's say maybe if I mention the media type as meteor type dot application dot XML so I will make an XML and let's say it is working with JSON or not let me just make it XML and I need to restart the server is it will be happening automatically but in other case it's not happening automatically let's right-click and say run as a non server okay so now if I go back to my postman to send this request this time we'll go for 1:05 because we already have one zero one zero four there and we'll go for a name called as pooja and let's have a point which is 80 even those point doesn't matter in this case okay let's run this and let's see what happens now if you click on this send button can you see that we got an error it says unsupported media type because what we are doing is we are specifying that it will accept only XML and that's the problem when you specify it will support only XML then it will only support XML that means if I send data in XML format it will surely work so we'll go back here and say I want to undo so that I will get the XML which we have written before okay so let's set this as 1:05 and let's say name is pooja and let's say this is 80 and if I click and I have to make sure that I select XML and if I click on send now you can see it is working so that means when you specify the type of data you want to accept it will accept only diet particular thing is it's better to have both right our client can send adjacent data or they can send an XML data that's their choice if you want to assign both if you want to accept only this to not anything else you can specify those two things here and that did this will be a good thing you know I specify both so that when a client sign they try to send something weird it will not accept so it's better to mention the consumes annotation as well so consume says what about data you are going to accept XML JSON plain text what else right so once we have mentioned this okay it's a some memory leak issue but it's fine let's go back and let's go to postman and okay so we'll sakes and oh okay we have these two times now I want to go for JSON this time so let's enter JSON data so we'll say Jason again we have to type the same thing right I should have it's somewhere to reuse but let's make it fast so I would say Idid is let's say once your sex and we'll say name is equal to let's say in double quotes let's mention the name I will send this 1:06 in double quotes as well okay now this will be let's say pravin let's say points and let's have a point which is let's say 85 okay we'll keep that in double quotes okay now if I send a decent format let's see what is working or not if I click on send it is working you see that again you are getting data in XML format because we have not mentioned what about data you want to send right you cannot specify by going to headers and say hey I don't want XML I want JSON in return so if I say send now okay where's our data is it coming but not avoid it this is giving you this inform this is giving you JSON format so that's how you send requests that's how you accept requests and yeah that's how we use add consumes annotation so in this video we have talked about ad consumes so yeah we have talked about lots of stuff right we have seen how to how to send a get request how to send a post request in fact we have also included database now where you can fetch data using using XML or JSON on the client-side the thing is I want to achieve some more features now so when I say more feature that means go back to my database which is MySQL here and I have done some changes in my code I mean in database first especially I have dated this ID 1:05 just to check something and I was trying to update this name so initially it was my ankh now I just renamed it to my a my you with the help of this this update query so can ditch can be achieved this too with the help of restful services I mean at the height will send a delete request a client may send a update request how can we do that now if you remember we talked about get request now get is very simple if you want to fetch data of course you will be using get if you want to create a new resource on the server you will be using a a post request now for update we have a special type of request method which is called as put again there's a main difference between post and put again we'll discuss that in one of the theory video but then put means put is something we're using which you can update values and post is something with the help of it with the help of which you can create a new resource we have already used post right now it's time to go for put in fact even before starting with the resource what I will do is I will go back to my Eclipse and here what I will do is I will create in the repository especially I will create one method for update of course right we want to also go for update as well so what I will do is I will copy this now this time you have to go for update so of course the method name would be update here so I will say update and the query here would be it will be different it will be update in fact we can I can just copy this code so copy and paste again we can call this as code reuse right so let's say update alien set name now I don't know the name so this will be question mark so you can put this question mark here I don't know the point so it will be question mark here and I don't know the role I'm in the ideas that will request so now here we have to replace those question marks right the first question mark here is a name so this makes more sense if you have a name so such string question number one it will be named the second question mark is point so it will name which will make this edge to this will be points and the third question mark is the I so this should be three right again we could have written that in sequence so let us do that here so it means cop cut this part and paste it here so we are going on in sequence one two three one two and three record all this question marks and that's it this is how you update the data but then how will you send data from the client okay to do that we have to of course create our this sauce here now how do I get a source now I'm pretty good source for getting an alias or the updating an alien's of course we have to create a put request so I will copy this code and paste here now instead of using a post request we have to go for put request because we are updating data so she put and the method name would be let's say update alien so it's the updated and it will update the data but then update need to be done only when you have that particular data in the system right example if I try to update something and if you find if we don't find data then of course you cannot update it right in fact I will I will check that after some time the main motor here is I don't want to create the thing I want to update the thing because I don't want to I don't want to create I want to update again there are some there are some restrictions which you have to follow that will see bit in some time but time being what I'm doing is I'm using a put request now using put request you actually achieve updating okay now there are some people they do this debate that using put you can create a new resource on using post you can update resource our first of all to be specific post is for creation of his source put is for updating but then you can use it while you can use it anywhere right it will not make any sense because for you it will work the only thing is if you have someone else and if they are checking your code it will be difficult for them to understand what you have done so let's let's all follow the same standards right so let's use put for update and let's use pose for creation of data okay now this sounds good let me just go back to my I have to be sure the server is already done let me just go back to my postman or postman now here I want to go forward up let's let's go for post first I will go for a post request where I will create a body I can have this time I will go for 1:05 will go for name Praveen and points 85 so these are my values and I don't want to send this equation to send a request for alien right that's my URL for that and let's say send the moment you do that you can see in our database we got one zero five and we got okay let me refresh this code you can see we got one zero five we got Praveen and we got 85 now I want to update this data so I want to make it I want to change this once it afford to my uncle Ian how do I do that so to update of course we have to use a put request now when you send a put request of course for aliens you will say 4 1 0 4 I want to put name as my my UNK so we say this is mayank and will will have a points okay what should be the points let's let's go for 90 points so the initial value is my U and 80 let's go for 90 points and let's go for my UNK will this work let's try it out the only technique on sand and if I go to my database that you can see there is nothing wrong there if I select star and alien you can see we got 1 0 for my UNK and 90 that's how you can update data not just have one query what if you don't have a ID which you are specifying so the request which I am sending now is ok where is my postman it is here so the data which I'm sending is let's say once your for what if you try to update 1 0 8 or 1 0 7 now what will happen because we don't have any data with one zero seven right so let's say for 1 0 7 I want to change this name from my you two mi-jung to chico that's his nickname his my nephew actually and then let's say point is hundred because his my nephew he has more knowledge than me so of course this should work so the moment add on this code ok you can see it worked something something happened you are sending up put request and let me just go back to my database and he finds the next odd oh this is not working on the server side you got it on the on the server side you go you can see we got data on the server side and we have also tried to update but the problem is while you are updating it it is not affected because we don't have any data which with 1:07 now what is my intention here is see if it is not that if the data is not there it should create a new resource so using put I have two intention here the first intention is either okay you can also return the data you can also return the response to the client hey we have not got any ties 1:07 that's one way you can do that I can will do one of the in one of the video how to send a response code but then let's say we have a scenario here where if the resource is not available I want to create a new resource in that way what you can do is you can check so while during this source you can check if before first you have to fetch the value right so how do I fetch value so we can use this get a lien method again so that we have this gateways that method now we have gate alien so what I will do I will I will check if repo dot get alien and if I pass the idea will say a one dot get ID and if I if I fetch an object using this if I compare this with if it is equal to equal to null okay so if this is equal to go to null well we'll say okay so in case if it is equal to equal to null what is that okay so if the object is not available so when I say get alien by passing that ID that means that the object is not a bit about the database in that scenario it should create a new resource so I will say repo dot create a new resource right and it should work now let's try it out let's try let's try I'm very hopeful for this now let me go to our postman and if I click on send okay something is happening on this outside it's saying it is updated but then let's verify if I go back to my database and let's select star o which not working what just happened okay so it says get oh it will not return you null right that's so that's an issue it will not return you null because when I say get alien I'm actually passing a new object oh how do I verify oh okay so what I'm going to do is okay let's let's try to apply a programmer mind here so when I when I try to update this data and before before okay how do I verify if this data is available or not so if I say okay so okay okay that makes sense so if I say get a DNA by bypassing that ID if this gate alien ID will say get ID is equal to equal to zero because it will be not it will be a idea right it will be zero so if I don't have the data in the database it will return me a value which is zero is that the case yes it will return you a zero value okay now let's verify let me just restart my do the same thing let me just say send and if something is happening on the server side because misawa is loading interesting some time okay now let's get back to database and done you see that it is working now so we got 1:07 chiku and 100 okay that that's easy so that's how you can update that so you can you can create so if if the data is there it will update if it editor is thought that it will create it's that simple after watching all these videos you might be thinking rest is damn powerful right because now we can use all those HTTP methods to achieve this we can use get to face the values we can use poast to create a resource we can use what to update the resource now the next operation would be you guessed it right we have to go for delete operation so if I expand this you can see we have one more method which is delayed again using rest you can use this form you can also you also use other the thing as well but then this for are very useful so we'll use delete now now of course you know why we will be while be using to it because we want to date a resource so what I will do is if I say hey I want to I want for I want to delete the alien with an ID which is 1 0 1 so when I pass when I said I delete delete request for alien 1 0 1 if I click on send it should delete the resource ok should return the day source as well it should also daily resource but as you can see it is not working of course it will not work because method not allowed right is because we don't have that method on the server let's go back to server now and let's create a method which we'll use for deleting so I will say public and I also want to return what we have dated so I will say alien and I will say delete delete alien you know the name should be killed in him that will make more sense right so let's go for kill alien here I know that's warnings but then it will go here so we'll say kill alien and in this you have just just have to pass an ID the way we have done in this one which is get alien so we were using this at idle right the same thing we have to do there so I will copy this and I will say delete a kill alien by accepting this value which is ID and you have to also specify the request type so ok if you have to also return the value right well you like older than sometime but then we have to return data right so we'll say I'm going for a delete I am going for delete request with a path alien so I'll say path alien and in this part alien we have to say slash and we have to also mention the ID so what do I do you mentioned the same thing we have we must have done forget right you can see for gate alien also we're using the same stuff the only difference would be instead of using a get request you have to go for a delete request so it's a delete so either I did it and here we have to return the Aiden object now so how can we do that so first of all you have to file a get method right so I will use a get repository so I will simply use that get method here so is that get code it's here it's here it's here yeah so I will do the same thing I will say a report dot get in an ID in fact we could have we could have typed that right let's go back there and say this let me say alien a equal to and let's return the value let's say return a our job is done but then this is not the only thing we want to do we just want we just don't want to fetch it we also want to delete it now dating is very easy you can simply say repo dot delete and you can pass the ID but unfortunately will this work not before one before you went deleting we should verify if do we have that data so we'll say if a dot get ID is not equal to zero then only we have to do it rather otherwise we don't have to delete that so only when you have two data in a database then only we should delete that but casada is do we have this MIT do we have this method there unfortunately we don't have date meter inside the repository so let's create one so I will click on this and let me create a delete thing okay now what should be the code and I mean you know that we have tried SQL query so again I am damn lazy so what I will do is I will just copy this code and I will paste that stuff here now the query for me would be delayed okay what's the delete query so instead elect okay then it from alien where ID is equal to comma I don't know the ID so I will just say ID is okay this one as well so I will say a1 ID would be I okay I just need to remove this extra spaces here so what we are doing is we are firing or delete query and then we are specifying that ID which is whatever idea passed and then it will which it will hit the particular ID of yours okay I hope it will work now let's go back to my resources everything seems good I want to delete a particular idea right so I will go back to my postman so I hope it would work I will do it once in a one and it work and you see that it is fetching the heater oh but the important thing is is it working on the database let's go back and say select star from alien for this and you can see alien harsh gone now we can do it once there are two one zero three we can do anything you want if I try to delete the ones your nine which is not available there and if either one you can see it is not retained data of course it will not return because we don't have anything with one zero nine we should have signed the the response code right maybe the data is not available okay we will try to do that so this is how you delete data again how to send response code that's a different thing we'll talk about that in one of the video but then time bin you can you have seen all the methods of HTTP we have worked with get we have worked with post delete and update or we just put time to make a quick recap so till this point what we have done is we have worked with different methods of HTTP right we have worked with get requests we have worked with post fort and delete and you can see we have used avi also we have also used a postman as a tool to send the request to the server and everything was working fine right and we have enjoyed the videos till this point now if you observe this scenario here what we are doing is saying in terms of recap what we are going to do now is let's try to understand what are things we have done so let's let's do with the get request so let's say if I use a sends a get request and a user user intention is to fetch all the aliens here for that we have it on this method which returns all the aliens now as you can see we have get alias method now this method will be called whenever you request for the aliens so this path annotation will map your request type so we did when you request for aliens this was a map it and you can return all the aliens but again in which format you're returning them so we are returning them in a nice format now you might be thinking can you can you really send a least format to the user because user will be a client right it may be a browser it may be another application so of course we have to convert this Java object into some format which is understandable or with all the application and that's where XML and JSON comes into picture okay but that question arises how will it convert your Jai your object into JSON format so we don't have to worry about that might remember in your libraries we have added one of the dependency okay instead of opening this we should be opening our XML or not this XML DePalma XML file now in this table we have seen there are some dependencies using which you can convert and luckily or we have Josie here now Justin says hey user or hey programmer you just have to give the object it is my job to convert that object into XML format but then what if you want Jason so for Jason we need or what we need one more dependency which is moxie right again we can also use Jason we can use Jackson if you love it but Josie says hey why you have to add X live is when I can provide you those lab areas so they are using Jersey library here but if you have a choice right we can also choose between Jason and Jackson and also of this one which is Moxie okay so we have added this dependencies using which you can convert your object into either XML or JSON it's just that when you return you have to mention what it reduce now your application says hey I can produce a JSON data or I can produce a XML data now depend upon what I use a request for you if our user is requesting for XML you will return XML a for if a user is requesting for Jason you will return JSON right now that is called as content negotiation so you are negotiating a content with the user and that is perfectly fine so that is about your get request right now we also need a post request or is that this is here's a post request let's say if you want to create a new alien in fact what we have also done as we have connected this thing we did a base right so if you create a new alien that alien will be stored in the database and to do that we have used a repository here and in this we have done with all the configuration again you could have use data source here if you have a different implementation for database you can do that our application is so this code this file here is not concerned about what about database code you have written there you can also fake it in fact if you remember the first few videos we were faking that we have a database right we were using some dummy data at any storage but now we are we actually have masculine right everything was working you know we have a post request we can use get requests we can use put until it so we have to do the same thing it's just that you have to change this annotation from put delete we have a post and we also mentioned Park now why would you eat apart we have already specified path here right now to get that link example if I go to my postman and you can see we have aliens here right if I say I want one particular alien or if I want a Arian to be updated or if I want it to be deleted let's say I want to delete this once your - we just have to go here just say delete request and says and it will be deleting it will do it will get deleted right that it works so everything was working so why I mean what next we are going to do see we have jealousy is not the implementation right and if you are working for a company there's a chance that you might be implementing Jersey or maybe some other implementation now one of my favorite implementation for restful Web Services is spring spring the worst because Spring Framework is a new framework which provides you lots of features right if you implement spring MVC spring code it's an awesome isn't it's not something booked to work with so what we are going to do now is we will be walking but the same stuff the same get request same post request for delayed all the same thing with it is of course but with the help of rest the spring list right so we start with spring rest and let's try it let's also add the database feature but that will be with the help of hibernate or maybe some JPA right so let's make this more exciting let's also a database and all those stuff but then before going for all those things there are some prerequisite the first prerequisite you need to know is spring code again it's not that difficult you can just search my channel or you can go to description area and find the find the videos for spring code those things we you need and that's it everything else will I will teach you how step I will teach you everything step by step in fact if you even if you don't know spring code that's fine because when I explain you about those spring cool features while in spring lowest you you will understand you will understand all those things but you don't have to worry about that okay now we of course we are going for Spring Framework right the second thing we need is all the using a different IDE now RZR we were using Eclipse right again you can continue with Eclipse but then it is always better to use advanced one especially for spring teamwork we have STS as a IDE you can see we have STS here STR stands for spring tool suite which is specially designed for spring based applications again you can also use eclipse here okay it's your choice but then I will go for STS and you can also learn that right so why don't you let's expand our knowledge box and let's also learn STS now question arise from very well download that so I will show you the down okay let me show you if you go to a browser let's go - okay let's go to sts this is the official it will take it off in the website of STS which is here spring tool suite you just have to download STS from here there's option of all versions you can you can click on this if you depend upon which machine you use it will automated you take your OS and it will download that particular budget for you you don't have to worry about this so once you click on this it will get downloaded you have to just you just have to unzip it and you can use it now you might be thinking oh we are using a new IDE now we have to learn lots of thing you don't have to worry about that because STS itself is based on Eclipse Eclipse platform so if you're using STS internally you are using Eclipse right so let's go let's start with that in the next video we'll talk about the first code with spring rest now when you open s tears it will ask you for the workspace you want you want to work with again you can create a new you can clear new workspace odd you can work with the same workspace which will be which me were using for eclipse ultimately springiest years or STS itself is eclipse right now you can verify that when you go to STS and if you click on I have four tests years you will get an option you will get that this works on Eclipse platform specifically Eclipse neon three so the version which I am working on maybe if you are using a new version it will be having a latest version of New York or maybe Eclipse okay so the if you remember this is the project which we have worked earlier right now since I have this project in Eclipse I have worked on this project in the existing workspace I got this project here now what I will do is I will create a new project so to get clear the new project now this time we are going for a spring project okay there's an option of spring boot in spring it's a new thing again you can go with normal spring MVC or you can go with spring boot now just to make these things easy for you I'm going for spring boot because spring boot says you don't have to do much of the configuration in fact if you're going for spring MVC there are lots of confusion which you have to take care Springwood says hey don't worry you can charge the I can start with the project and the way you can do that is by saying new so I click on your project Explorer and say new and create oh okay so I'm not getting the option here so I was right like I will say oh can you see that I'm not getting the option of spring project here so if I if I see I don't have a spring project option we are using STS right so we have to make sure that you are using a spring perspective so if I click on here you can see we have a spring perspective you have to click on that and say ok now you are in spring perspective now if you right click and if you say new and if you click on spring start a project now you can see we had we are getting option of spring startup project now this is basically your spring boot project so if you click on spring starter project ok it will give you the service you'll again we'll talk about this in some time here I mentioned my rest I will mention my project name as spring boot less because this is not just a rest a project this is a spring boot rest project and will mention the group group ID will say kaamdar telesco and will mention here condor telesco okay everything is set let's click on next now here we have to select where because we are working with web right so we select web and nothing else just click on next and then click on finish the moment you do that you can see we got a project now this is a spring boot project and you can also see s there s simply means a spring project and at the end you have boot this is a spring boot project if I expand this and if I go to my palm dot XML file its let's see what exactly this file is if I go to Pont or XML file you can see we have we have spring boot dependency here and then we have spring boot start up start up web so this is the web project basically this is a spring boot web project you can also implement MVC here where we are going for spring rest let's close this and I think else if I expand this mean if I expand Java for example if I open this spring boot rest app you can see we have a main method here and you can if you run this code it this will this will work now one of the awesome thing about spring boot project is when you select web and if you expand your memory dependencies of course you will beginning the some web lab it is there but with those web libraries as you can see we have spring and we have spring MVC here with this we have one important thing which is this one can you see that we have an option of tomcat embedded now what exactly it means let's say if you build a project on your machine and you your machine in your in your machine you have Eclipse you have Tomcat installed everything is set up let's say if you are going to your friend's house or if you have a different machine now if you don't this project on on the other machine what you do is you have to install Tomcat as well you have to download Tomcat you have to configure Tomcat but Springwood says hey don't worry why you have why we need a external Tomcat but I can you embedded target that simply means you don't need a external server what I will do is I will stop the external server you can delete just delete this if you want I will simply stop this now what I want is I want to create a ok I want to create our URL using which I can access some data now we have a choice of creating a web page here from which you can access data but that's not a good thing right we are building a restful web service where you don't have to mention a page we can request the data using postman so what I will do is I will first of all on this code and of course this will not work this will work maybe let's try so I will say one as one as oh now how do you run this you will have to run this on server no this is the spring boot project right so anybody want to run a spring boot project you have to select spring boot app just click on this now that's your spring boot app which is running and you can see in the console here it is running let me just minimize all this extra stuff which we never use anywhere and we got an error it says the port is already busy oh that's bad is it the issue yeah that's the issue because I already have my Eclipse running and running straight here let me close my Eclipse and let me just make sure that this is X this is stopped or maybe I will delete this tomcat server from here to be on safe side anyway so I will write it again I will say run as run a spring boot app it should work now and you can see again we got an error what's wrong who is using the port number 80 80 that's bad really bad ok so what I will do is this will not be an issue in your machine ok what I will do here is I will change my port number in case if you are facing the same issue you can change your port number you can just go to applications or property file and you can say Sawa dot code again my intention here is not to teach you what is what is Springwood and how to configure this stuff or you can learn that in a different cause of spring projects right but then if you want to change your port number that's very easy just go to application properties and you can change your port number to eight zero eight one now let's run this this should work now otherwise I will call this project I will say run as one a spring boot app buts running OH finally it is started I don't know what which service is using maybe the Eclipse is not a properly closed maybe this one is not properly closed so but this is working you can see we are using a port number which is 80 81 okay now what I will do is I will go to my post man I will say hey I want to request for 88 you want and it will say enter you got nothing of course we will not get in you will get nothing right but what I want is I want to fetch all the aliens so obviously I get requests I will say I want to fetch all the aliens if I send a sender if I click on the send request it should return the data okay now how do I do that it's very easy we just have to create a class and that class will is responsible to return the data the way we have done in rest or spring the rest jersey right I will mention this is my alien controller now technically this should not be a controller this should be a resource that makes more sense right this is a resource I click on enter now how do you mention that this is a spring project I mean how do you mention that this is a spring a controller or the resource now to do that we have we just have to use and the annotation which is called as rest controller so whenever you want to implement restful api you just have to annotate it with rest controller the way we have done in injure say we have used ad path we have to use at rest controller here and this you just have to mention a method so we say method name is public this return a list of aliens again we can buy database code but that will duel after some time we say list of aliens and I will say this is get aliens and this will return a least now to return at least we have to get on this first right so I will quit at least of aliens here I will say least all aliens and I will name this as aliens equal to new aliens but the problem is we don't have a Indian class here that means first you have to create a scene in class oh now I am bit lazy I will ensure of creating the class I will just simply copy paste this again you can call it as code reuse like the same alien class let me paste it here now problem solved is it the problem solved yeah problem solve it we don't have any show with aliens now and I will simply return the aliens but unfortunately this aliens is empty so I would say alien I will say alien a1 equal to new alien and I will insert some data for this I will say a1 dot set ID we'll mention the idea is 1 0 1 I will say a1 dot set name I will set the name as naveen and I will say a1 dot set points I will set the points as 70 ok that's my points let me just use one word object I will say copy this and create one object for me this will be a - a - a - a - and this will 1 0 - I will name this as Priya let's say points is 80 once I got this two objects - alien objects let me add that in a in that least I will say aliens dot add let's add a 1 I would say aliens dot and let's add a - ok so we have added this two objects everything should work now now ok so far before before running this code let's let's I have this question for you will this work I think about it and let's see the answer now so let's restart the application now this is one thing you have to you have to remember when you work with spinning boot don't start your project once again if you run this project once again it might give you an issue because Springwood project has emitted Tomcat so it will not restart when it on this project you have to click on restart to restart the project or they start the Tomcat server let's go back to our postman and say hey postman send oh it's still not working of course it will not work because we forgot one of the annotation here which will map it right they remove the path annotation there here we have to use request mapping because you are mapping with request right so we have to say the quest mapping and I will say a request mapping with slash aliens or we don't even need slash there so it's the alien so whenever you get a request for aliens this is what you have to run let's you start the application and let's go back to our Tomcat oh sorry postman and let's say it one it worked can you see that we got decent data we got you are the 1:01 the first alien to be caught the second alien as well it's so simple right so instead of using Jersey we can also use spring boot now the thing which you have observed here we have added no dependency nothing right in fact to be going to data in adjacent format and doesn't awesome thing what Jason we don't have to actually add any dependency everything is there but who is doing that in in your Jake in your Jersey we have Jersey converters white which is mock see here if you expand this now in dependencies you can see we have something called a Jackson now Jackson is pre built-in in spring rest project or spring web project so this Jason is just George it's Jackson is responsible to convert your list of objects into Jason right so it's an exciting tool to work with again in the in the subsequent tutorial we'll talk about how do you use post request put request and a net request so the get request works perfectly right but then think about this if you remember your your address with Josie we have and if you have specified the annotation which is at at get right here we can mention that in the in the method itself we can specify a comma here you can say method and that get odd you can simply use at the rate get mapping so when you say get mapping it means you are using a get request you can also use post mapping put mapping and delete mapping for other thing again we'll see that once we continue with the videos in this video via the most important part is the database connection now thing about this if you want to work with data base because I want to fetch all this data here from database so of course we have to we have to connect with database we have to create a repository right so we have to say in repository right and we have to specify all this thing example there to specify all these properties and of course somewhere we have to specify all these properties but you have to mention your URL your username password the URL and you have to mention this methods we need a method to find all the aliens after right you have to mention the entire stuff here you have to mention the entire this entire code right for one alien you want the entire code for create alien this is the code for delete alien you have a code write software for everything we have a code there but what if I say you have easier way now how do i implement that now first of all I'm not using normal JDBC here of course we'll be using daily BC but then we will be using a layer of it which is called as a spring ORM now we also have a concept of JPA again if you're not familiar with JP a concept I would recommend you to learn about that as well I do have a hibernate playlist if you if you follow follow that playlist you will understand how exactly JPA works or how exactly happen it works again you will find that in the description area but even if you don't know that's fine I will we can ignore that internal part of it we can understand the overall structure what is happening what I will do here is first of all I will add a JPS support in this project and as you can see in my pom excel file I don't have a JPS support so we have to add a JPS support how do I get that how do I get a spring JPA support for that you have to go to my depository and search for spring boot oh what's that spring boot J P a now if you search for spring boot JPA your your first research in this you have to make sure that you are selecting one point five point four because that's our project version if you'll see that's the project spring boot version we are working with so let's use one point five point four copy that and paste in your dependencies now you got a spring boot JPA support now once you have added that and the moment you done this project even if you are not changing and stuff we have just added dependency right let's try to run this code let's say run as spring or will you start the application the moment we start the application you will get and either okay before solving that data I want to tell you one more thing let's say if you want to create a project and you're sure that you will be building a project which will be having a rest and JPA both so while the while you're getting the project at that time itself you can mention that you want to work with JPA so you don't have to manually add this dependency again you can it's very easy you can just right click here and say neo spring Strutter project and you can mention it at the default stuff here you can mention I want a web stuff so you can mention web and you can mention JPA so you don't have to manually manually do the manually add a dependency but let's say you have added this dependency right anyhow but what is this header now it says cannot determine the embedded drywall class for database type none what what is this error now what is what happens you know your spring boot project is very intelligent all you can say very smart it says hey programmer I can see that you have added this spring JP dependency but you have not specified the properties Oh spring understand other things so what you have to do now is you have to specify those properties here but which property have to mention now all those settings we have to do here so it's very easy actually you can mention URL and it will give you a list here right and inside this list you have to be you have to select spring dot datasource dot you either because we are using a datasource concept here and and this will mentioned JDBC : masculine that was slashed localhost the same the same property have to mention in normal JDBC right so omission 2006 slash the database name I forgot it abbis name which we are working with so if I go to my alien depository the database is less DB right next you the same thing we said West DB the next thing you have to mention is username so I will say spring dot data source dot username we mention the username as root and I will say again spring dot data so stored password I will mention the password as zero and the last thing is the Java class will say spring dot the same property you have to mention in the any JDBC okay don't there's nothing new this is spring data source dot it is driver dot that driver - class - name you didn't have to buy heart this thing okay it will be pop-up it will pop up automatically so we'll say calm dot MySQL JDBC driver right so we have specified all these properties when you work with Indian repository here the same stuff you see that the same stuff and that's it our job is done I think let's restart the application again this will not be fetching from database but at least the a patient should work oh we got an error now it says it is not able to find Karma's ql dodged it is a driver because we forgot to add mysql dependency let's add that as well let's go to mammal depository here and let's say hey I want a master connector in fact you don't even have to do this if you know that you are working with spring boot JPA web and mask you'll while cutting the project we know so search for my oscar it will give the dependency but since we have not done that there let's do it manually here I will say I want I want to retain in version which is four point three point nine I can you can go with anyways any which you love okay we have I did the master dependency as well let's reset the application now it should work and you can see there is no error right there is no error now but again it will not work on database because we are not fitting database how do I do that here so in this project if you want to make it work within a base you have to go to your package and say hey I want to create a new class now what if I say you don't have to get a class you have to pay the interface you went to thinking okay we have to first get the interface then we have to get a class and then we'll implement let's see how it works we'll say alien repository okay and we just have to extend or will it click on finish we got the interface here right we just have to extend with one of the one of the internal one of the internal interface called Ascot repository where you have to mention whatever class you're working with and the primary key whatever parameter you're working with so I'm working with a primary key in teacher now in your master database you have a primary key which is this is not our table let's use rest dB let's verify what you are working with and select star from alien okay so this is our data right now this is your primary key which is integer so ever to specify integer here and now you might be thinking okay we have to define all the methods here create get update and delete and then we have to get one more class which will implement in it this interface right that's how Java works but what if I say you don't have to do any of this stuff let's give this interface empty and let's not get any other class and in here in this resources we have to create an object of a de repository will say in a repository and we'll say a repo that's it and here we'll simply write act Auto wire now this is a spring cold feature if you want object you don't have to get the object by saying new or alien repository you can simply don't act or to word it will search for the implementation it will give you the object you don't have to worry about this and here let's not create the object by ourselves simply say the data will be coming from the data will say report dot repo dot find all so we have to use internal method which is find all okay this works okay so we can type the state I not work with let's change something let's let's let's run this for let's see what happens the moment you run this code you can see I am using an app on now and oh we got an error oh it says it is not a management if you're using database we have to also make sure that this is an entity because we are using a concept of hibernate and in hibernate we have to mention this as identity and not just hibernate we are talking about in general JPA right so in JPA we have to mention identity and add ID so i ID will give you a ID will make this as a primary key and this class is entity class ok so we are learning from a doze now let's go back to our postman and let's say hey postman fetch the data and you can see that we are getting all this data and these things are coming from database right so that's how this that's how this stuff works here you just have to say find all just typecast it with little east and everything will work fine you don't have to use any Traver stuff everything will work fine right or something right so this is how you use a spring rest with database with the help of GPA so it's many cap what is happening here the magic thing here is we are not getting any class which implements is interface and it still works right so this thing is done by spring for you now there are lots of people they say hey string is not that good now what is this spring is awesome spring is not good it is awesome right it will give you all those extra features which which you need all right so that's the magic of spring boot again will once we continue with the example in the next videos will you will get more idea about it but I would recommend you that before going for the next video do practice this because this is something which you will understand only when you do it because it's the magic right and magic when I show the magic you will be thinking oh this is awesome but the real thing you will understand when you try it by yourself so try it by yourself then you will see the actual then you will understand the actual stuff
Info
Channel: Telusko
Views: 676,393
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: BZi44GOD8kY
Channel Id: undefined
Length: 154min 39sec (9279 seconds)
Published: Sun Jun 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.