Spring Boot Tutorials | Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Deven 20 and in this video we'll talk about spring boot now before going for spring boot let's try to understand what is frame framework and by this so famous now to understand this let's go to ear 2000 so in that time if you want to create a project in Java for enterprise you will be using lot of Java EE features and one of them is EJB now EGP was quite famous and it was one of the best technology available at that point and you can actually run a features right like message eggs then look up for the entities the only problem is you know it becomes difficult to manage them and then of course you are working with entities so they are heavy and that's where these people they came up with the concept of pojos where you can achieve the same features with the help of Pujo and they have included that in Spring Framework now spring framework provides you this feature of working with pojos it provides your feature of dependency injection and we can work with a opie so there are so many features provided by Spring Framework so it becomes a framework where you can achieve any business purpose and the best part of Spring Framework it can integrate with other frameworks as well like hibernate and stoats and now we know why spring is the best framework now this is one problem now we talk about Spring Framework of course you will be building big applications right and when you say big applications you will be needing some external Java files so that's one thing you have to work with Java files the second thing is the configuration and trust me when you're working on the enterprise application you need to add a lot of configuration or you have to convey a lot of stuff and that's where the problem starts you know because if you're working on a project you want to focus more on the convention you're coding but basically not on the configuration because we are developers we don't want to spend much time in the configuration I'm not saying we can like skip all the configuration but we focus right to focus more on the coding part now the thing is spring is good but we want to focus more on coding and that's where these people who created Spring Framework they thought okay know what why don't we create something where as a dollar per they will focus more on the convention and that's where spring boot comes into picture now again spring boot is not a replacement for Spring Framework because as a developer you are still using Spring Framework okay so it doesn't report it something work you are still using the same framework but in between just to help you we have spring boot because spring boot says hey developer I know you want to work on a project and spring is the best framework the only thing is they said there are some Chow files which you have to add and there's a pod fiction you do and don't worry and they're for you so spring boot says I will give you dependency and I will give you the configuration awesome right now the main idea of spring boot is to give you a production ready application so the moment you create a spring boot project you don't have to do any configuration it is runnable and you can deploy it on the on the pressure server it will do nothing but it will work you don't have to do any configuration and the more features you want you can add some features and for those features you can do some small configuration but all the basic convictions will be done by spinning boot ok now there is one more thing you know let's let's save yeah we are not working on spring boot we are working with normal spring project and we want to make a web application and if you deploy the application what you will do is of course you need a server maybe a virtual server or a cloud server and if you have a server you need to install a Linux server ok so that's done so we have a physical server then we have a Linux OS and on that OS of course you'll be installing a web server or maybe a app server it may be Tomcat or the GlassFish and then on that Tomcat we'll be deploying the value file which you create for web application so so many layers right and nowadays we are moving towards micro services and we want to make it bit easier for us right and that's why spraying boot says hey you don't have to actually do all those things by yourself it's bringing boot it provides you a embedded server which means the applications and files which only create ok we will not be making bad files now we're making a jar file and inside the jar file will be having Tomcat oh we'll be having Tomcat inside a jar file that's why that's an embedded Tomcat all embedded server you can say so spring boot says your jar file will have a embedded Tomcat which means you can run your project and on any JVM you don't basically need a web server there now spring would provide to so many features two of my favorite is the first one is starter projects because if you want a project let's say if you want to make a web application spring will say a spring boot will say okay we got spring boots Tata web it will give you a web project and if you want maybe if you want to work with JDBC spring boots says ok don't worry we have spring start of the spring boot starter JDBC you can simply use that and it will give you a ready project what about the configuration spring boot also has spring both auto configuration which will do the confusion for you so that you can focus on the convention protein core not on configuration so let's say if you want to do some configuration maybe you want to modify conflation maybe you wanted to some manual configuration is it possible that's just spring boot provides you the XML files and the answer is no spring boot will not generate any XML file for you okay because there is no XML file then how would you modify if you want to do some quantification and that's where we have a special file which is application toward properties you can add the properties which you want there is quite simple and in this video we'll talk about dependency injection now if you are working on a project let's say if you are working on some advanced project of course you will be using some design patterns and one of the design patterns which we come across is dependency injection doesn't matter which language we use maybe Java C sharp PHP we have this concept there but what is so important so in fact we have this questions right what is dependency injection and why everyone want to use it in fact if you talk to some expert in industry people who are working from a long time they use this concept by default you know for them it's like a normal thing but for new developers it's a new thing right for them you need to understand how what is dependency injection and why to use it now how to use it that we'll see in the practical but then how do you why do you use it that's a question now think about this now when you say dependency in in software programming software what we do is we build good right we build objects if you're working with object-oriented programming so we build objects right and those objects are dependent some on some other objects so we create this object graph right so one object is depend up on some other object and that object depends on what that object to give an example in real life let's say if you want to buy a laptop if you want to build a laptop let's say you are a laptop manufacturer now in this case in laptop you have certain certain parts right we have a ram you have a hard drive you have a screen and of course what makes a good laptop is good parts right example if you buy a laptop from any company the same company they have a low-range laptop and a high range laptop what changes is the type of hard drive we use the type of RAM we use so of course all these components are not built by the same company so let's say if you buy a Apple machine of course Apple will not be building all the step by themselves they will be buying it from some other companies maybe this screen of MacBook is from Samsung maybe the hard drive is from Hitachi maybe the RAM is from Sanders so all these different company they they help they help one company to build a project or build a machine now the same way if you want to build up project and if you want to let's say if you have an object and that option is depending on some other object in this case you will not be building all this stuff by yourself of course you will be having classes for that right so in Java what we do is we first create class and then we create object now to get an example let's say we have a class here which is class let's a laptop and in the inside class laptop we need a object of hard drive we need her object of RAM now how many do it of course you will be saying new right and when you say hard drive of course you need a punched class right and that lets say we have hitachi hard drive now let's say if you build a macbook with Hitachi hard drive what if in future you want to change it because we want to achieve this concept of loose coupling right I am sure you know about this concept of tight coupling and loose coupling that's what we have learned in software engineering when you say loose coupling it means one optic is not totally depend upon other object it may get replaced example if you have a touchy hard drive in future you might want to use Samsung hard drive it should pass it should be possible right and that's why we use this concept of abstraction what we do is we say ok for each class let's say Hitachi hard drive let's create abstract class or an interface for less hard drive so when you instantiate it it will be something like hard drive ABC or hard drive obj equal to new Hitachi hard drive so that any future you can change it from Hitachi hard drive to Samsung hard drive it should be that easy but there's a question here we are hard coding it right we are saying hey we I'm going for a new with a hard drive and that's a bad thing because the moment you say hard coding the moment you say new you are going for tight coupling that's that's what we don't want so we want someone else to give me those dependency or that's important down here so the laptop object is dependent on the hard drive object that's a dependency so we want to inject the hard drive object inside this inside this laptop class and the way you can do that is by using some service which will be external service they will inject the dependency ok how it is possible so we have this concept of dependency Injection containers now they are responsible to create a object for you of course the object has to be created so they are responsible to create the object for you and then they will be injecting in your plus okay but wait to mention that I want to inject and how would you feel to create the object and that's where we have to do porn figuration now there are different ways of configuration configuring it example if you are using Java in Java we have Spring Framework and in Spring Framework we need to do a lot of configuration but earlier days we used to work with Spring Framework ways to configure everything using XML ok so that's that's that's how you can configure so create XML file and in the excel file you can mention hey if someone is asking for a hard drive give this object ok now when you say XML it means you can edit XML in future right so that is not a tight coupled ok but still as a job of Rama you don't want to focus more on XML and that's where in we have spring boot in Java using which what you can do is just we will be having some class as Hitachi hard drive right or Samsung hard drive so on top of these classes you will simply write add component that's it ok so you simply like add component which makes them dependent which makes them a component of a spring framework which will be generated as per requirement ok so those those object are ready with Spring Framework but what about this plus how will this mention that you want this object so on top of your hard drive what you can say is you can say Auto wired ok that's it your Spring Framework says or your spring boot with the help of Spring Framework says ok here someone is asking for hard drive ok so this class needs a hard drive and I do have a component there so I can connect them like that subtypes of Auto wired and just imagine so beautiful they are getting connected again the implementation you will say later but this is this is the main idea behind dependency injection right but why we need this is it only because of tight coupling the answer is yes we don't want to achieve tight coupling we want loose coupling plus there is one more important factor which is testing you need to test your software now we always test our software as a whole but sometimes we need to not sometimes always we should all we should also be testing your each component each unit now the thing is let's say if you want to test a laptop and laughed when you said when you test a laptop you should also test the hard drive but don't you think when you buy a hard drive like when Apple bought hard drive from Samsung they must have tested that hard drive right why you need to test the hard drive again and again so what you want is when you are testing a laptop you want to separate the hard drive right so in in fact in Jogja world what you can think is you have a class and that class is working with a database object now you are testing this class right you don't want to test the database object so you can create a mock object of this of this class only if they are loosely coupled so if you have a mock object you can easily test this class without affecting your database okay and to achieve that feature you have to make sure that your software is loosely coupled and that's why we have so many words right we have loosely coupled we have dependency injection so so that's that's so amazing about a pencil injection you just need to implement it so that you can test it better you can maintain it better and how it exactly works that we'll see in the practical video so once we have seen the theory of springboard so why do we need springboard and what features it provides it's time to create an application but before going for the application of course we have to do a software setup right now when I say software setup basically we need an i d-- ii which is integrated development environment now which ID we have to use here of course everyone have their own favorite IDE is available right example some people do love NetBeans some people love Eclipse now for spring which one to use of course we can use NetBeans NetBeans does provide you in build plug-ins for that you can also use Eclipse or one of the best one for spring itself is SDS now spring belongs to pyro tools so they have their own IDE which is STS and if you want to go for the paid version you can go for IntelliJ so if you just go to google and search for so if you search for IntelliJ just click on enter you can see this is the official website which is which will also JetBrains and you can download the IntelliJ IDE the only problem is it's a it's not free for spring so they have two versions one is the community version and second one is the enterprise one or the ultimate which you say community is free so if I click on download you can see we will be having two options so community is free but then if you want to work with Spring Framework you need to go for the commercial one which is ultimate and commercial yes commercial provides you spinning features so you can go for this one so people who are working in Java Java environment from a long time and they think they want to invest so that it will improve their productivity of course you can go for into deejay otherwise in this series we'll be using STS now STS is a spring tool suite this this is developed by PI orbital and this code this works better in if you want to go for spring boot now is it all very different IDE the answer is if you have already worked on eclipse this is almost similar because this is built on Eclipse IDE so when I click on download for this so I have Mac so I'm using this so I've just clicked on this if you are using Windows you will get a Windows option there and you can see it is downloading now so you can see site up here now once you have downloaded site up just have to click double click it if you're using Mac otherwise for Windows you just need to unzip it and it works perfectly so for Mac is you just have to do some extra stuff you have to shift the STS to the applications so that you can directly access from the application folder so if I if I view my if I see my application you can see we got STS here so if I click on STS and it will take some time to open as it as I'm doing it for the first time so once you have open STS it will ask you for the workspace now for this what I will do is I will say bring toriel's or spring tubes and click on launch now one it will it will open yours tears and all the projects will belong to that particular folder which here mentioned there and you can see we got STS open people who have worked on eclipse they know this this is the same right so if you if you run eclipse you will see the same screen now if I go to about STS you can see it says it is based on Eclipse platform so it says build version this one and platform is Eclipse oxygen so ultimately you are using Eclipse the only thing it provides you is shortcut to make spring applications now how do we create our first spring application it's very simple you just right-click it and you say new now if you are making a normal spring application you can simply select maven project and you can select spring otherwise if you are creating a spin spring boot project this is amazing we have spring starter project option now you will not get this option directly in eclipse ok so that's the advantage of using s tiers click on this one now it will ask you for this service URL now why all these things because when you cut a normal project in Eclipse its straightforward just say next next next but here now since we are using spring boot the thing is spring boot as I mentioned it is a opini ated project which you get that means the feature which you need it will give it to you you don't have to do much configuration as it also supports auto configuration so what happens is whatever project you need you will be getting that project from the official server so they have this service called as star dot spring dot IO so whenever you need a project you need to go to this website and download the project that means you need an internet connection to create a springboard project and of course everyone has it nowadays so the app which you want the project which you which you want to work on so the basic structure will be getting downloaded from the office website so for that we have to give a proper name to it we can give any name doesn't matter I would say first project and now you have a choice because you can select either maven or griddle that's your choice I prefer maven I would go from Aven here and then you have to mention your group ID you have to mention your version number if you want or defect I will keep it as it is I will not change it at this point maybe in the next video we'll create a new project with different names and then I will click on next now here you can see just giving you so many options and that's this is the best thing about spring boots you know the time I'm making a video we also got spring to point to earlier I was working on 1.5 but then 2.2 got some new features it does support spring 5 it does support reactive programming as of now we are not going for all those stuff so let's keep it wasn't which is there and then if you want to let's say if you want to work with spring JDBC it's very simple just type JDBC and then you can save this one the amazing part about spring is it does support auto configuration so if you have not provided any manual configuration it will do it for you but since this is our first video let's not get into that I will skip all those things as it is click on next and click on finish the moment you click on finish you can see your project is coming from the official server and you can see the progress here it is downloading from the official server and that's why it takes some time to build a project and if I expand this it is still into you know downloading phase so it will take some time to complete these steps what if you are not using STS what if you want to get this project what you will do so if you are using eclipse it's very simple actually so let's say you have eclipse and if you when you open eclipse how you build that project because if I say if I say right-click so this Eclipse okay so if I right-click here for sainu you can see you can see we don't have spring Stata project option in this case what you do is you just have to search for star dot spring dot IO this is a website where you can download the download the app so this this is spring initializer from where you can download desktop so you can see we have group ID you can name it let's say first project the same name which I am giving and then if you want a dependency you can just type it here as of now I don't want to change anything simply click on generate project and you can see it will give you this zip version you just have to unzip it and then in Eclipse you just have to import the project you will get the same thing which we got in STS so the only advantage or one of the advantage of STS is you don't have to go to the office website you don't have download that you will get that directly as as we get the option and you can see we got our spring boot project here again will not be doing any much code here it's just simple spinning boot project maybe I can print hello world just to show you if if this thing works and you can see I got some code here nothing to worry about we'll talk about this later I will simply type this out and here I would say welcome to boot that's it nothing much now how do we run this project now we can run this project normally as a main normally will run as java application but then if you want to use spring features of course you have to go for the second option which is bringing boot app let me just click on that now this is extra features which you get and that's why it will take some time to do and you can see regard the output as welcome to boot at this point we have not used any spring feature okay we'll do that later but this is our first application so this is our head start for the first code of spring boot in this video we'll talk about spring dependency injection and auto wiring so we'll try to understand that with some diagram so that we can understand what is happening behind the scene so to do that what I will do is I will make a simple spring boot application so I will say spring starter project and I'm by the way I'm using STS you can use eclipse as well the only thing is to download this app you have to go to start dots playing dot IO because we don't have this window in atlas by the way you just have to go to this website and download the app the spring boot app and then I will I will keep all this things default because I'm not concerned about the name yet I'm just concerned about the concept I will simply click on finish now we got our project here it's on the way so it does take some time to download this app so we can see that we got our project here this is your spring boot project in which you have the job folder you have resources you have test folder where you will put all your January test cases you have moment dependencies where you have all the required dependency for the project now what I will do is I will simply create a very simple in fact I will creating a very simple application here and I would say let's go back to so if I open this demo application you can see we have so you can see we have a main method here and inside this main method I am simply calling I'm simply saying run now I want to create a class here which is alien and the way you do that is by going or by saying a class and this class name it will be alien and I will click on finish I want a simple pojo here which is alien nothing fancy and it is alien I want certain properties the way you create a property is very simple you say private int I would say a ID and I would say private string a name so these are two things we need in fact we can also go for technology so every alien need has a favor technology right so I will say Tech and this for this three variables I want Gators and Sato so I will say soz generate Gator setup so I will say generate getters and setters for all these three variables click on OK and you can see we got a simple pojo now that the idea behind dependency injection is what if you want to create an object of alien here in this class in Maine so normally what we do is we create an object of alien by saying alien a equal to new alien so what we are doing here is we are manually creating the object by ourselves a neo but then the pence injection says hey developer you just have you don't have to focus on object creation you focus on the logic we will give you the object menu when you require it so here I will not say new alien then question arise how will you get this object now if you say this run method of which is which we get by default now this run method returns an object of configurable application context so run method returns object of this one so what I can do is on the on this side I can say configurable so his configurable application context I would say context equal to because anyway the run method returns that object so let's hold it now once I got this object your Spring Framework say simply say context dot context dot get Ben so when you say context get Ben it will ask you for the type of class type of object you want I would say hey I want a object of alien class and your your Spring Framework says hey you will get the object but can you trust it I don't know let's try now how do we trust it let's go back to alien and let's have a simple method here which will which we will execute just to see if it is working because if you if you don't have the object of course you cannot call a method so I will go back to this method and I would say system dot out dot println and I would say in show so if you get the object from this bring framework that means yes you got and it will it will be able to call show so the way I do that is by saying hey dot show so if it is calling sure that means you've got the object and you can see we have not done much of the configuration here we are simply getting the object of alien I mean we have we got a class alien and here I'm saying context dot get been alien dot class let's run this and let's verify what is happening now the put on this application we have to say spring boot app because this is not our normal spring the spring boot so I would say spring boot app and it's running it's running something is happening behind the scene and record an error I was expecting it so we got the error i it says nope being of type comm dot example dot demo dot aliens so of course we don't have this object but why what is happening behind the scene and what this method is all about what is happening here so to solve this problem let's understand what is happening here now spring provides you under that container so the way we have JVM we also have a spring container to understand that let me just go back to this diagram this page application here what I would say here is we have JVM right and on that JVM we have I'm inside this JVM we have a special container and this container is called as a spring container so I would say this is a spring container now what this container will have this hat this will have different objects but which object so the object which spring specifies those are objects in fact the object which we create here those are called as spring beans so whatever you have inside this will be called as a spring bean but how do we create a spring spring green and when they get created so basically we have two different scope when you create a spring bean first is the singleton scope and the second one is prototype scope in fact we have in spring we have different scopes available we also have request then we have session but then those things we use in web applications in core applications we use this to only which is singleton and prototype now how do we how do you specify singleton how do we specify prototype and what does it what exactly it means now if I go back to alien here so if you look at this alien this alien is not a spring bean here and even if it is spring been when it gets created so the container of spring bean or the container spring gets created on this line the moment you sell run it returns you the context this is where it initializes your container and in this container it will try to create all the objects but which object let's say if I have ten classes if you have 15 classes will you create object of all the classes not exactly you have to specify as a developer which object you want in this application I just have two classes what if you have let's say ten classes fifteen classes and out of ten classes I want to create objects only of six classes so I want Spring Framework to give me six objects other object I will take care of or maybe I don't even those objects how do we specify - Spring Framework hey I don't want all the object I want object only of alien the way you do that is by making a by writing ad rate components so we have to use this annotation called as a delayed component by doing this you are specifying hey spring a spring framework this is a class of which I want an object now by default the moment you on this application the moment you sell run it will create a spring it will create this thing for you right this will create this container for you the momentum in this application it will create one object of alien here so let me just name it as a one so it will create a a one object for you and this a one is of type alien so a one is of type a land class right so you can say alien class so the a one object here which is object related by Spring Framework is of type alien dot class it will be available in the spring container okay it will just it is just available the moment you say get Ben now let me just say get Ben your Spring Framework will say okay developer wants are being here of alien so because when you say get minute says okay DataPower wants a bean but of what which type that's where your that's why you're specifying here you're specifying here I want a li and type bean so it will check do we have a agent B in there hey we do have it in start we do have an alien being here it will get connected automatically so that means spring framework is injecting this object in your application so this object is created by spring framework or spring container you will get this object in your application that's what we say dependency injection right now it should work right we have mentioned cop we have mentioned component there I will sell run as again spring would application and this time you can see we have not got any error you got the output as in show so it is working so the moment you say component it will make it will make this as a spring bean and it you can use it anywhere now let me just add a twist here what if I say get Ben once more will it create two objects and how do we verify how many objects we are getting 25 that I will get a constructor here and I will say right like so sway shows constructor with fields maybe without fields I don't want any fields here I just want a simple constructor which will print something which will print which will have super doesn't matter if even if you have super there I would say object created that's what I want so I would say object created so the moment I go back now how many times it should print object created only once right because we are saying we are using what this object only wants let's run this code as a springboard and you can see which says object written only once what if I don't even try to fetch this if I say comment this section and let's run this now you can see we got object read only once I mean even if you're not saying get being you got object created because Spring Framework says hey even if you don't want the object we will give it to you because by default Spring Framework uses a concept of single turn design pattern which means it will give you the object pre hand I mean you don't have to wait for the object creation the moment you start the application it will give you the object okay what if I asked for this thing two times I will copy this code and paste it here this time I would say this is a 1 and here also I would say a 1 now what do you think it will it print object created once or two times so you can pause the video you can answer that in a coming section so I hope you have entered the value in the coming section let me just run this code and you can see it is doing only once so we are getting object created only once because in total it will create only one object because by default Spring Framework uses the concept of single tent design pattern so in singleton you get only one instance how about if I say prototype here so if I go back and if I say prototype and the way you do that is by going to or we have to use one of my addition which is I did scope so the addition is an attrition scope you have to mention what value you are going for so we are going for so we have to mention proto type now when you say prototype it means it will be using prototype now so you'll be using prototype now which means it will not create an instance by default for you the moment you ask for the instance when you say get been it at that point will create object for you so in total you will be getting two objects now so in this let's run this code and you can say it's a is object created object created two times because we are getting two objects now every time you say get been it will create a new object for you okay now the point is what if I don't even call this one we'll say come in this part now what do you think will it print once two times or zero now since we are using prototype which means even if you on the application you will not beginning this object so you will not get this object if you're using prototype so this will not be there if you're using prototype let's verify so you can see we have not got object rated or e1 once because it is per type so it will it will create the object only when you say get been quite simple right so this is dependency injection and these are you is component but the video is more about auto wiring what is happening when you say auto wire so to understand that I will create one more class here and I will call that class as let's say laptop because every alien needs a laptop right and that's how you can you can work on a project I mean not not exactly laptop computers also do some people write code on mobile phones as well so anything I think any any gadget will do here I would say private int L I D which is laptop ID and I would say private string brand I'm not concerned about other parameters here we can talk about price CPU speed ram ram conjugation and everything but then time bin let's keep it simple right like I would say so generate to get a status I would say generate getter and setter select all and click on ok and I also need a to string me the suit I'd file if I want to print something I would say soz generate two string and here I would create one which is public void let's say compiled it's a laptop has some compiler and then we want to compile it and it should say compiling so how do we do that so we say system dot out dot println I would say company you can write a complex code here but then just to make it simple I will say simply say compiling and now in Alien when I say show it should also call the compiling a hold on how do you say compiling here because to Paul that compile method Union object of laptop right so I would say private laptop laptop so regard laptop object here and just to make it work I also need a against before this I would say right like right like so's get an insider which is that getters and setters select all click on OK so we got a laptop unit as well and nothing fancy I just want to call show and from so I want to say a laptop dot compiled now what do you think will it work because we have a laptop plus we got alien class inside it in class we got laptop plus okay so we so alien class is dependent on laptop plus now now what will happen so if I go back to demo and when I say show in fact I will do it only once now because we have seen how do we call two times and also want to use seeing a singleton here just to make it simple again now what will happen so if I go back to demo let's run this 25 what is happening there and you can see we got nullpointerexception but now yl point exception is because if I go up we are saying laptop compiled in fact this is the error this is the line error so if I click on this line ok this in show and in citizen here so you can see we got laptop dot compiled this is the problem the problem is the laptop is not instantiated it because before using the object we have to say new laptop but again we are using spring framework right why I should say spring laptop and your spring framework is not even creating object of laptop so if you want to get the instance of laptop you have to say at a rate component so now when you know this application in your come in your container you will get one more object so I will say this is one more object here I will call this l1 and this object will be of type laptop dot glass so this is object of illenore class and this object of laptop work class and what I want so we call so when you say component you will get both the objects now will this work because we got the object right we don't have to say new keyword so the moment hood on this code Oh again we got the same error we got the null point exception but we got the object right but this is one problem a one object here is dependent on l1 how your aid an object will know that this element object available and how do we search it so we have to connect this tool we have to say hey alien there is some laptop object available there search for it how do we mention that how do we ask a lien to search for it automatically and the way you do that is by simply using a concept called as Auto wire so I will say Auto wire so the moment you say Auto wire it will try to search for the object in the spring container now will it work let's go back there and run this code and it worked you see that it worked we got the object we got the output as compiling so what is happening behind the scenes so when you say auto wire it will try to connect because a1 has an instance I mean alien object needs an instance of l1 or 8 laptop object so it was search for the type okay by default Auto wired searches for the type not for the name l1 doesn't matter in fact by default the name is not even l1 the default name for anything is laptop itself but with small L the default name for this a1 object is not even a 1 it is small a alien so you can search my name as well but we want the type here ok what what if you want to search by name for that we have to use one notation which is at qualifier so using qualifier we can do that so I will say add qualifier and here you can mention what you can mention a laptop so now it will search by name can we change the name of the lab can you change the name of fear because by default the name would be laptop right you can say hey I don't want laptop as a name I want L a p1 can you do that yes we can do that we can have a name now la p1 which means the object which spring creates here when will will not be laptop one now it will be lab 1 so this is the object name and since we are using qualifier in the alien itself we have to say lab 1 because we have to search by name right so by default Auto wire searches by type at qualifiers such as by name so I hope you understood something how what is happening behind the scene if you enjoyed the type of session because I'm just trying out different ways of explaining your different concepts let me know in the comment section if you liked it because I will making some more videos based on this on spring so that's it everyone I hope you enjoyed in fact I should be doing a quick recap what we have done so I wanted the object of alien so engine is basically a class which has four attributes or four properties we can say a ID a name Tech and laptop the laptop will special type and we have a laptop class here using add component it will instantiate your object the when you say instantiation means you want a new object right so don't make you say component you will create object in a spring container and using this auto wire you will get connected with the with the out with the with the object which is percolated by spring framework let me run this code one second to verify and it works right so try it out and let me know in the comment section if you have any questions and your feedback is it good bad in this video we will see how to create a web application using springboard in fact we have seen what is springboard we have seen how to achieve dependency injection auto wiring in springboard but in real world what we do is we build a web application now it may be some page some website very where you will show views it may be implementing angularjs or using JSP or maybe you just want to create a service I can will go towards rest service later but in this video we will see how to create a web application where you will be making a view in JSP that is tricky okay so what I will do is I will create a web application but how do we do that now if we remember the steps for creating a spring boot it's very easy just open STS right click here and say new and click on spring starter project and here of course the project will be coming from starters start dots playing dot IO and I want to give a proper name so I would say my web app ok you can give it doesn't matter and the group ID would become doctor disco and here also I will mention it steady scope ok now once we have done with that click on next now this is important stuff because we are not building a normal application this is a spring boot web application so of course we need to add the web feature there now when I say wear feature you must have seen spring MVC in fact if you have not seen my spring MVC video that is the important video you have to watch first so if you have not seen that in the description area you will find a link for the theory video now here we have to add a web light so I will simply search for web and you can see we got this option of web now since we are using spring version 2.0 we have one more option which is reactive web which is introduced in 2.0 will not go for that now let's stick let's select this web and click on next and then at the end you will click on finish you will get your project but is it working that's a question and you can see this project is coming from the official web site so it will take some time so we got our project here which is my web app now if I see this project you can see we just have one package which which has of file now if I open this file you can see something here but let's ignore this part as of now because we just want to focus on creating a web application so we close this and if I open my eyes I see we have a main now basically when you say you want to create a web page now so first of all I want to create a page which is index dot JSP so inside this mean now this is important you can create a web page you can create any JC page provided it is below it belongs to a web app folder so inside this we need to create a folder called as web app so I would say a folder called as web app and inside this web i we can include any number of pages you want so i would say no and i want to create a JSP page so we'll search for JSP and you can see you can say next and i will name this page as home dot JSP and click on finish now what I want is whenever I request for home so the URL I want to go for here is I will write it here so the URL I want to go for is of course localhost colon 8080 that's a port number which it takes slash whenever I search for home or may I hope we can have any URL there but just to make it easy whenever I search for home it should call home dot JSP now if you have worked on and we see you know what I'm talking about so in MVC we create a model we create a view so this is our view and this is our request the request goes to controller and the controller will call this home dot JSP Oh unfortunately we don't have a controller yet now how do we create a controller in in spring it's very easy you know in servlet if you have worked on servlet to make a controller you have to get a servlet that will do a lot of things but here we just need to say a new plus and then we can give any name I can say this is my home controller again there is no compulsion that it should have a home controller name you can have any name doesn't matter and then click on finish now once you got a class it's very easy you just need to add a method normally if you remember servlet you can just drink these things with servlets and solve it what we do is we get a service method and don't worry even if you don't know sorry that's fine because spring MVC is much much easier than solids so we can't we need to create a because we want to perform an action so I would say I want to create a method which is public or maybe I will return I will say void and I would say maybe home again doesn't matter what your method name is but just to keep it simple I'm using the same method name as the request and I would print something here not nothing fancy I just want to print something because the first step would be see technically if I want to make you understand this if I open my web browser so when I search for localhost 8080 slash home I want to get that page as you can see it is not responding anything now it's because we don't have that link ultimately what I want is someone should accept this request so step one would be I want a controller who will accept that request so of course to do that first of all you need to give a special preference to this page tota to this class we need to add a annotation and trust me when you work on spring applications you have to remember a lot of annotations so the annotation which you have to use here is ad controller that's it you just need to use a notation called as controller and you have to import the package now of course when you're going forward rest control rest API or their services you need to go for the rest controller but as of now let's keep it simple let's make it controller now whenever I get a request your Springwood application will make sure that this controller will handle all the requests and who is doing that thanks to Auto configuration if you remember in Springwood we have this feature behind the scene we it is doing conservation for you so we have a dispatch sahabat behind the scene and that dispersed off that is responsible to send requests here but how do you map it because there might be different requests right currently we are send requests for whom maybe I want to send requests for products I want to send a request for contact us I want different different pages for different different requests so we need to specify something called as request mapping so using this request mapping you can mention for which request I want to handle this so nice I will say whenever we get a home request I want to input the package first so whenever you get a request for home I want to execute this method but how would I know if the method is getting executed so I was a system dot out dot println I know I'm printing on the console I should be printing on Paige but let's do that later so I will simply print hi because if it is sprinting hi that means there is someone who's accepting the request okay that sounds fun no what I will do is I will run this application but how now if you remember if when you work with servlet or normal spring MVC we need a server right and you can see I have not added in any server you can see it says no servers are available oh that's a problem now but what if I say you don't need external servers to run Springwood application well that's a magic right because if you expand this mapping dependencies you can see we have a tomcat here with that yeah you can see that we have embedded Tomcat inside your project itself and that's a magic you know because you don't need external server to run this now you will say what's the advantage of this but look at this advantage let's say if you are working on a project and if you have if you want to test this project in some other laptop and you've got your friend's laptop now and luckily your friend's laptop has eclipsed all the SDS but then if you are using a normal application you need to install Tomcat externally but here you just need to import the project that's it because project itself has a Tomcat so it saves lots of your efforts and plus it is easier for deployment ok so once we know this let's run this project because the moment you run this project it will start the internal Tomcat so here we go I will right-click on the project I would say is run as Springwood app it will be some time obviously so you can see it is running and the Tomcat has started you can see that it's saying stomp catch started at port number 80 80 so go back to our browser and I will say localhost colon 8080 slash home the moment I say enter we got an error okay that's fine we got an error that's fine important thinkers go back to the console ok I hope I will be getting hi there oh you can see that we got high that means this controller is handling your request that was a first step ok when you build an application web application we go into a series of steps first we need to create something which will accept the request do you send you a response does s/s your thing you know so this is something going wrong with the response you can see these energies on the server side not on the client side client has sent a pure perfect request it's a server problem but at least there's someone who's handling this but then how do I call a page that's a that's a second thing we want to mention how do I call this home dot JSP it's actually very simple you know after printing hi or maybe you can remove that statement you simply need to mention hey controller you are responsible to call this home dot JSP page simply say return home dot JSP now since we are returning a string you will say string that's it when you say string return home dot JSP it will call that particular JSP that sounds awesome now how it knows where to search because you might be having different folders how it knows that you have to search in web app and that is what is done inside spring boot so spring boot has this auto quantification stuff which is actually searching so in the configuration itself they have mentioned that your pages will be there inside main web web app yes if you are changing the path then you have to do some configuration we will see that later but as of now we just need to mention return home dot JSP and it will call that particular JSP that's a beauty right in fact the same thing can be done we have done in spring MVC if you have seen those videos but let's run this let's see if it is working I will right click here I would say run as run on spring boot ok now this is an obvious hello what is what is going wrong here the problem is when you work with the external servers you know when you work with servlet or normal spring MVC whenever you restart the application it will restart your tomcat server but unfortunately the Tomcat server which we have here and this in this project is embedded tomcat so every time you run this project it will try to run another tomcat server it will not restart the old one and that's the problem here so what you have to do is you should always close the earlier run so you should terminate it and then you have to run this otherwise you can click on this button which will restart the old application so it's a relaunch and it should work now so you can see there is no problem Tommy restart tomcat started out the white our browser and I would say enter now something weird happened you can see we got the same but we got something in the downloads you can see we got home dot JSP as a download that's weird right now it is happening here the thing is when you say you are calling home dot JSP now JSP is a special type right and of course everyone knows that spring boot is something which is very new and when spring boot came into picture the rest api's or micro services were very famous so that means when spring boot came into picture their focus was on rest api or maybe there are different new technologies right not JSP we have velocity we have theme leaf so your spring boot says hey by default I will not support JSP because my work is to resend a response example if I say you know there is one more annotation here which is response body I can't really talk about response body in detail later once we once we talk about Wednesday API so if you say response body let me just show you something what happens if I say response body so let's run this and if I go back to the page and if I say enter you can see it is sprinting home dot JSP you can you can print any text you want example in home dot JSP unfortunate is it is empty I will print something here I would say hi ok I would say I love Java so if I if I say I love Java here and from the home controller so it will call it will print home dot JSP because this response body says hey whatever you are returning is actually the data it's not the page and image data but I don't want to print a data right I don't want response body what I want is I want to call this page but unfortunately your spring boot doesn't support JSP now in this case you need to add a support now of course you will say hey what's the use of a spring boot when you have to add the support but trust me when you work on real applications nowadays we don't use JSP we work on we work on West api's we work with different of you technologies some projects are based on JSP that's fine you can just simply add that feature but to do that just open your parmesan file and in this you just need to add one dependency for that so I will just come back here and here I will add that dependency but which one so you have to go to my main repository I would say MV and wrapper so to go to I will deposit and here search for Tomcat Jasper now Tomcat Jasper just select this one the first one and you need to select a jar which you need but which one I'm not sure so to understand that I will go to my own dependency so which are version you are using for a medic Tomcat which is eight point five point three one let's pick up the same one eight point five point three one and copy this code go back here and paste and you can see we got Tomcat Jasper now your spring boat knows how to convert a JSP into servlet and of course right behind the scene doesn't matter which technology you're working we have servlets running so I will go back here and I will relaunch it I hope it will work this time is it saying it is still downloading the source is it maybe let's try it says it is still downloading but let me try once again I will say slash home and it can you see that we got I love Java and you got the URL because in the page there was this text but this is working right so this is your first application where the flow is happening right so you are calling homepage it is returning home dot JSP but what if you want to send some data what if you want to I mean what if you want to accept some data from the from the client-side or if you want to perform some operation that we'll see later but the main idea is this is how you build a simple web application in springboard so let me know your thoughts in the comment section if you have any questions and do click on the like button if you enjoyed it thanks for watching everyone in this video we'll continue with the web app using spring boot so in the last video of what we have done is we have go to this page which is home dot JSP in which I'm just printing our love Java I don't want to print this localhost part as of now so I made the changes here and once I've made the changes I can just restart the application so that let's see if it is working I will go back to my browser and say enter you can see I got I love Java now what I want is you know if I go back to the controller everything seems good at this point but normally when you create a application you don't keep all your files in web app you don't put all your pages and web app because web app is app folder which is public what if you want to make it private what if you want to put your pages in some other folder example let's say inside web app I have one more folder here which is called as let's say pages so how this folder which is pages and I'm moving this home dot JSP two pages so that's the first change which I have done the second change is normally when you work on spring framework or spring MVC in fact if you have seen my theory video of it you don't normally you mention the JSP pages is because in future you might want to change to velocity you might want to change to theme live of course you don't want to put a dot JSP at the end so we're able to search the page which is dot JSP so I have made two changes here the first change is the location of the page and the second change is dot JSP have removed the JSP part now as we know we have a concept of auto configuration inside spring boot which is doing this stuff for you so the default folder is web app the extension is whatever you mentioned here but what if you want to configure this because we have not done any confusion here right if you want to do any configuration we have this special file called as application dot properties now it belongs to this resources folder and if I open this application dot properties it is empty as of now because we are not doing any manual configuration but what if you want to do something now what is that something here so I want to specify two properties the first one is prefix so the part of a page is defined with the elbow prefix and the extension of a page is defined with the help of suffix so it's very simple just say prefix now I'm going to say prefix you can see we get the end at least here so you don't have to by heart those properties basically this is a key value pair okay so on the left hand side you specify the key which is fixed and the value is something which you specify so I want to specify the prefix here but as you can see we have so many prefix here and now you can guess it right because we are not using massage we are not using free marker we are basically using spring MVC view so we'll select this one and then we'll say we'll say equal to whatever your prefix is so my folder which is slash pages so I have my web app inside this I have my page inside this pages folder that's the prefix the next thing we have to specify is suffix so I will go for suffix and that is spring MVC dot view dot suffix and the suffix is dot JSP so those are two things you have to mention and likewise you can configure whatever you want maybe if you are working with it up databases of course you can specify all the properties here and this is kind of cool right because you're not using XML anymore we are using properties in fact we have one more option of EML so we can use that as well but this is where you can specify all the properties so yes spring boot does provide you auto configuration but that doesn't means that you cannot do any confusion by yourself you can so if you don't do any quantification this is how you do it but let's verify whatever changes we have made is it working so I'll run this application and then I will go back to my page I will say enter and it's working against that it's working just to make some changes let's say if I make a mistake here instead of saying dot JSP if I make it let's say dot HTML now I just made that mistake there let's try if it is working I will go to my browser and say enter you can see the got an error so this is how you can configure it so of course you have to make sure that you have a proper data there so that how you can configure that's how we can separate the extension and the location of your page so basically what we have done till now is we got this home page which is dot JSP and then we have a controller so whenever you send a request for whom I mean whatever your website name is let's say if you have a site name telesco so it will be the disco comm slash home so that's my request so I'm handling that request by so whenever I use a specified slash home I will be calling god I mean home dot JSP just to show you on a page here so I have a browser open so if I go to browser if I say localhost colon 8080 slash home it is calling the home page how can be that sure it's very simple if I just go back to my astreus and if you can see in the home controller you face if I show you we have in fact in home dot JSP we are printing I love Java so everything is working perfectly in fact we have also seen how do we configure stuff again will be doing lot of quantification in future but time being let's do something new now what if I want to pass a data and of course right when you have low and servlets we have done those things now what I mean by that is if I go back to my browser and if I say hey I want to pass some data example I want to pass and name example if I pass naveen I want to print that name here so I will say welcome naveen but the name might change right so if I'm passing them in here maybe next time I will pass let's say John maybe I pass Cristina so I will say I will keep changing the name right I don't want to write this name here because of course it will be constant ok that's one thing I would do before printing something here first I want to send some data so I was I saying data normally in the browser what you do is you put question mark and then you say name is equal to no VIN that's how you send data so you say question mark name equal to Naveen so we are passing this name to the server but on the server side we are doing nothing right you can see in the home controller we are not accepting that value we want to send it that data to the home dot JSP first we have to accept the value but how would I do that now go back to your server technology in sublet what we should do is we used to create object of httpservletrequest and using that request object we used to fetch data is it possible here let's try oh but how would I fetch data because we have not created the object now spring gives you the comfort of creating the object if you need example in servlet it was compulsory for us to refer both the objects the request object and response object even if it don't need it but in spring you have a choice if you want to use it go ahead and use it it's very simple just go here and type HTTP servlet request and we'll say i'll eq so we have the access to that request object now so you don't have to do anything extra just refer it and automatically spring will connect it with it with the available request object and once you have that request object you can simply fetch the value right so what you can do you can go back here and say I want to fetch a string which is a name in this case and it will be request dot getparameter and in this you will specify the key right keys name now how would I know keys name because from the client silence and in that so I'm saying name equal in a bean now of course client will not be typing all those things in the browser we will create a good UI I mean we we can't create a good UI from where a client will sign so there will be a text field where you will type the name and the moment you click on submit the URL will have the values right that's how you get request works and now once you have a name I want to print this name on the home dot JSP but that will be secondary part right first I want to check if the value is coming here so I want to say hi plus name just to verify if the name is coming from the client page to this to the controller you know should always do something in a proper sequence because then it will make more sense now once you have made the changes I'm trying to print data here so just to recap what we have done I'm using this HTTP servlet request object you can use it anytime you want it's not if there is no compulsion that you should be writing this and then you just need to specify name here and your printing name if I want to on this quote I will just relaunch it okay now I will go to my browser and if I say enter of course they will not be any change here it will print welcome is no name here but let's verify the console because I'm just printing on the console so if I go back to my console here and if I go you can see we got hi Naveen so at least the data is coming from the client to the server but now I want to show that on a page right how would I send that data from here to the home page now technically here from a controller I am calling dot JSP so it might be a request dispatcher or it might be response dots and we write we have those two choice here but how will I know which one is this so if I go back to our browser you can see we got a different page but there's no change in the URL so normally if this is the case it means your Spring Framework is using requestdispatcher to call home dot JSP and since you are using equal dispatcher that means it will they will be referring the same request object ultimately okay if you are not sure about this concept I would highly recommend you to watch my sublet videos where we have talked about liquid dispatcher so in request dispatcher to solids or one summit of one JSP share this same request object so what you can do is you can use the request object example I just want to pass this name to this home dot JSP so we can use a quest dot sect attribute that's one way and in JSP you can see get attribute that's fine otherwise we can use session now if you remember we also have a concept of HTTP session right now how would I use those things here it's very easy if you want to use HTTP session just type HTTP session I will say session equal to request dot get session now once we have a hold on session object what we can do is we can add the name in this session object I will say session dot add at us set attribute and you can say I want to pass a name and the value is name itself so I want to pass a name not known this name becomes a key and whatever the value in we have inside name that will become your value which is nothing in this case and then we are adding the value in a session object but how will I fetch that in home dot JSP that's a question now it's very simple right we can create we can write a Java code here and then we can simply use such an object otherwise we can also used a STL so I will use a dollar sign here and I want to print name so this this is a J STL format or e l which is expression language now will it work because I have done everything right the data is coming from the client I printed it on the console at all it was working I have added the value in this session and Here I am pulling it from the session but will it work let's try so I'm relaunching the application so let's get back to the browser and say enter oh it's working can you see that we got welcome naveen so whatever data you are sending from the client side it is getting printed on the home dot JSP so this is how you can achieve one simple flow of course we are not doing anything big here because we want to just go into a small small steps right so what we have done till now is I am calling a home page I'm calling a home request and then I am sending some data which I am able to print on this home page now can I use HTTP response object because we have used request object right what if I want to use response object is very simple just give a comma and say HTTP servlet response and now you also have a hold on response object I mean of course we don't need that object in this example but let's say if you want to use response object then this is a way so everything looks cool right it's it's almost similar to servlets but that's also a problem if it is similar to servlet why do we need a Spring Framework right we have some amazing feature and I don't want to reveal those features in this video so just wait for the next video in which we will reveal what we can do with spring MVC we can just skip all this extra stuff you know we can also skip this all these things how that will say in the next video now if you have seen this example till now it's very simple right we are accepting a home request and with whom a client might send a name so in that case I'm fetching the name of a client and then I'm adding that to a session so that I can print that on home dot JSP everything is simple but now what are do is I just want to simplify this code because this call being spring MVC code a start it's not that effective right so what we will do here is ensure of saying this you know I don't want to use this request should it be serve a request and response I want to accept this values directly I want to accept this name directly can I do that can I just remove this thing and still it should work let's try now if I remove this of course it will not work because we are using a questa object right that's one problem what if I simply say string name that's it nothing much just a string name and let's remove this line so whatever data is coming from the client side will be assigned to this name that's what I'm expecting the next thing is about this session object because I will not get a session object without request object so what if you can just simply specify because you are working with spinning framework right so spring framework should give it to you so I would say HTTP session session and I think our job will be done because we don't need this line now and automatically you will get a object of HTTP session thanks to spinning framework because we have this concept of dependency injection so we got a string name and record HTTP session session now will it work okay this is this is just a try so let's run this lets relaunch the application so you know relaunching done and if I go into the browser again and if I say enter so you can see it is still working so there's nothing wrong with the code it's working right so how is working if I pass anything with work example if I pass let's say with another parameter if I say name one and inter you can see is not working it is only working with name parameter there is some matching happening then the scene so the moment you say hey I'm passing a name here and you also have a name here so it will directly match with it but still if you want to have a different name example what if I don't a name here I want to have so let's say chef's name I have my name now we have a different variable name right and of course I have to change here as well so I would say my name and my name everywhere so of course I have to change that so it's the we have a different name now and still if I if I do that of course it will okay so we have to really launch it again so let's go back to browser and say enter so you can see it is not working so of course we need to have if I pass my name that will work but here I want name and then I want my name so will it work now so if I say name and there it is my name how it will match now in that case if you have a different name we need to use and annotation called as request parameter so we'll say request param and in this request padam will specify the names I would say name so anyone from a client side when you get name just assign the value to my name now this is also helpful when you are passing multiple parameters because we're one parameter which is easy to map what if you have three to four parameters in that case you need to use request by time or you don't have a choice example if I'm sending a age name let's say Marx we have three variables right so in that case you need to use request but I am so that's how we use we can ignore the question object and response object now I have one more thing what if I want to skip this session as well now first of all why do we need session that's a question right now we need session is because we are adding data into this into this session object and then that data is going to this home dot JSP now let's go with the flow here so if the flow is whatever data you are running from here and whatever nivi or name you are interpreting here it is actually going to dispatch a servlet now despite a servlet is a special thing inside spring MVC right so whatever goes it goes to dispersed omelet now dispersed of it basically need two things the first thing is it needs the data and the view name of course white as a client are you need a view and you also need a data so here also if you can observe we are passing the view name and we are passing a data so data will be let's say you can call data as model and you are sending a view so we have a combination of that which is called as model and view now how do we use it it's very simple so you will say instead of creating object here you can say model and view I would say MV is equal to new model and view and here so once we got this object what you will be doing is you will be adding the data in the model and in fact in the MV object and you will be mentioning the MV name in fact you in stop saying return home I will say turn mV and of course when you are doing a Model View object you will not specify string there you will specify Model View itself but then how will I specify this view name and I don't want to use session anymore because we don't normally use sessions we will be using model and view so of course I have to still send two things the view name and the data in fact I don't even want to print on this on this on the console it's of no use so I have addy I have a model of the object I want to send this DITA my name and I want to specify the view name so the way you do that is by saying MV dot add object and inside this add object you can you can pass two things the first one is the key the second one is the variable name which is myname in this case so you are sending the data but what about the view name in order to specify the view name you can also say MV dot set view name and this you can mention the view name is home so this this is very powerful you know because you can add multiple objects you want example you can say MV dot add object you can add one more object you can pass a number of objects from here and you can set the view name it is more clear to work with it's more clean because if you get stuck if you get bugs it will be easier for you to remove from here now but after making all these changes will it work that's a question let's run so let's get back to the browser and say enter and you can see it works so instead of using HTTP session you sure of using servlet also that request and response we can simply use model and view so just to give you a recap what we have done so I'm accepting the home request I'm accepting a name and I'm sending that data so I'm sending two things the data and a view name and when you say data it's a model and we have a view name so spring MVC gives you a special class Palace model and view which can hold two things the first one is the view name and the second one is the data you can add multiple data as you want so you can do that with the help of an object so that's it from this video now what we have done till now is it's very simple code we are accepting a request and then with the request I am also getting a name which I am able to print on the home page but what if I want to send some more data because normally when you send a detail from a client to server it might have multiple parameters maybe I'm sending the entire object values example let's say what I will do here is I will create a simple class and I will name this class as let's say alien now of course when I say alien it simply means that who someone who is working in a virtual world so basically we are programmers and we work in virtual world that's why therefore either for programmers as aliens so we have with this alien class which is very simple in this agent class I want to have certain variables so I would say this is int Drive it ain't a ID I want to have private string a name and private string language so everyone have that a1 favorite language right you might work on multiple languages but there is some language which you always love so we have these three variables and with these three variables I want getters and setters so regard variables and we got getters and setters I want this values to be accepted so from a client side I want to send this three values so the example if I go back to my page if I want to send three values I would say name now this time it will not be name it will be a name because I'm sending a names okay we'll just go back to my thing so we got a name a ID and Lang let's go in that sequence so I would say a ID is equal to one and and and we'll say language is equal to Java so I want to send these three details the moment I say enter you can see we got an error because we don't have anything as named there we got a name okay so first of all I want to I want to send these three things if I go back to my page you can see I'm not accepting all I'm a setting name it should be a name this time and then we need to create three variables right it will be a ID it will be my name regarde language but what if I want to accept the object as it is because of course why do you are sending three values so instead of accepting proof in two different variables what if you can accept it in an object will that work so what exactly I want is I want to say alien alien will this work let's try so whatever you do instead of saying name I would say name I will simply say obj and then I'm passing and alien so I'm not passing one by one variable I am passing the entire object so that's a question will it able to accept all the values because alien does have three variables but will it work let's try so I will go to my home page and then we are printing a object here because object dot a ID I will give a comma and then I will say object dot a name and then we have one more which is obj dot lying so we got these three things so will this work let's try that's what we are trying to do here so if I go back to my page and if I say enter you can see is working we got one Naveen and Java so that's the beauty of about spring MVC right you can send the values and it will be getting a it will be getting accepted in one particular object so there are different there are multiple reasons why should be moving from normals of the JSP to spring MVC right so this this thing is working now if I I just want to do one change here which is what if I want to X what if I want to implement rest services example instead of sending data in this way maybe I want to send data in JSON format maybe I want to accept data in JSON format maybe I want to give a proper link for the hierarchy how will I do that now if you are not sure about what is ready API if I will surely recommend you to watch my REST API videos using Jersey so that you can understand how to implement rest using spring in this video we will create a spring boot application using spring data JPA and spring MVC normally we'll create a web page where you will try to add some data in database we'll try to fetch something so let's let's see how do we do that we'll create a project here fresh project I would say new spring stellar project give a name to it I would say spring boot JPA and then I will say next now here what are things we need now first of all we are making our web projects I would say web now what type of database I want so of course we'll be using jps so we'll select JPA here and then whatever database I want now we can use any database we want example we can use MySQL we can use Posterous now just for this example I'll be using in memory database which is we can use h2 database which is in memory database and of course we can use an idiot abyss you want and then we'll click on next because these three things we need then we'll click on next and we'll click on finish the moment you click on finish you will get your project of course it will take some time so here's we got our project and you can see we got our project which is called as both JPA now what exactly I want now first thing I want here is to work with edge to write in fact if you if I just want to show you what exactly I want I basically I want an application where I can add aliens I can fetch aliens so maybe I want to add new alien 680 ins I will be having two parameters one is I'll be having the name of the alien and then I will be having in an ID for the alien so just to show you that I will need a class I'll put that in a model package and the class name would be alien and in this class alien I need I just need two things one is a ID and the second thing is the name and for this we need getters and setters and I also want to implement two signature years so I will say so we got two strings so we got a ID a name record gives it is for that and two string method so this is the this is the class I want to work with now that I want is I want a page when I will be able to add those values so in the resources and in fact inside main I want a folder where I would call it as web app and inside this web app I will be getting a JSP so I would say J is and I will name this JSP as home dot JSP this is where I will put putting my values now what other fields I need I need a form tag well I was able to add those I will be able to put those two values so this we want right we want a form and a user will enter two values a ID a name the moment I click on submit it will call add alien I this aliens will be added in database and of course we can create one more form where you can fetch data and of course if you can do adding you can fetch data you can fetch it by yourself as well right that will be assignment but I want to make it work the moment I click on submit it should be able to add alien in try just to run this application let's see what happens if I if I just run this code so I would say spring boot this project run a spring boot app so let me open my browser and I would say localhost colon 8080 and if I say enter you can see it's a space not found and even if you try to access home dot J's but let's try that and you can see we still got either because you need a controller to call that page right that's how spring MVC works you need to make it flow and to do that of course we need a controller and we have not done that yet so let's build a controller here so I'm basically trying to know create the basic stuff for us new class this will be a controller and I would say this is alien controller name can be anything doesn't matter and I want to put this in a package called as controller and inside this controller I want to have a method so I would say public string home and this will return home dot JSP now when I want to call this method uh first of all I want to make this as a controller because this without is it will not work and here I want to say add request mapping and in this I want to specify the part so whenever like I've called the home page it should call home dot JSP oh I missed one thing it should be double quotes here ok everything seems good now if I run this project let's see if it is working so I will just relaunch the application and I will go back there and say enter oh you can see it is still not working because we missed one thing if you remember we are yet to add the Jasper right when you are working with JSP we need to add a pawn file which are going to add one more dependency which is Jasper and how do we get that we just go to mV repository search for tomcat Jasper this is what you want and all of this as well I want eight point five point three zero the problem is eight point five point three one is not working properly in my machine so I'm using this one let's find if you even if you go for three point one okay it's done and let me just relaunch it once again I'll go right now browser and if I say enter you can see we got a page right so this thing is working perfectly it's very simple stuff nothing fancy right so what is happening now is the moment you call when you when you the moment you say homepage it is calling home dot JSP I mean the moment I say slash it is calling home dot JSP it's working perfectly and now what I want is the moment moment I click on submit it should take these two values and add it in database but hold on where's our database with our table right that's a question now first of all how will your spring boat knows that you want to connect with h2 so it's to the database right now first of all you need to enable a database because it is in memory database right it's not external to your application how will you enable it and for that we need to do some settings now luckily spring boot has a concept of Auto configuration and it will do the confusion for you you just need to specify what you want and the way you do that is buying going to a resource in this application what properties you just need to set one property which is spring dot h2 is it daddy has dot console you just need to say an Abel equal it true that's it that's the first thing you do now you need to also mention the platform where you're working with so you have to say spring data source dot platform and here you will mention h2 this is the platform you're working with and one more thing okay the last thing this is where you have to specify the URL example when you were to work with any database of course you have to specify the URL right and the way you do that is by saying spring dot datasource dot URL equal to now the URL is JDBC : h2 : meam and now you can mention any device you want so I would say Naveen here but how will I access the database because if you are using mask you'll you have external application to I work with right here what you will do is you will just reach out the application or relaunch it and the way you can acts URH - database is very simple just go back to your browser I will not be using memory anymore so I will just come back here and I would say hey I want to access microns so the way you do that is by saying localhost colon 8080 /h - - console you just have to say is - - console it will give you a login page now it will say driver name it will specify JDBC URL now there is something which you have conveyed in your database right this is what in the application this is what you are mentioned the username which is as they don't change using him don't change the password because paths we don't need password there and simply click on test connection and you can see test successful right so you are ready to go ahead click on connect and this is where your device is now this is your database but you can see we don't have any tables see it and of course we want a table right but how do you get a table now to get a table you need to enable JPA and you can see we have no beyond we have not configured JPA yet do we need to do that do we need to configure JPA you just need to do one thing in the annotation you have to say at entity and here you have to say at ID this this is the thing we need right we have to specify that that's an entity and this is the ID now to also make it well first of all you have to input the package now don't make it to work we also need some data in a table right if you have a table of course which you should be having some blank data now spring boot will do will give it to your spring boot we'll create a table for you first of all let me just relaunch it let's just verify what is working before adding any data so if I go back to the browser and if I say refresh connect ok see that we got a table there awesome we just said identity and ID ID we got a table that's the part of a springboard and that's the power of spring boot auto-configuration now let me just check if the reader is there so I was a select star from alien and I would say run oh it's empty you can see we got a table but it is empty I want to add some data you can do it from here or if you want some data pre hand what you can do is you can create a separate data file you can say no I would say a file simple file and I will name this file as data dot SQL so we got it at all SQL there and I will open that with with a text reader ok you can see we got this thing here you can write a statement to insert data against not compulsory you can do it from the h2 as well but every time you relaunch it since this is a in-memory database every time you relaunch the application you might lose your data right so it's a insert into alien I would say values at least I want one data there so I would say 1 0 1 and the name here I want is not me at least I want this data there and after doing that let me just relaunch it just to verify if it is if it is coming there I'll go back to browser I will just simply copy this so that I don't have to type it again I will defuse it at a base I would say connect you can see we got alien if I say run this query can see that we got data so your spring boot is actually loading that file at runtime so you can write any number of SQL statement you want if you want to have that database to work with now what I want is I want to add some data right mod it or not from here but from the page so what I will do is from the home dot JSP I will click on submit it will call add alien but if you can see we don't have any controller will accept our alien so let's create one so I would say public string I would say add alien and from here itself again I will call the same home page I don't want to get in either page I would call the same home page of course when you say fetch aliens then you can you know create another page where you can display that data and here I would say this should be called so for that request mapping and this should be called whenever you have a request for add alien now since we are sending data there now if you remember our last concept of model object we will simply say alien alien here so whatever data is coming from the client will be assigned here automatically it's that beautiful right okay everything seems good I would think seems good but then how will you add data in database if you remember we need to write JDBC code right or in JPA or in hibernate we need to have a repository link we have to create a we have to create a separate code using which you can add we will do that here as well normally what you do is we first create interface and then we create a class which implements that interface so just let's get interface first so I would say no I will not get a class as of now let me just create interface and I will call this as alien repo again you can say alien Dao or the PO doesn't matter and I want this to be in telesco condor telesco dot demo dot Dao this is where I want all my repository and click on finish now this repository will have some methods right which is normally we create week up like read operation side we say create update delayed and all those stuff now since we are using data JPS printed a JPA it gives you this awesome feature such as be with me to see the magic what I will do is I will simply extend this interface with another interface called as code repository and we will say here you are responsible to work with alien and the primary keys of type integer that's it you just need to mention the class name and the type of primary key you will get all the code operations now how it is working so you can see if I click on code repository we have saved method we have it tribal so all these methods are declared here or that means we need to define them hold on will not do that what are we simply do is I'm now see if you can see I'm just quick I have just created the interface in this interface it is already extending cut cut into the code repository which has a lot of methods and we are not implementing any method in the alien controller what I will do is I will simply create object of alien repo I would say repo I am NOT creating a class ok nowhere I'm feeding a class here which implements this repository and I will simply say Auto why order because what a while will will instantiate it it will look for the object of alien repository and I am not good at any class I have not created any object and that's the beauty of a springboard because if it is working then spring boot is awesome right so let's try in fact this is a feature of hot spring data JPA let's try I will relaunch the application I'll go back to the browser and I will refresh say connect or first of all I want to say select star from alien restaurant you can see we have only one data and now I will try to add some more data I would say once your - I would say per min and I will click on submit you can see submit has been done and we got the same page let's go to database list just to verify this working oh it's not working you can see we have still not added or because we have done one mistake right even if I call add alien you are not saving it right so you have to say report save alien we missed one statement how can I do that relaunch it go back to the browser and click on I will say simply sent the same request because in the URL we still have values I would center's there is no error go back to the h2 run I will just run ok see that we got the second data it's that easy right and if I show you the code once again we have not done much right we have not written any JDBC code this is the beauty about spring rest so what we have done is we have this home page from where when I click on submit' it goes to alien had alien this an alien is doing nothing much it is fetching the value of the alien and it is saying report save now it is report I pose the reference for alien repo and alien repo is an interface we are not implementing this interface anywhere spring data rest is responsible to do that for you so behind the scene there there are some classes which is doing it for you right because you don't have to think about much about it and of course you can perform all the code operations but what if you want to do something else now that's a question right we will try to answer that question in the in the in the coming videos but then as at this point this is working for you so you can save you can fetch you can update you can delete all the methods are available just try to by yourself and it should work so in the palm file as well we have not done any changes much we just added the Tomcat jasper and here you just need to add these three things you have to enable your h2 you need to specify the platform and data source that's it addy which is the URL that's it and this thing will work in the last video we have seen how do we create a simple spring boot application using JPA and h2 now let's go ahead with the example so whatever we have done till now is very simple we are simply adding this alien data and the moment you click on submit a little goes to a controller a controller will fetch those values in the alien object and the same data will be saved in the database with the help of this alien repo now thanks to spring data JPA we have this concept of creating the interface and simply extending a code repository normally what we do in databases is we create a separate class and that class will have certain methods and normally what you do is cut operation so it create update delete and read so we can do all those things in fact you don't have to write the code spring data JPA makes this thing easy for you and says hey you don't have to worry about that we have implemented all the code operations that's awesome right in fact we have done only one thing till now which is doing save now there are certain questions which need to answer now if you can see this example I have not used any service layer so we have a controller and controller is interacting with lapo so that's not how we write an application in the enterprise scenario right what we do is we also have a service layer now why do we need a service layer is because as a controller we are always not sure that data is coming from database sometime data is coming from the network sometime data is coming from the input provided from the user so we need to process something and that processing should be done in this service layer in case if data is coming from the repository then so obviously I will interact with it with repository so that's how we build application but here since we just wanted to explain about spring data JPA I directly went with the repo ok so once we got a repo here and once we are able to say add what if I want to fetch in fact before the before the video I just made some changes from the last example the first change is in the alien class I have added one more field which is tech so we had a ID we had a name now I have also added tech and I have added five values so if I go to data skill you can see I've added five values in show of one I have learned this application once so if I go to my console the h2 console and if I say select star from alien and if I done this code you can see record five records because I want to do some extra stuff right if I come back to my code what else I want to do is I want to fetch the alien so in the last example I've just added the alien right what if I want to fetch an alien it's really easy you can create separate form for that of course we should be doing and they should be dividing this line by something but I'm not that good with designing so let's ignore that so I would say get alien so I want to fetch an alien based on the a ID so I will not be doing any of this stuff so simply a ID so the moment you click on the moment you type in a ID and say submit it it should return in a ID now unfortunately we don't have any controller which will accept that request so I will copy this and I would say this is get alien and the Metheny will also be get alien and this time I'm not expecting alien of course I'm expecting a string which is called as a ID and if you want to map this I will be using a request param so the equation time is something using which you can you know or fetch the value from the client okay so once I got the value instead of saying safe I will be saying okay now what what is it what's the method I want to use here and where do I stored it okay that's a question now in fact we have one more question about home dot JSP which page I will be using now I want to show the cause right so of course I need a separate JSP page and that's the thing with you know with the view technologies for everything if you want to show something to the user you need to create a separate JSP let me just do that I would say this is Show alien dot JSP and this stick to be where so sure alien dot JSP should be a part of web app ok this seems coach and in this show alien I want to display the data about alien so what I will do is I will simply say dollar and I want to send an alien so this is what I'll be sending from the controller ok so that's that's good so here I would be saying alien or not alien I would say show alien JSP so this is what I want to return but with the view I also want to send a data right so as we have discussed in the earlier videos we can use a model and view here because it will make more sense so I will be saying model and view MV is equal to new model and will of course we need to import the package and in the in the constructor itself I will specify the view name this is one way you can also set the view name or you can use a constructor to do that and ensure featuring a string I will return MV but in this MV I also need so with this MV with this view I also want to return the data but from nowhere I am fetching data right now think about this when I say get alien by passing an a ID what do you think how many aliens will get one or many of course one right because when you mentioned primary key you will be getting only one ID or one object so you will be storing that in an alien so you will say alien alien is equal to repo dot now what should be the method name because say will save it let if you go down with that you can also check exist by ID we can check because this will return boolean if the object present there but I don't want to check I want to fetch so it should be this method which is find by ID so find by ID in which you have to mention the a ID which you want to fetch that's it now once you get this a ID fine by ID will give you the object oh okay this there is one issue here the method find by IDs the code repository is not applicable for the argument string oh I'm not returning is it a string oh my bad a ID is basically integer right of course because we it's an integer so how can you write string my bad and once we have done that it is still giving you an error it says cannot convert from optional alien to alien now there's a beauty about Java 8 you know Java 8 gives you this option of giving cubic it keeping it optional just to handle the null value as we are we we normally use optional so we have to choice here one you can make this optional as we can do in Java 8 or at the end you can say may be all else you can okay not a good practice but for this example it will work normally we should be using alien object or any blanket in object or you can you should be losing null in fact we can also say a new alien that will make more sense so I'd why to play with nulls anyway yeah so now once we have this once we have this values we need to add that in MV so I would say MV dot add object and I will add alien that's it is that simple this should work how do I verify this so let's read redone the relaunch the application and go back to the browser okay not in the h2 of course I want to go to the page if i refresh you can see we have this form a separate form here which is asking for the a ID I would say 1 0 1 and click on submit oh it's working again so that we got 1 0 1 naveen and take this Java now same thing can be done for 1 0 4 click on submit you can see record once your for komal and Java of course not everyone is in Java if I say 1 0 5 click on submit you can see we got the week and Android now the thing is what if I say 1 just 6 now once your 6 is not available in database right and that's why if you have returned null in else part it will it will give the exception but here if I click on submit you will say you will get blank object because we are returning a new object what you can also do is you can create a fake object with fake data and you can return that that's fine you can do that so once we have done that does this I can fetch values can you update yes you can so that will be your assignment you know so create multiple methods in fact create multiple forms here for updating as well and create multiple controllers so we have let's say update alien till it alien and if you are thinking do we have our methods for that of course we have example if I say repo dot we have a method koalas delete or we have a methyl I did it all in fact we should be having option for update now in case of update what you will do is you will do it the old record and you will save the new record or I guess even save will do it for you so try it out that will be an assignment because not everything I want to spoon-feed you so try it out let because you will learn by experience so that will be assignment now see all these things looks good right but I just have one concern can we do delayed update of course we can and that will be the assignment you know how to how do we use delayed and how do we use update but I just have one query here what if you have a complex query because this code repository which you have here it provides you all the basic code features what if you want to do something extra something very different can we write our own methods and the answer is yes you know we can write our own methods example let's say if your if you want to fetch if you want to find elements or if you want to find aliens based on so whoever has a latent in the name can we do those complex queries and the answer is yes I can how to do that that we'll see in the next video so whatever we have done till now is what let me just relaunch the application just to see what we have done earlier so I will go back to our browser and I will say localhost colon 8080 and the moment I send a request you can see we are getting two different forms and of course I have not done it in a proper design but the second one is for searching example if I search for ones you know one if I click on submit you will get data for alien 1 0 1 and if you give different data you will get different stuff now in this video what we are going to do is we'll explore some more some more ways to fetch the data because you can see we are able to do cut operations right and that is perfectly working but what if you want to do some you know customized query how would I do that example let's say to just just to simplify this I will do one more thing I will open my JSP page which is home dot JSP and let me remove the first form because we don't need that as of now we just want to fetch the values right so after making the changes I will relaunch so let's just to see how it looks like and if i refresh this is what you got you know we got a simple text field and a button now what I want is I want to fetch values but not based on the a ID I want to fetch based on something else so maybe I want to fetch based on the technology so I don't want people who in fact you know let's keep it hid here doesn't matter what I enter here what I do is in the in the controller I just want to fetched I want to try try out different stuff example maybe I want to fetch from the based on the technology so what I will do is I will print on the console itself let's say I want to print all the aliens who are learning Java or who is working on Java so in that case what I will do is I will say report dot now what do you think which method I will be using here because we have the methods like find by ID which will give you only the a lien with a particular ID what if I want to search by a technology can we do that maybe something like this can I say find by tech we can see all the methods which we have used before which is save find find by ID delete all these methods are predefined in your spring data JPA how about find by tech do you think it's available and you can see we are getting errors even if I type something here if I say find tech bye it is still giving you an error is because it's not there if we don't have this method now we don't have this method where so if you see in the code repository we don't have that method but ultimately when you set up or dot find by tag it is searching in this alien report right all these methods are coming from code repository so find by Tech is not available in this interface what if I create one can we do that find by tag will give you a list of aliens so we can say these two it returns a list of aliens and the method name is find by tech let me just declare the method here we'll see how do we implement this later but as of now let me just write this method here which is fine by Tech and here I would say string Tech will this work see know where I have so I'm not defining this method anywhere I'm just declaring it is it your spring boot JPA or spring data JPA is so smart that it will be able to find it find the agency ID Tech let's try it out so what I will do is I will just relaunch it and let's go back to the browser it doesn't matter what I enter here example even if I say once the other one doesn't matter it will fetch the name of the alien but we are concerned about the console right so if I click on submit the important data is in the console can you see that we got two people or two aliens 1 0 1 and 1 0 for Naveen and Komal both are working on Java I mean can you imagine that your spring I mean your spring data JPA is helping you with the query so this is a magic right nobody is happening behind the scene can I do anything can I create can I write any method name now you have to follow some protocols here the protocol is the method name should start with find by or get by so this is what this is the option they wish they give you so find by get by so let's say if I make a mistake here so ensure of having find by Tech if I make a spelling mistake by adding find by Tech one you can see we are getting errors here so method name should start with find by Tech find by and then it should end with the name of the property so Tech is a property inside it is in class right and that's why it is working so if you conceal in class we have tech as a property and that's why you are able to search by Tech that's why take what is not working if I say Tec even that will not work so it should be dead it's that simple right so this is how you can write your query so you can do find by name you can say find by tag so this is one thing you can do can I do something something else example let's say I want to fetch the alien based on let's say a ID so of course we have that value but let's say I want if I specify a ID 1 0 2 I want all the aliens who has more than 1 0 - it's something I greater than right ok I do that so if I say system dot out dot println I would say report dot find by a ID so I want to search based on that a ID but those value should be greater than that right so we will use a method name as greater then this is the protocol again you have to follow you have to say greater then and then in this you have to pass any a ID 1:02 so you have to mentioned find by then you have to mention on by which attribute which or which property which is a ID and what you might do greater than less than so we'll go back to type O and let's do find the same method here list of alien I just find by a ID greater then and I would say this is ain't a ID now this looks cool I hope this will work because if I make a mistake here even if I say ID it will not work yeah it has to be a ID maybe GT works let's try if I say enough saying other than if I say GT or GT doesn't work so it says invite delivered query no proper no property GT found by type and it should be later then okay let's go with that and then I will go back to my controller nothing to worry about here let's relaunch the application and once it is done let's go back to the form and I will say enter and you can see we got all the aliens we got 1 0 3 we got once your 4 and once your 5 you have not got once your 2 and once the 1 because that's what we are doing here we are saying it then once your two so you can try out with less than as well it should work you know there are some things which you have to try by yourself as always ok now I want to do one more thing so let's see if I go to the apposite tree I want to let's say there is a complex query now because this thing looks cool but in real application we do write complex queries what if what if I want to write my own query is it possible and the answer is yes you can define your own queries now the way you do that example let's say I want to find by tech but then all the value should be sorted okay so did they have to be sorted by name so I will find by tech I will say Java but the values should be sorted based on names so we can do that we can say find by tech and we can have any method name doesn't after that because because see if I say sorted this is not actually a valid name because if I say at least alien so you can see this is not a valid name even if I pass a tech here this is not a valid name and that's what it's saying but since we are writing our own query so that name doesn't matter so what we will do here is we will mention hey I want to write my own query and this query would be so in the in the double quotes you have to mention the query now which query are we going to write SQL query here normally when you work with JP of course you will you will be using JP QL now if you have not learned JP QL before it's almost same as hql and if you have not worked in SQL before it's similar to SQL with some changes example in SQL we say select star from table but in jpq l we simply say it's from because select star being a boilerplate code we don't need that so what we'll do is we'll say from alien now here you have to mention where that because we are filtering right so I want to I want to check by text so I will say Tech is equal to now the value is coming from the user right so in that case you will be using question mark but in jpq l we also use the question mark numbers because in a query you might have multiple question marks so you have to mention the number there and then you will say order by a name because you want to sort based on the name okay this looks cool this is how you write your complex query you can write any complexity you want you can you can pass multiple parameters so you can give a comma here and you can pass three to four parameters it will work okay this looks good now from here I will say system dot out dot println report dot find by tag sorted and I want to sort based on Java again so that you will you know you can compare the first output and the output it will give you so let's relaunch it and just for the formality I will say enter here ultimately we need to check the console now if you look at the console see the output it is 1 0 4 and is 1 0 1 that means they are sorted based on the name so it is Komal and that means ok comes before n so that's what the that's what you can do so yes spring data JPA was like a magic so basically it's a library which helps you to get the stuff you want right you just need to mention the meta names it will work for you til this point what we have done is we have created simple application where a user can fetch aliens and user can add aliens now if you want to see what we have done till now it's raising but let's go back to the browser and we'll say localhost colon 8080 here you will get a form where you will search for let's say 1 0 2 when you click on submit you got the alien data it's perfectly working right now this is how you implement spring MVC where data is fetched with the help of spring data JPA and just to show you the code what we have done it's very simple actually we have a controller and we have a controller which is get alien and you are mapping with this latch get alien where a user is passing a a ID and based on that you are fetching the value in fact just for the experiment we have done this methods here as well let's remove them we don't need for this next example and in this repo as well let me just empty it I don't want anything else apart from the a ID now what I want is if I go back to the browser look at this URL now of course when you use a website we have a when you use a web service we have different kind of client slides we have human clients where they will interact with the helper browser but sometimes we provide a service to another services example maybe I want to fetch the data about in alien but the thing which is fetching the data is not a human it's a machine itself so one service is trying to access another service now they will not be communicating with the help of HTML and CSS although stuff they will be using some different format like XML or JSON and that's where we use web services right and one of the best way to implement web service is rest now if you follow the architecture first what it simply says is instead of having this gate alien question mark a ID is equal to 1 0 2 what makes more sense is to say your website name slash alien slash and whatever idea you want let's say 1 0 2 now this makes more sense right because it looks like you are accessing a resource so all this data is been dead on the website and the moment you say enter you should carry data so the URL which we are expecting here is alien slash 1 0 2 now if you are not family with rest api i would recommend you to watch my rest api cds or maybe at least one or two videos just to get the gist of it so you will find the link in the description area now in the registry api what you do is you say your website name slash what you want to fetch a resource which is a lien in this case and you will specify the ID maybe you don't want to specify the ID when you say a lien it simply means you want all the aliens how maybe you can also say aliens maybe I will say once your - maybe you want to fetch something else you want to fetch people or you want to fetch alien who work on Java so you can say slash Java maybe you want to fetch something else you want to fetch about laptop so you can simply say laptops right you it will give you data about laptops but here I want to implement this I want to say alien slash one zero one or maybe I want to fetch aliens for the moment I say enter I want to see all the aliens the way you implement that is very simple actually instead of using normal request mapping we will be using rest so here you will say alien slash aliens that's it it will call it now there's one change now when you save web services of course we will not be using JSP so of course we don't need a model and view object what I just need is data right when you say data in which format I will return it so time being I don't know which date which format by default every every formatted string right even if you say Jason or XML they are string format so we'll say string the method even I will say is get aliens just to make it attractive and it will not be expecting any values so no parameter and I want to fetch all the aliens okay so how do we return the value here it's very simple actually you say return wrap Oh dot now this is a method callers find also find all will give you all the aliens and this is a part of your repository right which is your code repository that is one problem when you say find all it returns it travel and what you have what you are saying is string right now in this case we to convert this into string so you will say dot to string so you are converting any tribal to a to string now still we have one problem it will not work now when you implement spring MVC by default when you say string it is expecting that I mean he when you say return string it will expect that you are returning a view name but now we are not reading a view name light we are reading a data in that case you will be saying response body so via we are informing our dispatch servlet hey I'm not returning a view name I am returning actual data so don't try to search for the view name just print whatever is there in this spawns your ass being disposed of it says okay I will do that for you so let's relaunch the application and if I go back to the browser and here if I say aliens enter case that we got our data and of course it doesn't look good but we will try to do that because this is not even Jason this is not your next sample this is just a least format in a string will try to use JSON and XML in upcoming videos but as of now let's stick to this so important thing here is we are able to fetch all the aliens so this is like a rest URL but what if I want to say alien slash 1:02 I want to fetch one resource if I say inter is not working in that case I will create another method just to show you how it works I will say enter and paste it here and the URL is alien right so alien 1 0 2 simple and the method name would be get alien nothing much I would say find so this time I will say report dot find and this will be find by ID which is once your - in this case and that's it it's so simple it should work oh it's not working there's an error we have to convert that into 2 strings I will say dot tostring ok once it is done let's refresh go back to the browser and say enter oh it's working can you see that we got our data but now if I change it to 1 0 3 oh we got an error that means we need to get another method for 1 0 3 hey that doesn't make sense right if you have hundred records you will not be creating hundred methods so what I will do is I will put that as a wild-card so I will put that in curly brackets I would say this is I this is a ID and these are it works oh no anyway you want to keep as a wild-card you say curly brackets a ID and you will accept that a ID here so I will say int a ID and you will pass the a ID so that means you are making it wildcard so it doesn't matter if a user says 1 0 2 or 1 0 3 depend upon the value it will fetch the data for you so let's relaunch it go back to the browser say enter there's one more thing here wait to use me to use and we show you here which is called as pattern variable with its path variable actually so we have to use a path variable and we have to say AI D is equal to AI D so whatever it is coming from here will be assigned here now let's verify and if I go back to the browser say enter oh it's working can see that it's working so when you say once you're - it's working once your three is working if I say 1 0 4 it's working right so it is working for all the values so that's how you send a request okay so get is walking so that means I'm able to fetch all the values what if I want to submit values what if I want to create an alien and send it to server now of course I need to create a form but that will be HTML we have to get a form light what if I want to send data in JSON format and in fact here as well I want to fetch data in JSON format we are not doing that how would I do that that we'll see in the coming videos til this point what we have done is we were able to create a resource so when a user is asking for aliens we will return a JSON or the least of aliens and when a user is asking for a particular alien you will return that particular alien so we are trying to implement rest here so if I go back to the browser if I say page ok so if I say aliens it should return the least of aliens and we are getting that and you find it if I ask for particular alien itself once you are - we should be getting that now I just have one concern here the concern is I want this data in JSON format right normally that's how we share data either JSON or XML the data which you are getting on our screen is not appropriate right how will another service understand what you are sending now in that case we need to convert our response in JSON format spring should be doing that by itself right why we have to do something now if you look at the code we have made some mistakes not exactly a mistake but then it's not proper now the first change I want to make is issue of having code repository let's use JPI repository so that will get some extra features because JPI depository is a user interface which extends paging and sorting repository which indirectly extends code repository so we have all the features of code repository + extra features now when I say extra feature this is what I want you know when I say find all it should return at least because in code repository it returns it Treville so let us go back here in the controller and so we have made that change ok let's let's talk about this one when I say find by ID it will return you an alien right so what if you don't convert that into string let's kill let's keep it alien now the only thing which it will ask you is to cut is to change the return type from string to optional alien and that's what you will be doing so will be converting the return type from string to optional alien now when you say optional alien if you have not gone through Java 8 videos that's fine it simply turns an alien object but in case if you don't have a data it will return in it will return optional data now here we will say again we will remove 2 strings so we say find all but then here will say will not be keeping it as string we will be keeping it as a list of aliens so once we have this code here let's relaunch the application and go back to the browser so we'll ask for aliens and yes we are getting least of aliens and look at the format now this is a JSON format right and what if I want only only one alien it's a 1 0 3 you got the alien right you got the alien in JSON format so this is working so we are able to get the data in JSON format okay now what are the things we need what if I want to submit this data and how exactly we will be saying this in a particular format now if you have a Mozilla Firefox it does look good in when you when you have a Jason it does look good there otherwise if you want to submit some data because of course you will be sending data from client to server as well right and the format would be Jason how do I create Jason here and in rest api we have some other methods as well like we have a delete and update how do you perform those operations and that's where we will be using a client for doing that and all the very famous client which we all use is postman so we will be using postman to do for the stuff you know how to send data how to fetch data and how exactly to use this that we'll see in the next videos in the next video we'll try to submit data we'll try to fetch data and we will also try with XML you know we have not done XML yet it's only JSON here so how exactly that works that we'll see in the upcoming videos but before that just to go for a quick recap when you say find all it simply fetches the least from the database and it returns you to the client but hold on there is one question when you say it returns unlaced who is responsible to convert that into JSON because we have not done that right we should have been done somewhere you know converting Java object into JSON now thankfully in the moment dependencies if you can expand we have a Java file here which is called as Jackson ok locate that haters so if you can see we have Jackson code now this is responsible to convert your Java object into JSON yep so we can also use other libraries if you want Jason or whichever you like but by default springboard go for Jaxson until this point everything was working perfectly right but then we need something here because when we were fetching this values example if I say hey I want an alien with one zero three week or decent value but then normally if you want to fetch value it is you can do that with browser if you what if you want to send a jason how do you do that and what if you want to delete a resource and updated resource in that case you will be needing a client now or not the client which we can use here which is very famous called as postman now using postman you can you know you can send a P I request you can fetch data and you can do lot of stuff so we'll jump to this postman app so normally well initially you know postman app was available as a Chrome extension but now they recommend you to install the app so when I click on postman apps you can see four different platform they have different to set up here and usually it was only one for chrome now you can see we have postman for Mac and for Windows for Linux now since I am using Mac I'll be clicking on this if you are using Windows of course you'll go for download and for Linux we have download there now they'll pick on download here so it should get downloaded and you can install it it's very easy actually so it is getting downloaded it will wait for some time so it is getting unzipped so when I click the on the postman app or the download I will it will give me a pop-up and then we'll say okay move to a patient folder yes so we got our postman here so it is getting opened and we got a sign up now of course you will be doing a sign up I guess it is free account initially there was no sign up but then after you know once they got popular they have they are asking you to sign up outside sign in with Google on this page what you will simply do is this is where you will send a request and this is where it will maintain the history of your request request stuff now again it will take some time for you if you are new to postman you know how to access all the features here now will go step by step it's very easy actually just go to so this is up this is a place where you will send a request you can send any request doesn't matter which page is it a HTML page or a JSON resource doesn't matter we'll go for google google.com and when you send a request you will get a response and this is your response odd right the thing is this is your HTML page you know this is a Google page the only thing is it does it's not a browser it's a client we using which you can fetch JSON database using which you can send DJ so not just Jason but XML as well so this is basically a rest call API or client now this is where you will get the response right there's a place where you'll get the response now of course when you say google.com that's why you're getting this stuff right well if you send a request which you are sending on a browser so if you can see this is the request which we were using right which is localhost colon 8080 /le --n / 1:03 now when you click on send you got your JSON and it isn't isn't format right it looks cool now because this is a proper Jason format in fact if you send a request for aliens or you can say this is an array of JSON so this is a decent object which is which is an array itself and then we caught multiple JSON object inside that now what if you want to send different above request let's say post get delayed how you do that so this option here so you can see we have get we have post we have put there's so many options available right we will be seeing delete and delete and put later but as of now get is something we will be using and when you send a request you will get a get request now can i specify hey I don't want to use JSON I want to use XML now there's a concept of content negotiation again we'll talk about that later but yes we have an option to do that so you will go to headers and you will mentioned whatever data you want this is where you as I mention the header whatever data you want whatever the data you are send into this server you can specify all that here now we also get this data straightest thing which is or doing it okay if you if there's any other let's say this was not found it will send positive for which is your favorite response time everything is available here you know so this is the best place you can test your API is what if you want to send some data so let's say if you want to send some data you will use a post request and this is where you will send data so everything is available I can we'll be doing that a lot in the eight subsequent tutorials but as of now in this Oh the important thing was how to use a postman and how to download it so in the last video what we have done is very simple actually we have installed a postman and we were trying to fetch the data from the from the server now the data type was Jason right because it isn't JSON data as of now but then when you talk about web services we have different formats you know one is Jason the second one is XML and people still use XML and what if you want to use XML because by default you are getting Jason right now of course when you have a web service you need to provide both the format's in fact there is a concept of point in negotiation now it is quanta negotiation as a server you have options of providing data in different formats like XML PDF or Jason now as a blind client will request different above data let's say client say is hey server I want I want Jason now so we'll send a JSON data what if a client says hey I want XML a server will send XML data so data will be same but the format will change now what do you think do we have an option on a server side that we will only provide Jason what if a server says hey I will not be providing PDF I will not be providing XML can we do that and the answer is yes that's where we have a concept of quanta negotiation where a client will specify what they want and so I can say hey this is not available or we know when client says Jason server says ok I have a JSON ticket how would I do that now first of all let's go back to postman and since our service is running just means let me just relaunch the application once and I will go back to my postman now if you can see this is the request which we have sent last time right I will do the same thing again I will click on send now you got Jason right by default Springwood provide you Jason now what if I want to specify hey I don't want Jason I want to work with XML now will sever provide that let's try now in order to request for XML data what you do is in the headers you know if you news when you see this page this option of header in the header you mention I will only accept so these are there's a key which is except again you don't have to by heart all this stuff you know it will give you suggestions but the moment the moment you start working on stuff you will get different types so we have accept and in this except you have to mention hey I want XML in fact by default is Jason right so if you want just new can type Jason here so it is application slash Jason if you want XML you will simply type XML and in this we will select application slash XML now once you have specified that click on send that means you are requesting for XML and if you see the body it's empty unfortunately XML is not working but what went wrong look at the status here and that's why these stages are very important so how do I mention that in springboard now spring we were thinking spring boot will be awesome right it provides so many features and now spring boot says hey I don't support XML that's weird right now the thing is in the modern world we are using JSON a lot you know most of the services are based on Jason not a lot of people they use XML and that's why spring boot by default supports Jason what if you want to add a support for XML you know that that's a good thing you can add a support now to do that you need to go to your palm directional file and this is where you will add a dependency for XML because if you say maven dependencies as we have talked in the last session that we have a Jackson code now this is this is responsible to convert your normal object into Jason right you can see we have Jason added but then there is no XML converter here so we have to add that now where you will get that and what's the name of the converter so you have to go to your mavin repository as usual so I will type MV and repository and here I was searched for Jackson data format XML now this is the first link which you have to click this is the this is the link now which what you select now to verify this which wasn't you want you just go back to your package manager or package Explorer and here you will find the version of J's Jackson which we are using for any Jackson which is two point nine point six so just to make it compatible we will go for the same version for data format I will click on two point nine point six I don't want to comment I'll copy this code go back to your blog back to your code and paste now this is where you are adding a support for XML okay it should work now let's go back let's relaunch the application once and we'll go for postman now and click on send Oh something went wrong maybe the server has not started properly so now you can see when you send a request you got XML and finally we got an XML right so if you know it supports both you know if you say if you say I won't XML it will give you XML if you say Jason it will give you Jason so what your format you want it will give you that and in both the cases you will get a status of ok because your server now supports both so click on send it's working so this is the power of point a negotiation you know depend up on client you can send a response so you are you are sending both a client will say okay I want XML I want Jason so that's how it works now what if I want to modify on the server side I don't want to provide a Jason I only want to provide XML can we do that can we restrict because pling now supports both right so whenever I send a request for Jason this should not work so this is what I want so the nightly constant it should not work I want on I want to provide only XML now in that case I will go back to my code and okay what are the changes we have to make so this is the method which we are using right right get a lian's and when you send a request for aliens this is what it will be calling now to do that to restrict it we need to add one more attribute so normally this aliens is the attribute of path so by default when you don't specify the attribute name it is part the second attribute which you have to use here is called as produces so you can mention what it produce and in the curly brackets you can mention hey this one only produce so I will say double quotes application slash XML so this will produce only XML this method is not going to produce jason because if we don't mention that by defied produce both but what if you want to restrict so you can mention the moment you mention XML it will only support XML now it will not support Jason let's verify let's relaunch the application and here if I click on send so you can see Jason is not working now we got we also got an error it says status4 not six not acceptable so this is the power of you know we can we have a power on the server side with what format to support so when you say some of it will only support XML now so everything looks cool right it's it's very beautiful whatever we have done makes sense now what if you want to send data because at this point we are only fetching data right what if you want to send data now what I want is in my database if you can see we only have I know five to six records maybe I want to add more because when I say get aliens of course the reason is not working so we'll go for xml so xml and click on send you can see we have this records we have five records i want to add one mode and i will be sending the eternal json format and for that i just want to support it to use okay let me just say one zero three three and okay i want it i want Jason for this one because we have restricted only for one method right so this should do should work with this so you can see we got Jason for this request because we have restricted only for aliens now I want to create a resource I want to create a new resource so I want to say once you're six maybe name would be Priyanka and the tech is let's say Android or maybe maybe machine learning how would I do that so that will say in the next video and the next video we'll see how do you send data to the server and how it will be accepting it on the server side and how it will be saving it so till this point what we were achieving is we were able to fetch data from the server and we were able to know see that on postman so we try to using Jason and the next ml in this video what we are going to do is we will be posting data on the server so we'll be using a post request to send data from a client to the server now in HTTP we have different methods you know we have get for fetching data we have post for submitting data we have put to update data and we have delete to delete data of course now we'll use a post here so before that what I will do is I will remove this video says because I want to support both now I want to work with gate as well I want to work with Jason as well as XML in fact you can you know Indy produces itself we can give a comma here and you can mention Jason but let's remove that you know it will support both now now after doing that what I want is I want to send data on this server so when I say I want to send data how would I do that so I will go back to a postman now any way you want to send a deed you will use you will use same URL first of all you will use a URL which is a lien now I know it's confusing right because we already have a URL which is a lien where you are passing the ID as well the difference between the new URL which we are going to use now and the old one is the type of request so we'll use a plug-in where it will be clear in some time so we'll use a post request now and I want to send a data now how do you sent inator so to send a data we will be using a body here so these are this option of body in which you will mention the data now of course we have three attributes right we have a ID we have a name and tech now to do that we have multiple options you know you can use a form data we can use or we can use binary format I'll be using a raw format here okay in fact also we can also use fontina let's try with form data first now in form data you have to specify the key and a value pair example we'll say a ID and the value I want to go for is 1 0 6 and then we have another key in which we have a name which is let's say in this case I will mention Priya and then we have tech and I would say machine learning so we have this three attributes right we have three attributes and we have three values now how would I send DITA so its first offer it's a post request this is the URL and then I will click on send okay I got an error it says four not four of course right we have not done anything on server-side earth so this source is not available so we don't have anything which will accept the question so I will go back to my server now in this we need to create a method which will accept it now in this case we will be using the same method here okay which we have used before now there will be some change in URL example the URL here will be alien now if you look at this two URL both are alien right so both are for alien but there is one difference this will be for get require this is for post request and this for get request how would I mention that in order to mention that we will be using a separate you know in fact we have an option here we can specify comma and then we can mention method type but you know in the in the new spring option we have another option which is post mapping and get mapping so here we can use a post mapping and whenever you want to go for a get request you can use gate mapping by default is get but you know it's better to mention get mapping post mapping a trick it makes more sense now this is one more thing you know if you don't want to mention response body every time because if you are building a list application you don't need to mention response body every time you do you just need to do one thing instead of using controller we will be using a rest controller now rest controller simply means that all the methods will be working for the rest you know it will be sending JSON or XML response and then it will be working with restful api oh it will be a restful api okay so those things are done so we are sending alien request and it will accept an alien now we don't want to return a page we just want to return you know the same data which we got so i will say i want to send the same data which i got so i will return the alien itself that said it we are accepting the data and then we are adding that a 10-8 elevator base and then send get will this work let's try so what I will do is I will just launch the application so let's go back to the postman and here if I click on send again oh it's working can you see that we got the response back the same data okay but is it is it there on this server and the database let's verify I will go back to my browser so if you remember in order to access your database we will use h2 - console will say connect ok so let's find a query and it's there can you see that we got 1:06 we got RIA and ml yeah it's there in the database so it's working in fact we can also verify by sending a get request we can say aliens / 1:06 is it there yes it is there right so that multiple ways of verifying it so that's how you send a data in fact you can send a data directly using so let's say if I if I go back to the body and if I say hey I don't want to use form data I want to go further what you know you can do that you can type a Jason in fact I'm lazy here so I will just use one of the best feature copy paste and I would say this is 1:07 and we'll use a name as Gordon and we'll use a technology let's say the products okay and in this case I will click on send okay we got an error it's not working what went wrong here okay so what went wrong in fact if you observe the database it is inserting 0 null and null now in this case when you use odd or D tau what you have to also do is on this server side so we have to use one of the annotation which is called as the quest body so when you are sending using as a raw date of each day so normally that's how you do it right that's how you work on web services you send other data so we have to use a special edition colors request body just to enable it so that relaunch the application and open postman and click on send so when I click on send you can see record one mode error now we have to change one more things in shop sending a text we are sending adjacent that's what we have to mention so we'll select Jason here click on send and it's working you can see we got a ID as 1:07 we got a name as Godin and robotics so data is getting added we can very firm to from database it will work in fact we can say 1:07 and let's send a good request just to verify viz there and it's there I can see that we got the response which is there nothing wrong everything is working if I go back to my server what if I want to specify what about D time consuming maybe I don't want to consume because see when you say produces and consume so these are two different words we have to remember whenever you send data from a server to a client so a server is producing right so that's a produce when a server is accepting data from a client that is consumes okay so we have to remember those two words so whenever you except data from a client in that case you can mention what type of data you want to accept so you can mention so this is four parts then we can give a comma then we can say con Jim so you can specify what you want to consume so you can say hey I want to consume only JSON so you can say applications or JSON and now when you send data in XML format it will not work so this is how you consumes data on the server side so everything works the gate is working the Porsche is working what about update and delete that we'll see in the upcoming videos so what we have done till now is very simple if I go back to my postman and if I send a URL so in fact first of all let me just run this application so I will say run so go back to the postman and here I would say localhost colon 8080 /le ins now when I request for aliens you got all the agents which are there in the database right in fact what about aliens we have added in the last session now since we are relaunching the application it you will lose all the data because we are using in-memory database of course you can use external database like MySQL or Postgres but as of now this is working perfectly now get is working post is working so we were able to send ETA we were able to fetch data what about updating what about deleting so let's try deleting first so I want to delete this data which is let's say one set of work Komal job I want to date this data now to delete a data of course you will be selecting a delete operation so you want to delete a novel it updated there and then here you will also mention the IDs so they will say alien slash I won't delete one set of four so you have to provide your primary key or whatever field which is unique of course right normally it is always a primary key so when you say one zero four that means you are deleting a record which is which has an ID as one zero four and the moment I click on send you will get and otherwise that there is no error oh it's it says okay that's weird let's verify let's verify by going to the browser and as usual we'll be using our h2 console to verify that and I will spare you filing a query here okay so you can see data is still there and it is not getting deleted so if if you are getting data it should be retreating right so it's not working so delete operation is not working properly so let me just try once again I don't know what went wrong there I'll click on send okay you are not getting in here either but it is not working right now in order to perform or delete operation on this server side there should be a mapping right we don't have any mapping here so what I will do is I will create on the mapping so it's a public already turn in the alien and I will not name this method as delete alien now while you are accepting you will accept a ID and that will be path for you and here I want to return an alien but from where you will get the alien of course from using a get method right so first you have to check do we have that method before deleting as I would say a typo dot so first let's let's fetch the object right so if I pass a ID here we will get that particular alien right so I would say alien a is equal to so we are getting a reference of that and let's at the end return that as well and then here we'll perform a delete operation because see of course we have to return right so we will let a user know what is getting deleted we want to delete it right so we'll say report dot there is a method which is called init in which you have to pass the object so you have to pass the eaten object okay so we are passing that so you can see we have a alien a and we are passing a here we are entering the same alien okay now what should be the mapping should I use request of course not right request is for in request you have to mention whatever method you are working with should I use get mapping notice forget and you guessed it right we will be using idyllic mapping and in this you will mention whenever a request comes for alien now look at this you know we have delete where we have the in fact this method it has URL alien this is also alien and this is also alien the differences the type of request we are working with so this is delayed this is get and this is this I mean this is get and this is also get but then this is post so what is missing here so if we have to say a ID right we have to mention that which which ID you want to delete it right so that was a mistake so let's relaunch it so we learn from our mistakes right okay let's go back to the postman and now click on send oh we got an error it says so I guess what is going wrong here is now since we are deleting data of course the object will vanish right and when you are saying you have this a here this is still in a persistent state and that's why when you when you try to delete that it is it is lost right and once you have lost the data how can I return that I guess that's the issue I can we'll do more research on this later but this is what we are facing now so timing are whatever return is I will say deleted that's it and here we'll say string so that I know at least we'll get some response we launched it and I will go back to my browser just to verify whatever we have done is working and I will click on send oh it's working we got deleted so we are here in the browser let me just copy this query and run and run and you can see we have we don't have that one sit afford now so that was the issue once the object gets deleted you cannot access it right because it was in the persistent state so that was my bad so this is how we work with a delete method what about what about update how do you update data now in that case I will go back to my blog I'll go back to our postman in case of update what you do is we used a put method now when you say put it simply means you want to update the existing data and you can also create a new resource okay so in post we only created resource input we have a choice if the data is available it will get updated and if the data is not available it will be getting created now in that case I will go back to the blog body first of all I want to fetch some dummy data I will fetch 1 0 5 because I'm lazy so I will just copy this and I want to update the same data so I will say put go back to the body and I will paste it here now the data which I want to update is Rafiq has moved from Android to iOS and we are sending a JSON request ok that should work the moment I click on submit ok we got the same response but is it updated on the server you can see no it on the front of some of you are getting Android ok so it's not working so we don't have a method alright so I will go right to my stairs and here I will create another method for update and this would be put mapping and we'll say path is equal to slash alien the same stuff but this time we will be accepting the object right it is same as post you know so what we can also do is we can simply copy this code as it is because this is what you are going to do and paste for put the only change you will be doing is the method type which is from post to put and here instead of saying add alien will say update alien or maybe we can say we can say this save odd update alien because if it is there it will be updated it is not there it will create a new alien and will say save alien and return alien let's verify which is working so what I will do is I will relaunch the application so put basically means update okay so it is created update once we have done that go back to the browser or go back to the postman we are not supposed to pass once your five right it is supposed to be localhost colon 8080 /l Yin because we are not passing any ID okay so that's the only thing you have to consider so if I run this code you can see we got updated value which is iOS and now if I go back to database just to verify let's fire the query and you can see that we got Rafiq as iOS developer so that's how we can update and what if you don't have these shows let's say if I I will go for the put I will go for a put here and then we'll create a new new data let's say again Priya because we don't have pre in the database now so I will say ml and click on send you can see it works we got a response and if I go back to database just to verify Priya is there right so it is save or update and that's how you use put so that's it we have worked with get post put and delete right okay now I just have one thing now let's think about it what we are trying to do here is we are creating a rest application which can do code operations now what if I make this work more easy I mean think about this controller if you look at this control it is doing nothing right it is simply accepting the request and calling the methods that's it don't you think if you have a controller which is only accepting the request and calling the methods we don't even need this controller what if the request is die activating Apps accepted by this repository that will be awesome right as a developer you don't even have to create this entire file just imagine now I know you'll be saying it's impossible but wait in the next video we will I will show you how do we do that in fact let me just reveal this surprise the method the concept name is spring data rest so in spring data rest we don't have to use MVC that means we don't have to use a controller and that will be awesome in this video we'll talk about spring data rest now trust me spring data rest is magic now to understand that magic let's go for the previous example which we were doing in the previous series so what we have done till now is in fact we'll be doing the same thing but with the help of spring data at rest and trust me it is damn easy to work on so what we have done is we wanted to fetch all the aliens now what is alien basically alien has three properties a ID a name and Tech and then we have a database where I mean I have inserted some values in fact we have this data to SQL so I have inserted this values in the database now let's see if I want to fetch an alien if I want to add new aliens what we need is a controller right so whenever you send a request request goes to the controller now depend upon what type of requests you are sending if you are sending I'll get requests this will be getting called so this forget requests this is for fetching all the aliens and this is for put which is update basically then we have post and then we have delayed so what one of the files we need we need a controller then we need a repository value which using which you can perform the operations and thanks to spring D tau we got this feature where you don't that write much code so everything is working perfectly right but then think about this controller so normally when you work on spring MVC rest you need to cut create a controller and if you look at this control it doesn't makes any sense right because this controller is simply accepting the request and it is performing the operation so I want to do the same thing but without controller is it possible and the answer is yes and that's where we have spring data rest so let's implement that so what I will do is I will just close all the windows here and let's start from scratch so I will close this project and let's create a new project so I will right click here I would say new spring starter project and we'll name this project as spring data rest and then we will click on next now when I say spring data rest what are the modules which I need now first of all we need database right so of course we will be using h2 we don't have a choice right we need database may be masculine h2 now since we charge we are just testing it so embedded database will make more sense we need JPA because of course we need repository and we need rest repository again I have worked on the project before that's why I'm meeting this frequently access area but in case if it is not there in your setup don't worry just search for it here example if I search for the rest this is what we are selecting inside web we have the rest repository okay so we have selected h2 JPA and rest repository we don't have to go for web here because we are not making a web application now click on next and then click on finish that's it this is what you need to add so we got our project here and if I see the dependencies we got a lot of dependencies here now what is the first thing we need now of course we need to configure h2 right and where do we do that so in the resources we need in the application properties we need to specify some properties for h2 I will not be typing those again because we have done that in the last project so let's let me just copy those properties here and paste okay now what are these property is very simple first you have to enable your edge to your database and then you need to specify which platform you are working on and the URL now once you have specified your properties the next thing we need is initial data right of course I can create this file by myself but then just to save the time I will copy this file it's very easy actually you just need to create a SQL file or a normal file with an extension dot SQL and then just add your insert statement there and put that in resource folder that's very important now once you have done with the application properties and the data to SQL ok it looks good let's create a repository right so we need to in fact before that we need to create a model class so in this demo I will create a new class I will name this class as alien and let's put this class in a model package now this class alien will have certain properties the first property we need is will say private int a ID and we'll say private string a name and private string Tech and for this we need getters and setters so let me do that get an setters select all click on OK so we got our and I also need a two-string method so I would say so's to string that all the variables which you want and we got it okay now since this since we are working with JP is of course we need to add this annotations right the first thing we need here is at entity this is this is how you mention that this is a this is eligible for database let's import the package and for a ID I will mention this is a primary key and then I also want to create generate the value by itself so I would say generated value I want it to be auto increment basically now this is done we have done with the ID and generated value so it should work next thing I need here is a controller oh that's that's good right we don't need a controller here because we are working with spring did a list directly we go for a depository so we'll go for interface and this interface I will name this as alien rapport the same thing which we have done before and click on finish now in this this angel repository will extend one interface which is JP a repository because we want all the features right now example when I say all the features I'm talking about you know code operations edition which is adding new data and then deleting the old record all those stuff let's input the package now once I've got this interface it should work right everything we have done you see see is so simple we have not done much and I have simply created the agent which is a module class and a repository there's no controller so if you want to build address services you don't basically need all the extra features right even you don't need bill you don't basically need MVC for that but will this work now there are certain questions we need to answer because if I see the controller let's go back to the controller let's see how a normal control looks like so if you look at a controller we have a name here which is aliens okay we have an alien name we are mapping it with some something right so when I use a send a request we have a lot of things here which is which is used for mapping in fact this one how will you pass this value if I want to pass some specific values now is it your spring boot data JPA or data rest is so smart that it will give it to you okay so okay it should but then it requires only one thing and that one thing is you need to use and annotation here so it is called repository rest the source and in the bracket you have to specify two properties so we'll say control space or two attributes one is collection resource URL the base URL you are going to use I would say aliens and then the part so part is also aliens so you have to specify these two attributes that's it and it should work now because whenever you send a request you have to make sure that you are using aliens here now that this let's see if these workings I will just close this I will just stop the app store big old execution and start the new one so it's a spring data rest run as spring boot app okay now how do I verify this if it is working I don't know let's go back to the postman and we have some earlier execution let me just close those things and here I will send a request I would say localhost colon 8080 slash now whatever request I should send I will be sending aliens because this is what you have configured in your deposit right aliens here click on enter oh we got our data and if you can see it also has certain links now if you have seen petioles it we have to provide links as well right and it is doing it for you normally we do that manually but your spring detail rest is giving you the links as well and you saying that if you want to access any resource this is a URL we will be using let's try so if I click on this URL click on send you can see that we got data for one if I fetch about five oh it's working you are able to fetch all the records what if I say zero six let's try click on send or we got because you can see we got this status as positive for awesome so gate is working what about what about I'll go for post I want to create a new resource now before that I will just fetch this source so that I can copy/paste I want to create a new resource I will copy this code and then I will go back to post body and I will send raw paste make sure that is Jason and I will create a new so say Priya machine learning and the IE is okay I will say this is 1:06 because anyway we will be using auto-generated so I will say 1:06 and then click on sand does it lose it working no it's not working there's something wrong here it still go oh okay the thing is I should not be putting the slash 1:05 again I am a geek making it making the same mistake again and again click on send it's working can you see that or we got it one or the issue here the issue is that's my bad because in the data file you remember in the project in data file I started the numbering from one zero one that was my bad you know I should start numbering by from one so when you say auto-generated it started with one okay and in to access that now we have to use one okay my bad it was my mistake but that's fine we will delete it so it's a delayed and I want to delay it to one done it says no content is it because it is not treating a content I will fetch I will try to fetch all the aliens now once your six gone is it yeah once your six gone okay that's that's good so you can add rake you can do it in fact we can also use put for putting we have to update detail right and I will be updating one stuff once you have three this time I don't know is once the three it's per min so let's copy this and I will try to put so which is updating basically and I will go to go to the body and here so whatever the update let's say plumbing is not working on Python let's say Praveen is working on AI and then that's when update I want to go for and I will click on ok and is it updated yes we got the updated one which is a I so you can see that we got all the operation you can tell it you can put you can post so that's the magic of offspring data as West right now how it is working behind the scene now since you are mentioning aliens here so it is it's something you can assume right so the venues on the project it is localhost colon 8080 and the URL which you are mentioning is alien and anyway all the methods are pre-built in this JPL repository that's the power of spring data and rest says I will map it with the help of this URL so it's it is the power list because you are getting that all the features of linking and you got a rest or you got detail using which you can perform the operation so that's how you work with spring detailed list in fact you can do some advanced stuff let me know in the comment section if you have done that so I mean what you might you to explore more dance printed a list so that's from this video I hope you enjoy let me know in the comments section and that's it thanks for watching everyone
Info
Channel: Telusko
Views: 1,585,430
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, spring boot, autowire, dependency injection
Id: 35EQXmHKZYs
Channel Id: undefined
Length: 168min 8sec (10088 seconds)
Published: Fri Jun 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.