Transcript for:

welcome back aliens my name is Av ready and in this video we'll talk about spring framework and microservices and in this particular video we'll learn about spring framework how spring framework Works what is spring MVC basically how do you build a web application using spring how do you work with database connectivity how do you create a project using spring framework and how to connect with database as well not just SQL databases but no SQL which is mongod DB and then we'll move towards Spring aop also how do you secure your application where important right and that's why we'll talk about sing security and then we'll move towards microservices now of course when you move from monic application to microservices how do you create a project in monic how do you break it down into microservices we are going to learn everything in this particular video now when you look at this video the spring part we have used spring five there so spring five and spring boot 2.5 up version and for micro Services we have used version three so basically just follow the video and go with a specific version which I which I'm using there most of the content will relevant for spring 6 as well it's just that in Spring Security there have been a lot of changes recent time uh so at that point just just try to check for the recent code and I will try to push the recent code of Spring Security 6 in the description so make sure that you check the description for the code as well and we'll try to push the recent one as well with the spring six and the older one which is spring five and that's specific for the Spring Security other will work as it is now I hope you will enjoy this course of course a lengthy video but once you complete this entire stuff you will know how to build application using spring and microservices and also the prerequisite for this course is Java of course if you know Java you can start with this course otherwise look for the co Java video on this channel of course Co Java jdbc something uh which is prerequisite for this course and if you know the concept of Serv JSP it will be helpful if not still you can go for this course and somewhere you have to it will take more time for you for for you to connect what I'm trying to say if you don't understand what the savet is but I would recommend if you don't know savet check out the video in description you will find a code Java and Sal videos there so I hope you'll enjoy [Music] this now when you say spring it basically means spring framework right we're talking about a framework here and spring is one of the best framework available for Java yes we had different Frameworks before example for web development we were using STS in the Enterprise Market we would happy with ejb and then on the other hand we have database side where we were happy with jpa but then now with the help of spring you can achieve everything the one thing which is amazing about spring is it focus on pojos now remember when you talk about ajb it was all about entities right they were good you know you can build huge applications which are stable scalable using ejb but then still the components which we were using is very heavy in terms of the size or the services it uses but what if I say you can do everything with the help of poos now if you don't know what this pojo means you know if you don't know what services means that's completely fine just for this course you will get everything but then if you know about poos yes we can use poos in spring now spring is also the combination of multiple projects it's not just one project which you'll be using example let's say if you want to achieve a very common feature of spring which is dependency injection we have a separate module for it if you want to work on web if you want to make a web application we have a separate module for it and of course one of our favorite if you want to build a rest application we have a seate module what if I say spring also can be used for security that's awesome right as a developer we are focusing more on application development we are not focusing on the security part of it and spring will help you there as well if you want to connect with database with the help of some jpa spring has a different module so spring is basically a collection of module it has right and then we'll be going through most of this modu so we'll be talking about spring code which will help you for a dependency injection we'll talk about spring MVC which will help you for web development we'll talk about spring rest to make rest apis and this goes [Music] on so before going for the actual modules let's talk about one important thing which is the official website of spring now if you go to official website let's go to Google and search for spring and the moment you search for spring it will give you this official website which is spring.io just click on that so you can see says Spring by pyot so pyot is basically company who has designed spring and you can use it for free uh that's great that's an open source project which you have now if you click on Spring projects you can see we have lot of projects here which we have talked about before we have spring boot again that's a surprise for you what is spring boot will not reveal that as of now we have spring framework if you want to work with data flow we have that as well if you want to work on Spring Cloud we have spring Cloud we have spring data and then this is very important Spring Security and then you can also build spring web services right which is your soap and least goes on there are so many features which we can use here of course we we cannot be covering everything in this course we'll be focusing on most important part which is spring framework so of course we have our focus is this one with this we'll also cover spring Boot and spring data will be also there and then one of my favorite which is Spring Security okay that's the official website and the amazing thing about spring is you know not just spring it's about Java we have amazing documentation same goes for Java docs we have in fact is show of referring any book you can refer Java docs and you can solve your queries we have huge community in the same way we have spring documents as well so if you can just go to the guides do we have here uh so you can see you will get a lot of help from this otherwise you can just click on this and search for spring docs and you can see it gives you spring documentation as well you can click on guides and this is where you will get the reference documentation just click on this in fact they will also give you a performance so let's say if you want to read your book in mobile so you can also use UB uh in fact if you want to get that you can simply search for spring framework ePub and this will get to the official website you can just go here okay they're giving you 4.3 5. is also also available if you click on the first one you can see that we have the reference document dat which is ePub of spring five that's amazing right in fact one of I'm a big fan of this documentation at least for spring you can refer everything on book uh the ebook which I'm talking about otherwise you can just refer online you can see we have online documentation let's say if you want to know about spring Cloud just click on Spring cloud and you can see it gives you all the options you can click on documentation and you will get it so you can see I'm clicking on the documentation you can see we got single HTML open that and you can read the entire documentation so that's how you can refer documentation yes there are some books available as well on Amazon on different website of course so you can just go there and you can buy any book I will also try to list my favorite books available for spring but then as of now the spring documentations is awesome and with this videos of course now what are the pr physics for spraying the first thing you should know is Java of course right so if you don't know Java how can you work on Spring framework in fact any framework on Java so the Coe Java part is very important uh you should know what is oops you should know what is exception handling what is threads and all those basic concept of course Loops as well the next thing you should know is database connectivity So when you say database it can be Mas with Java it should be maybe post with Java so whatever database you have worked on with Java that jdbc is very important so if you know that that will be amazing the next two is very important the first one is salet and jsb now of course when you work on Spring MVC it's of MVC model now you'll be thinking okay we have done with Serv JSP why you have to learn old Technologies what if I say in Java framework you have we normally work on Tomcat right so Tomcat is Serv container which simply means even if you are running spring framework on Tomcat ultimately everything will be converted into servlets so having their knowledge of servet and JSP will be awesome so not everything but Basics are very important example you should know how to send data from client to server then what is HTTP request object HTTP response object all those basic concept that's it the next thing you should be knowing is omm framework now it can be hybernate ibes or simple GPA everything will be fine so I would suggest if you have not seen hybernate videos it is available on YouTube for free just go there and you can watch hyate videos so that's the basic thing you'll be required here that's it nothing else uh yes there are some optional concept if you know that will be amazing you know how internet works the protocol works then the next thing is you know we have for sping security we should be knowing about ldap o the basics of it of course that will be advantaged but again once we start with a particular module in every module I'll be talking about some basics of it what are things required to for you to learn so you can just pause the videos and can go back and Google it right it's not that difficult even if you don't know KJ that's fine you can just just pause this video and know pause the series go back to my COA videos watch some of them which is really available on this website just go there and you can learn basic Co Jaa and you can come back here that's completely fine so that's the prerequisite you should be having and that's it that's it [Music] everyone now it's time to talk about the requirement in terms of software of course right if you want to write your spring code where will write it in fact the we have the simple way right you can open notepad type your code and you can run it in command line we can do that but hold on we will be building big projects right so of course if you're learning spring framework in future you will be building amazing projects and that's where if you have a good ide with you it will be an awesome thing right and that's where we have lot of Ides available you can use net beans which is everyone's favorite you can use Eclipse which is industrywide famous you can also use one of the best ID available for Java which is intell J the ultimate version of course because in community version I guess spring will not be supported so you have to go for the ultimate version and it is awesome the only thing is you have to it's not a free version but then on the other hand what if I say spring officially gives you a separate IDE it's not compulsory to use that you can still use net beans or Eclipse so the so software name is springing tool switch so if you search for it you can see it will take you to the official website just click on that and you can download this spring STS for free now you'll be thinking oh we have to learn a new IDE now not exactly if you have worked on Eclipse before spring is Eclipse so it's a eclipse IDE on top of that we have a spring layer in fact if you just go here it says based on Eclipse can you see that so yes ultimately you are using Eclipse you're not using something else is just that you don't have to add a spring plugin Eclipse to use it right you can simply use stts so you can download any version which you like so let me for this machine I will download the windows 64 bit because this is a Windows machine now the only thing is of course it will take some time for download so if you want to install this version you also need to have a jdk available so jdk is something which is prerequisite but then which one so the suggested one is the minimum requirement for this is jdk 8 because we'll be working on Spring framework 5 for which jdk 8 is The Benchmark the reason is in Spring 5 we got reactive programming it supports spring uh reactive programming now and reactive programming can be achieved with the help of lam expession which is only available in Java 8 but then spring framework 5 also supports Java 9 so that's great if you have Java 9 with you you can also Implement Java 9 features in Spring 5 but will not be focusing on that we'll be only focusing on Java 8 features with spring framework 5 uh so once it gets downloaded you can install it you can unzip it and you can use it but how to do that that will see in the next [Music] video now once you have your SDS with you of course you have to unzip it because that's a zip version the only tip is when you unzip it make sure that you unzip with the help of a small name because if you unzip that with with a big name it may give you issue the issue which I was facing from a long time uh so make sure that you give a small name May remove that version number there now once you got the unzip version you can see it says STS bundle let's double click on that now it will give you this thing which is awesome you know because when you work with Eclipse you have to download Tomcat separately that's what you do and of course Tomcat is not that heav to download but then pyot says hey when you are using SS we will give you Tom as well so it will give you pyot TC server which is pre-configured for this and then we have STS as well so let's double click on STS and we got s is here and you can see that beautiful logo of it double click on this it will open just like Eclipse because behind the scene is Eclipse right it will take some time for you when you open it for the first time because you know it will a lot of files now it will ask you for the workspace name as we do in in Eclipse so here I will give a name as spring po again you can give any name doesn't matter it will simply get a folder in your document so that in which you'll be having all your projects simply click on launch and it will take some time to launch it since we are doing it for the first time so fast forward you can see it is still opening and so that's my friend is STS for you yes it looks like Eclipse now I will close this dashboard of course you can see we have reference documentation available here as well simply click this and you can see this is so if you are new to any ID if you have not worked on net beans E before on the left hand side you can see we have a project section because this is where you will be having all your projects in the center you have a area where you write a code and on the in the below section you can see we have console here so all your logs will be printed that if you want to print something on a console that's where you will get the output now let me create my first project now if you can see we can get multiple type of projects here we can create a spring start project which we are not going to see now that's a surprise we'll see how we can you know make spring easy the second thing is we have spring Legacy project will not do that now we can create a jav project but there's one issue with this the issue is when you work with spring framework we need to add a spring dependencies right and when you say you want to add spring dependencies you have to go to Google you have to search for all the dependencies which you need and then you have to download it manually but what if I say you can do that with the help of Maven because Maven will help you here so mavin is basically a build tool which will help you to get dependencies which will help you to create a package for your project which will help you to compile your codes as well but at this point let me use M only for downloading the dependencies let me click on M project okay so you can see it will say you want to create a project yes I want to get a new project now mavin has multiple types I want you to also know about Maven as I mentioned you can just go to YouTube or Google you can search how Maven works but just to give you a brief using Maven you can create multiple type of projects you can create a simple stand on Project you can create a web project let me create a quick start project which will have nothing a simple project uh click on next now you can give a special name to it now we have to also mention group ID time I will mention com. Tesco as my group ID the artifact ID will say this is demo project and then click on finish now once you click on finish it will download the MAV dependency it will create a project for you you with the all required dependencies but what if you want some extra dependency because if you see this project will have nothing it will not have spring dependencies it will not have hypernet of course we have not mentioned that as of now so how do you add dependencies what if you need a jarfile for spring it's actually very easy you can just open your pal. XML now mavin works with this file in which you have to mention the dependencies you can see this is where you have to mention what dependency you need now if you think mer is the only option not exactly we can also achieve this with the help of griddle so you can use gridle or you can use mavin that's your choice I guess a lot of people nowadays they are moving towards griddle but again mavin also works now question arise from where you will get this dependency so you have to type it you by yourself you have to just go here now the thing is it is downloading some artifacts for us from the internet as of now let's keep it on hold this is where you have to put your dependency tags but we don't know what tab we have to type right we don't know the artifact name on for the spring framework in that case let's go to the official website again now for this we will go to Google and search for mvn so we'll search for mvn repository now this is where you can download all the me dependencies so let's click on this website and this is where you will get for spring so just go here and search for spring and you have to download spring context okay so not spring spring context because spring context will also have spring code uh let's click on this now you can go with any version my preferred version is always where you have maximum usage so you can see we have a usage of 419 which is which looks stable so let's go for 5.1.3 let's copy this in fact when you click on that it will automatically copy it so just go back to your application and paste you just have to specify that and if you go to my dependencies now on the left hand side you can see by default we have J unit but hold on very spring if you can see it says building workspace so if you click on that it is downloading all the dependencies yes it takes some time depend upon your internet speed unfortunately my inter speed is not that good today so it takes some time and you can see we got dependent denes which we wanted right so if you don't have to download this jar file one by one it will get downloaded automatically the moment you paste that code so this is working you got your dependencies and now you can write your first application welcome back everyone my name is ready and in this video we are going to talk about spring core now spring core is the first module which we discuss from Spring framework now it's quite famous because it solved one of the biggest issue in the software world which is dependencies now I'm not talking about labes okay I'm talking about the objects think about this the Java world everything is object right in fact we call Java as objectoriented programming which means if you want to achieve anything in Java you have to get object and that's great right but there's only one issue with this the issue is when you work with objects we don't just create one object right you create multiple objects example uh let's say if you want to build a laptop so let's imagine you have a laptop class and which you want to specify some properties now if you want to use laptop laptop will have certain properties right the first one laptop will have a size of laptop a laptop will have a weight of the laptop right every laptop varies in in their weight laptop will also have a color so we can have all those properties but to make a laptop work we need a special objects inside it example if you want to build a laptop a laptop needs a hard drive a laptop needs a ram a laptop needs a CPU right so we need all those stuff right apart from this we also need keyboard and monitor but imagine we need all those things now you have two choice you can build all those objects by yourself now you will say we can do that right what we can do is we of course you'll be having a class for CPU you'll be having a class for Ram you'll be having a class for hard drive so what you will simply do you will create those objects right so you will simply say CPU opj equal to new CPU you will say hard drive whatever class name you have HDD obj equal to new HDD but we have an issue here the issue is you are creating the object right that means you are responsible to destroy it that's an issue okay uh when you create the object you are responsible to destroy that the second issue we have is what if you have two classes you don't have one CPU class you have two CPU classes one is Intel class one is AMD class and now if you want to design your own laptop you have two Choice which one you will prefer and maybe you want to prefer that at run time you don't want to do that compile time you want to say hey I want a CPU but I'm not sure what object I'll be creating maybe it will be Intel CPU or a AMD CPU because both these classes extends a CPU class and that's where problem starts you know that's where you want this dependencies when I say dependency I'm not talking about libraries I'm talking about the objects because your laptop will not work without this objects so this dependen is I want it to be injected in the application right so so when I when I want to build a laptop this dependencies should be coming from outside the world so maybe I will have a consultant and say hey consultant I want hard drives I want Rams I want CPUs for my machine building and that's where you don't have to make those devices you don't have to make your own CPU you don't have to make your own RAM and that's what dependency injection will give you now dependency injection has one more name to it which is ioc in fact we can interchange them uh we have ioc which is inversion of control or normally we refer that in in Spring framework as dependency injection so further topics we'll be only focusing on dependency injection we'll not talk about ioc but again if someone says hey what is ioc in Spring framework you can still talk about dependency injection it will work in fact dependency injection is very famous you can do that by yourself as well you can write the entire application you can use some designed patterns to achieve that but then you have to do that everything by yourself you have to make make sure that your class is singlet turn if you if you have that intention you have to get your factory classes but what if I say in Spring frame work you don't have to worry about all those things because we have a moduel which is spring core in which you simply have to mention what dependency you need spring framework will give it to you I know that looks like a magic but then it's possible but I'm not saying everything is Magic okay not everything will be happening automatically you have to do the configuration I know that's a tricky part but then again we have something in future which where you will where you don't have to do everything by yourself you can reduce that configuration but but as of now if you want to achieve that if you want to achieve spring code dependency injection we have to do some configuration now example let's say we go for laptop itself so let's say if you want to build a laptop you need hard drive right so you don't have to create object of hard drive it will be done it will be injected in your application but how you do that that we'll see in the next [Music] video Welcome Back everyone till this point we have seen what is frame framework and then we have also done this setup for STS now of course right spring is one of the best framework available for Java there are some other Frameworks as well example we have STS for web development or we have hibernate for database and yes we can use all those Frameworks right now Frameworks are awesome but there's only one issue when you talk about Frameworks and that is we have to do a lot of configuration to achieve the thing which we want in fact even if you're building a small application we need to do a lot of configuration but think about this if you have so let's say in this world we have so many people building different projects but all this project will have something in common so all the configuration will have some configuration which is common for all the projects don't you think the basic configuration should be given by Spring framework itself uh from a long time you know when spring was introduced everyone loved it but then there was still the problem with the configuration but now we have a concept of spring boot Now using spring boot spring boot basically means you know uh spring boot believes that you should focus on convention instead of focusing on configuration so it normally States convention over configuration so you have to focus on your logic don't focus on the configuration okay when I say don't focus on configuration talking about the basic configuration what if you want to have something customized that's where you have to do some changes right but for basic spring will give you our spring boot will give you all the basic configuration okay now to understand that let me create a simple project on Spring boot but then don't worry we'll be also focusing on Spring framework so this video we'll talk about spring boot how do we create a project and how do we uh use some features there but then the further videos will focus only on Spring framework now here first of all let's project now first of all you need STS here which is which we have already done and then we had this project as well which is demo project in the last video but now let's do something different let's say new and let's create a spring stter project as of now it's not important to understand what is this service URL stuff the only thing which is important here is the project name I will say this is my first project of course you can give any name and then I will give a group ID which is my company name itself I will say com. telescope and then here the package name I want is T discod DOT first that's my project name and then package name let me click on next and here you can see it will give you so many options right now this is different okay this the project which we are building now is a spring boot project so when you say spring start a project it means spring boot it will give you so many features right based on whatever project you are building let's say if you're building a web project you will be selecting web here if you want to build a project with database of course you have to search for jdbc in fact if you search for jdbc you can see we have an option here uh what if you want to work with mq you can choose that you can choose post there are so many options here what if you want to implement security and that's why we have security features as well but as of now we don't need anything and there's one more thing important which is spring boot version now of course in new version we get new features as of now 2.1.2 is stable so let's go with that click on next nothing much specific here click on finish now you can see we got a project here okay is it so fast not actually you know when you're doing this for the first time it will download all the project structure and the dependencies from the internet right so it will take some time but on this machine I've already did this so I have a local copy of the Java files and if you can see if I open this we already have a Java file here which is first project application. Java and the amazing thing is it already had some code in it because okay at this point we don't understand what that code is but let's keep it there you can see if I open this m dependencies we have so many dependencies here by default and that's the magic right spring boot will give you everything which you need all the basic stuff of course if you want to have some extra features we have to add those dependencies but as of now it will give you everything which is required okay and then the next important file here is the spa. XML so if you have worked on MAV projects before you know what his form file is right so this is where you will add requir dependencies I'm talking about the jar files in this case we wanted to work with spring boot so you can see we have added two dependencies one for spring Boot and for testing purpose as well nothing much here let's create our first stuff now what exactly I want to create that we see in the next [Music] video in the last video we have seen how to create a simple spring boot application but now let's understand how exactly we can use it so let's say we have this main file here in which we have nothing but then here I want to create the object now if you remember if you go back to the theory video which you have talked about which is dependencies or dependency injection normally what happens you know when you work on complex projects that's where you need to add some extra objects so your application will need some extra objects maybe those objects needs extra objects example let's say if you build a laptop you need a hard drive and to build a hard drive you need some other parts as well so you don't have to build so as a laptop manufacturer you don't have to build hard drive you can inject it or someone else will inject the hard drive in your laptop you just have to ask for it and even you don't even have to mention what company hard drive you need you simp you can simply Say Hey I want an hard drive now based on your consultant or based on your team they will say okay let's use Cate hard drive let's use maybe Hitachi hard drive that's their choice so here what I want is I can create the object of alien so I will say alien obj but hold on I'm not creating the object I'm just creating the reference first to create the reference of alien we need a alien class right unfortunately we don't have this class okay let's create one okay when I say alien I'm only talking about so normally I refer alien so all the programmers for me are aliens in fact not just programmers the tech professionals are aliens because we don't think in real world right we think in virtual world so let's create the class I will simply click here and you can see I'm creating a class the class name is alien and a very simple class now in this class I want to have a simple method okay like the method name would be let's say public void cool code because normally as a programmer we write code so we got code and in this I'm printing I'm coding that's it nothing much we got a alien class in which you have a method is code and then we are saying I'm coding let's go back to our application here now I want to call that method which is code of course right you can simply say obj do code but unfortunately this will not work you know why it will not work because we have not initialized this object here and that's what it's saying you can see it says the local variable obj may not have been initialized now of course this OB local variable inside the main method how can we initialize it the best way we can do that is by saying new alien but that's the problem starts you are saying new which means you as a programmer you are creating the object and that's where problem starts because when you create the object you are responsible to maintain it we don't want to do that right that's we are using spring framework come on we can inject the object but who will create the object for us and that's where spring says hey if you want object I will give you the object you just have to ask for it how will you ask for it it's very simple you can just go here and say hey Spring give me a bean spring will say okay I will give you a bean but tell me which class object you need do you need object of alien class do you need object of object class do you need object of added class there are so many classes available right Which object do you need and that's why you will say okay you're getting confused that means I have to mention so I will simply say alien. class so you're saying hey Spring give me the object of alien class okay spring will say okay but then there's one issue right in Java if you want to call a method we have to use the object of course right you can't call the method without the object so here what I will do is I will simply say which class object do you need so this get bean belongs to an interface which is application context so let me repeat get bean method belongs to an interface named AS application context so if you want to call get bean we need object of application context the amazing thing is if you click on this run method so you can see we have a line here which is Spring application. Run which takes the class name itself if you click on this run you can see we got documentation of course so how do you get the documentation you just have to press control and click on the method name which you want so you can see this run method returns an object of configurable application context this class or this interface is it interface yes this interface extends application context so that means this run is actually giving you the object of application context so we can simply say application context let me import the package I will say context equal to we got the object now once you got the context object I can simply come here and you can say context. getb our job is done will this work let's try let's go back here and say right click run as spring boot app I hope this will work let's try you normally learn by erors right so let's let's see and and we got error the error it says no qualifying being of type com. telescope. first. alen but we have a class there right it's just that see who is responsible to create the object spring framework right so spring responsibility to create the object of alien and you can mention that to Spring by saying add component So when you say add component on top of your class it simply means spring framework will know okay now it says add component which means I'm responsible to create the object of alen now it should work let's go back and let's relaunch the application and it worked and you see that we got the output as I am coding it's that simple right and that's the beauty about spring framework you are not responsible to create the [Music] object so once we have done with alien think about this when you say a alien can code so I'm talking about programmers here so when you can code of course you need a laptop right or desktop so if you want to code something of course we we go in multiple steps we have to compile our code and that's where you need object of laptop so let's create one so we say laptop laptop or laptop lab here I just want to create object of laptop if you want to make it work and here instead of saying I'm coding I can simply say lap. compile of course right when you code you have to compile your code as well so I will say compile now what is wrong with this code there are multiple things which we are doing wrong first of all we don't have this class class named as laptop and second of all okay we we don't have a class of course we don't have a method as well so let's create this class of course you can write click here you can create a laptop class let me just use a shortcut for IDs let's click here and say hey create a laptop class for me and you can see it will create a laptop class for you that's a very simple class go back to alien you can see we also have issue with the compile so I will click here and I will say create a compile method and job is done in this compile I will print of course you can write any statement here let's say compiling so for us it's not important what is happening inside this method what is important is how will this alien can get the object of laptop so of course right if you want to make it work you have to instantiate laptop as you can see we are not doing it and the moment you run this application now we are saying code of course code will call uh this method and then it will call compile but to call compile first we have to instantiate laptop let me just run this application as it is I will say run as spring boot app app and now you can see we got okay it's asking for permission again and you can see we got an exception which is null Point exception in this case of course right this object here is not instantiated so of course you can use new a laptop here but then that's the idea right we don't want to create the object by ourself we are not done for the alien as well and that's where we can use a special annotation here which is named as autowire so when we use autowire it simply means that this object this laptop object will be created by springing oh hold on we're still missing something we have to mention that component here as well right you have to say add component so you have to make sure all the classes has ADD component on top of it the same thing we have done for alien now what will happen is the moment of this application of course we have a detailed explanation for this in the next module uh this is just the introduction for spring boot how it can be awesome so you can see the moment you load this application it will give you two objects the object of alien the object of laptop right and in the alien class we are asking for the laptop object so it will check hey do we have object of laptop available in the memory and the answer is yes so it will give you the laptop object here and that's the beauty now since you are not creating the object it will be managed by Spring framework and trust me it will give you a lot of advantage in making big projects as of now yes it doesn't make sense because we have a very small code but hold on now the more we learn about spring frame framework with different modules different projects this will surely make sense so that's about autowire okay so you can see oh we have not run this code let's do that so we'll say right click run as spring boot app and you can see we got the output it says compiling that means we are able to call this code method and code is able to call this compile without any issue let me repeat we are not creating the object of laptop it is managed by Spring framework so that's it from this video in the next module we'll talk about the fancy Stu I mean the a fancy it simply means what is happening behind the [Music] scene welcome back everyone till this point we have seen some amazing stuff in spring boot right we have seen how to create a project in Spring boot we have seen how to use dependency injection there and then the important concept Auto wire but then still we have not understood how spring is doing everything behind the scene and that's what we have to understand now question arise how will you understand what is happening behind the scene because in Spring boot you cannot do anything else right because everything is done by Spring Boot and that's why next few videos will not be based on Spring boot it will be based on simple spring framework now when you say simple spring framework that's where you are responsible to specify everything what is the scope of your bin is it single turn or prototype again we'll talk about that in detail don't worry but we have so many things which is happening behind the scene that's what we are going to see now so let's get started so step one we will be creating a project in Spring okay not a spring boot project a simple spring project but then question arise how will you manage dependencies I'm okay I'm not talking about object dependencies I'm talking about jar files and that's where we can use MAV right so let's create a project here I will create a MAV project and we'll name this project as okay let's say next I'm not making a web application I'm simply making code application audio what do you say a standalone software so we select quick start here click on next this is where you have to mention a group ID so let's say com. telescope and the artifact ID will be spring demo of course you can go find your name it doesn't matter right and click on finish now once you got your project now this is not a spring project okay the name is only spring but then it's a normal Maven project what we want is we want to have spring features here but why so to understand that let's go back to app and this is where I want to create an object okay first of all Which object if you remember in the the spring boot videos we have talked about the class as alien let's stick to that here so let me remove this hello world oh first of all let me just verify if everything is working that's what we do right when you create a new project we just have to verify if the thing which you are working with is working so let's try so let's right click here and say run as Java application you should see some output in the console and it says hello world that quits at least this is working so what next let's create an object object of alien the way you do that is by going here and say Hey I want to say alien object so I will say alien obj but unfortunately we don't have this class right that's what it's saying here we don't have that class as alien so let's create one so of course you can just go there and you can create a separate class as alien otherwise you can simply use shortcuts here so let's click here and say hey create a class for me and now we'll be getting a alien class click on finish and you can see we got a very simple alien class now this is where I want a very simple method which is let's say public void show or maybe public void code the same method which we have used earlier and here we'll print I'm coding nothing fancy we got a simple alien and then we got a method which is code and we are printing I'm coding okay so the idea is I want to instantiate this alien first so I will simply say equal to new alien oh hold on that's what we don't want to do right we don't want to instantiate this object by ourself that's where spring comes into picture so what spring will do now now spring provides you an amazing feature of dependency injection or you can call it as ioc which is inversion of control where you don't have to create the object it will be done by Spring framework okay so let's ask spring framework here so we'll say hey Spring give me the object of alien okay we have used that method name right now if you remember we have a very special method here which is get bean now get bean is responsible to give you the object but we have a twist here the twist is get bean belongs to some interface right that's the first thing second So when you say it belongs to some interface it should belong to some libraries right of course in code Java or the jdk which we normally use here we don't have this as a in class or inil method right inside jvm or inside J so we have to get it from somewhere and that's where we have to add dependencies for spring framework if you go to your M dependencies you can see we don't have any spring dependency here so so that should be our first step we have to add dependency and to achieve that let's go to browser so we will go to m repository this is where you will get all dependencies right and here we will be searching for spring context now when you search for spring context you can see we got it here let's click on that now you can see we have so many versions the latest version is 5.1.4 now depend upon when you watch this video if you're watching this in Jan Feb of course you'll be working on 5.1.4 uh if you're watching this later of you'll be having some new versions but you know every minor version there will not be any big updates so we can select any of this in fact I prefer the dependencies which have more usage you can see we have 504 here so let's pick up 5.1.3 okay let's copy this code from here go back and oh where to paste it and if you are working on MAV before you know we have to just go to pal. XML file and this is where we have to add a dependency so simply paste it and the moment you say save now if you're doing this for the first time it will take some time because all these dependencies will be coming from the M repository the remote one uh luckily on this laptop I've already used it so we have a local copy of it and that's why it took around 1 second to do that okay so once we got dependency let's use them where to use it go back to your app and this is where you'll be using a interface in which you have this method which is get bean okay now was interface name okay you guess it right we have a concept of application context but then before application cont text came into picture we had something else think about this if you want to make a car you have to open a car factory if you want to make a mobile phone you have to make a mobile phone factory if you want to make a bean you need a you guess it right it's bean factory so we have an interface named as bean factory let's simply say control space here and you can see this bean factory interface belongs to Spring framework Library so let's click that let's instantiate it we'll simply say Factory equal to so we'll say bin factory factory equal to oh we have a problem here bin Factory is a interface uh we don't have a class so let's instantiate it with the help of a class and the class name or the class which implements bean factory is XML bean factory that's it you just have to mention that so you have to mention bean factory factory equal to new XML bean factory and you can see we got a strike there now this strike simply means that we should not be using this class here because this is dicated of course we have some new features you got some new classes so you should not be using this but time when since we are into learning State we'll be learning everything from start right so this is the first class which came into picture to implement bean factory time just ignore that dicated strike there okay now we have a issue the issue is when you say get bean so let's say if I say Factory here I would simply say factory. getbean we still have an issue here the issue is how do you know which class object do you need so that's what you have to specify in this bracket I will simply say hey I want object of alien but see spring framework is a framework right it's not a Superman it's not a magician to know everything right so when you say alien spring should know what this alien means and that's where we have to do some configuration of course right whenever you work with Frameworks there is a part of configuration that's one of the reason why people love spring boot right we have less configuration there but since we are not using spring boot we are using normal spring framework we have to configure it but how there are different ways you can configure this there's a XML based configuration we have a Java based configuration or we can also use annotations time being since we are into learning State let's use XML that means if you want to customize it if you want to specify configuration we have to create XML files and then you have to specify that file here but then unfortunately we cannot simply pass a file name here if you want to specify the file we have to create one more object here which is new file system resource is it there file system resource yes and in this you have to mention the file name so file name is we'll be creating that file I will simply name it as spring.xml of course you can go with any name but let's go with this okay once we got this Factory object once you have specified hey I want an alien but still we are getting eror see it's not because of we don't have that file it's just that when you say get B it will give you object right example if you see it says it will give you object we want object of alien in that case in this bracket I simply specify alien that's it you can see there's no error yes we have some warning but that's fine let's right click here at least let's see see of course this will not work because we don't have spring.xml file as of now let's see the error as well so I will simply say run as Java application yes I'm expecting error here and we got an error and that's why it is saying file not found exception which is it's not able to find spring.xml okay let's created then so let's right click on your project and I will say new and this is where you'll be saying Hey I want to create a XML file that's one way of doing it otherwise you can simply say see the problem is when you say XML we have to add the DDD as well if you don't want to do that you can simply say new there's the option of spring Bean configuration file and that's why we are using STS here you could have done the same thing in Eclipse as well but in Eclipse you don't get this option which is spring Bean configuration file let's click here and let's mention the file name the file name here would be spring.xml the same name which you have mentioned in the code let's click on enter and we got our XML file this is what we wanted right and you can see everything is mentioned let's enter say enter here this is where you have to say hey see if you go back to your app it says find I mean get bean of alien so we just have to mention hey Spring framework whenever a user says alien give the object of alien class now alien class belongs to a package which is com. teliscope do spring demo. alen whenever in a code says alien you have to create object of alien I mean this alien the class alien but we cannot simply use this syntax right we have to use syntax of spring and that's where spring says hey if you want to specify this create a bean as let's say Bean every Bean will have an ID so we'll say ID equal to and ID is alien so whatever ID you want whatever name you want to use in the code which is alien in this case mention that and also mention the class of which you want an object I simply say I want object of com in fact we can use alien because it will give you the class as it is say control space you can see it will autoc complete it and that's it that's the only thing you have to mention it's Beauty right you just have to configure it by saying hey when a user says alien give the object of alien go back to your code and now let's run this application so right click say run as Java application and oh no error but we have not also got the output the reason is we are not calling Cod so you'll be saying obj do code because as a alien as a programmer you have to code right so let's run this application now I hope it will work and it work can you see that we got I am coding and that's it it's so simple so what we have done we are trying to use spring framework to get the object as you can see the object of alien is not created by you it's created by Spring frame workk I know for this example it doesn't make sense because to create one object we are doing so much of stuff right but think about the complex of application what if you building an application where you have maybe 15 20 classes and every class will have let's say two three objects you don't have to manage those objects right you to manage by Spring framework but how do we do that that we see in the upcoming [Music] videos so to this point we have seen how to create a simple spring project where you can introduce a concept of Factory which will give you the object right it works but still we have an issue here the issue is you can see the code we are getting uh it says deprecated right so this class XML bin Factory is deprecated we should not be using that of course you can search for some other class which implements B Factory and which will make it work without depcat of course there's one more thing which is important here when spring came into picture in 2003 it was basically for dependency injection right that was the main intention but later on they added some new features right every time you go to Spring website you will see some new projects and of course for new features you need some new methods and those methods are not available in bin Factory so instead of using bin Factory we can use amazing thing which is we have a concept of application context in fact if we have seen that in Spring boot right we have this interface which is application context we simply say Factory again you can use different name but then question arise how to instantiate this because application context interface right so so we have a class which named as class path XML let's say control space so we have a class which says class path XML application context let's use that now this class implements application context example if I click on click here you can see it's a class which extends abstract XML application context if I click on that it says extends abstract refreshable config application context if I click on that somewh if you go up and up and up you will find everything extends from application context are we getting there today yes we got it so you can see we have the whole hierarchy there and unfortunately by doing that I have opened so many classes here let me just close them quickly okay let's get back here so instead of using B Factory now we are using application context the amazing thing is here itself you can mention spring.xml you don't have to mention new file system resource or something like that you can simply mention the thing and you can see we are not getting any dicated now because we will be using application context a lot so now let's right click and say run as Java application we have not changed much we have only changed from bin Factory to appliation context let's click on run and we got an error it says file not found exception that's weird we have that file here right the problem is since you have mentioned class path right it will look for this file in class path so you have to move this file in a class path in a Java folder of course let's keep it there now and let's relaunch the application and now it worked we got I'm coding that's great so that's how we use application content text in your spring [Music] project in the last video we have seen how do we use application context but still we don't know how everything is working behind the scene that's important right so let's understand what happens when you say application context and then when you say alien get bean to understand that what I will do is I will go back to my alien and let's create a Constructor here so let's say a Constructor so you can see we have a Constructor here a simple Constructor would simply say says alien object Creator nothing fancy we got a simple Constructor there what I want to understand is if I run this application of course it will call the Constructor first it will say alien object Creator and it says I'm coding that's great that means spring framework will create the object and of course when you create the object Constructor will be called but my question is what happens when you comment this because now if when I'm commenting this line that means I'm not asking for alien object so the question is will Spring framework create that object for me me even if I'm not using it let's run this the moment run this code you can see we are not getting I'm coding because we are not calling code but still your spring framework is creating the alen object for you it's because spring believes even if you don't want the object it is my job to create the object but how spring knows that we will be needing alien object later it's because when you run this application the first line of execution is main method right so it will come to main it will check hey the next line is we have to create object of application context Now application context will simply create a spring container for you now what is a spring container all the objects or all the processing which you do in Java happens on jvm so inside your jvm we'll be having a special container which will which we will call a Spring container which will have spring beans oh what is Bean now any class which has certain variables and every variable will have gets and Setters is normally referred as Bean now in this class we don't have any variables and that's why we don't have any methods but then this is also a bean uh so any you can imagine any class is a bean as of now so we want spring to create the object of all these classes but how spring will know that because you can see uh we have specified a file name here which is spring.xml so your spring container will check this file and it will say okay so it's my responsibility okay so I'm spring container here it's my responsibility to create the object of alien because as a developer you have mentioned that here what if you mention some more objects if you create object of object class if you create object of some other classes if you write five beans here a five Bean tag it will create five objects for you that's what spring is doing even if you don't need the object so if I create object of some weird class which I don't even know in J it will create the object for you okay just have to mention that there so the moment you run this line it will create the object for you in the spring container it is your respons now or it is your decision now to use that object or not in this case I'm not using it but still we got the object so that what is happening behind the scene the moment you say get being alien it will go to Spring container and say hey Spring container I want the object which whose ID is alien and spring container will say okay we have the object here just take it and that's your that's the object you're getting and that's why it is working but then with this we have some more questions okay example let's say what if you have certain variables here example let's say uh let me create a simple variable here or maybe I will not be using a private variable I will be using a simple variable int uh I will be naming this variable as age of course you can go with anything every alien will have a different age right okay it can be same but normally it is different so let's say if I have a age there and I want to print the age of a particular alien so I will say obj do age so when you print age what do you think what value you will get so of course you can specify the age by default you can specify the age here here you can say by default the age is 18 whatever age you want to specify maybe 12 otherwise for every object for this obj time and I will say this is obj1 just to check what is happening so I will say obj1 and then I'm printing the age and you can see I have not mentioned the age and by default the age will be zero so let's run this code and you can see the age is by default zero that's great so before printing age I want to specify the age as let's say 15 that's the age which I'm mentioning here let's run this code and you can see we got 15 that's great but what if if I create one more object or maybe if I want to create one more reference let me just copy paste the code as it is let's past it here and this time I will say this is obj 2 not one uh let's go back here I want to call code by obj2 I want to specify the age I don't want to specify the age for two let's keep it blank and now let's say this is obj 2.age now what do you think will be the age of obj 1 of course 15 right we have specified that but what will be the age of obj 2 that's the question let's run this application and you can see both the object has the same age so either we got two object with the same age or we got only one object and both the reference are pointing to the same object that's what is happening here go back to the previous example when you say line number 18 when you say hey Spring framework I want an object whose ID is alien spring will say okay we have the object take it when you go for the second time as well on line number 24 you're saying hey Spring container I want the object with ID alien spring will say we have the object just take it it will not create a new object for you that means both the reference which is obj 1 and obj2 are referring to the same object and that's why we normally call all the spring beans as Singleton beans because it will the object will be created only once that's awesome right but what if you want different objects what if you don't want to follow single T pattern what if you want to have a different object every time you call for the object and that's what we'll see in the next [Music] video in the last video we have created this El object and then we were trying to create two objects but unfortunately we got only one object now is it a feature and the answer is yes by default spring follows a singlet turnon design pattern which simply means by default spring container will give you one object but what if you want multiple objects and that's where spring gives you a choice and that is the scope let's try so let's go back here in spring.xml and if you see this Bean tag there is one more attribute here which is missing the moment you say control space you can see we have a attribute known as scope by default the scope is Singleton which simply means that spring container will give you only one instance of this class even if you ask for 10 times it will give you only one instance example if I go back to app and let me create obj 3 obj 4 still you will get one object because by default it's a single ton what if you want multiple objects and that's where we have to use a different scope so apart from single T we have one more scope to use and that is prototype now what it means prototype simply means that whenever you ask for a bean spring container will give you or it will create an object at that point example when you say get bean once with alien spring container will give you one object again when you say get bean with alien spring contain will give you second object so if you ask for 10 times it spring will give you 10 instance 10 different instance of that particular class let me prove my point so you can see I have made it single turn now I made it prototype now so if I go back to app let's run this application one so say right click and say run as Java application and look at the output now so you got 15 for the first object and you got zero for the second object because we have two different objects it's that simple right so Singleton is you will get only one object protype you will get multiple objects based on the number of time you're requesting for it but what if you are not requesting even once you can see I'm creating the object of application context which which is your spring container but I'm not asking for any object do you think it will create object for you let's try the moment you run this code you can see it's not saying object created even once that means in single turn even if you don't ask for the object spring container will give you that object but in case of prototype you have to ask for it otherwise it will not create the object right now which one to use it depend upon your situation right so if you think there's an object you need only one object of course you will go for singlet ton apart from that if you think you know we will be using multiple objects so you have to go for Prototype so that's the scope in spring so we have single turn we have prototype we have some other scope as well but that's that's not applicable in Spring code that's for some other [Music] modules so in the last video we have seen the scope in spring right we have Singleton and we have prototype now let's go back to our alien and you can see we have one variable here which is age now logically this age variable should be private variable right that's what we do we have to make sure that all your variables are private in order to access these variables what we need is gets and sets right and we don't have it here let's go back and let's create Getters and sets for this so let's right click here say Source generate Getters and Setters select this click on generate and you can see we got gets and Setters if I go back to my app now and you can see I don't want to assign the values now of course you cannot do you cannot even use the age variable directly you have to use set age and get age but time in I will not do anything and let me also remove this second object you don't need that in fact one more change I don't want to use prototype I want to use Singleton and the amazing thing is when you don't mention any type it is by default single ton let's go back to app we don't even need this age the thing is oh we need age but then it should print using get age right so I would say obj1 dog age the thing is I want to assign a default age because if you don't if you don't Define the default age by default will be zero I don't want 1 Zer at least the age should be 12 or maybe 10 how do I mention that 10 here of course there's one way you can just go here and say 10 right by default the age value will be 10 that's one option otherwise you can just go to app when you create the object you can assign it manually you can pass it to the Constructor what if you want to assign the value in the spring Bean itself because remember the object will be created by Spring framework so the Constructor will be called by Spring framework right so I want to assign the value for age here in this XML file can we do that let's try so let me just expand this bin now in this be tag we can have one more tag here which is named as property now what is property so every variable in your class if your class is been every variable will be known as property you can have multiple properties so that's the property here you have to mention the name of your property in this case it is age so we have name and now we have to also specify value so to specify the the name and the value if your property is a primitive value or primitive type you can assign value but what if your property is of type reference that we'll see later but time let's only focus on value I want to assign the value as 10 by default the age should be 10 that's what we want here but will this work let's try go back to app and you can see I have not assigned the value for age anywhere else apart from that your XML file let's run this code and you can see we got 10 so what happens here the moment you run this application on this line you will get that particular container in your jvm in that container it will check hey we have a springl file where you have mentioned about alien object so it is my job to create that alien object the moment you create the object it will also call the set age method you know why because we are saying property since you are specifying property it will call set age oh okay I know you don't trust me here let's go back to alien and oh we don't have a a Constructor as well we have a Constructor here so let me just put this Constructor on top so that it will be visible right I also want to go to set age and by saying here age assign that's it just to see if that set me is getting called let's go back to app and now let's relaunch the application and you can see it says alien object Creator that's step one second step is age assigned so first it will create the object second and it will set the age because we are saying property right just to prove my point once more what I will do is I will go back here and let me just change the method name from set age to set age one now what will happen is if I do that if I go back to app and let's relaunch the application you can see we got an error it's compulsory to have your method name suitable for your variable so if your variable name is age it should be age right it should not be one that's what it's it's saying here you can see it says Ed creating be with name Alien because if you go you know if you go there it says invalid property age because we don't have it there the property we are using here is set age one right make sure that you do set age yeah if you want to keep it set age one in XML as well you have to say age one then it will work right okay so let's go back and you can see if I say age it will it is giving me error so let's go back to our code and it set age so that that's why this set methods and get methods are so important in Spring so that's about your how do you assign a default value but what if you have a reference type now when you say Age It's A Primitive right a int but what if a alien needs a laptop now of course laptop will be a class then the laptop will have certain methods and then we have to create the reference of laptop in this alien okay when you say reference how will you assign the particular value to it because we cannot use value there that we'll see in the next video so now it's time to have a reference type here because what we have done in Alien is we have a variable which is of type int but what if you have a reference type because what it will affect is this spring.xml file because here you can see we have a property which is age and then we are giving a value with an attribute which is value itself now to explain that what I will do here is I will create a simple class let's say because every alien needs a laptop to code right so let me create a laptop class here here a very simple class laptop and in this laptop class let's say we have a very simple method which is compile so we got a laptop class and then the method name is compile here and then in this compile you will simply print code compiled that's it so we got a compile method and then we are printing code compiled and in this alien of course if you want to use laptop you need to do that in Alien right so from Alien you will say now alien has a laptop type in it so we'll say laptop laptop and then uh we will need a getter setup for this as well as well so let's come back here and say right like SCE generate gets and sets but this time it will be for laptop because we already have it for age right now this thing is very simple right we have not done anything fancy just just the normal stuff and then from the code you will use a laptop object and you will say compile of course right when you code something you have to compile as well these things are simple even if you run this code let's see what happens the moment you run this code you will get an error and that is nullo exception but why we got nullo exception is quite simple right because we don't have the object of alien anywhere because we are saying we are using dependency injection where the dependencies will be added by Spring framework so of course if you want to work with a laptop you need to create a bean for that as well so that's a golden rule if you have a class just create a bean for that so let's create a simple bean and this will be for laptop so I will say ID laptop and then we have a class and of course we know the class name now it's a laptop right and then this laptop don't have any variable so we don't have to specify any property in this case but the question is how this alien will use this laptop object or the ID so of course in the alien we have a property which is of type laptop but what you will give the value there because if you say value you have to assign A Primitive value or maybe a string type now that will work for Primitives and string but if you have a reference object you can't use a value here because now you want to inform spring hey this alien object needs a laptop object in that case you will you can not use a value you have to use a reference so you have to say ref and you have to say laptop so that's how you can refer it right so now let's verify if this working let's go back to app let's relaunch the application and you can see it is working so basically if you want to use a reference type the way we are doing here in this alien class we have a object of laptop if you want to achieve that you have in your spring XML file we have to mention that your property which is laptop is referring to the ID which is laptop toop here right now you can have multiple dependencies you maybe if you have your class aliens depend upon some other object maybe let's say a pen object so in that case you have to create a bean for pen as well provided you have a pen class right so that's how you implement this reference stuff in [Music] spring so till this point we have seen how can we create a bean and then how can you set the properties now when I say property it is simply a variable which you can access with the of Setters and Getters now here you can see we have assigned two properties one is a primitive property and second one is a reference property okay but what if if I go back to my alien you know one of the way you can assign the value to a particular variable is with the help of Constructor of course right they have two ways you can assign the values one with the help of Setters and second with the help of Constructor so what if if I want to assign the value to AG but with the help of Constructor so let me create a Constructor here with variable of course so here I will simply right click and I will say Source generate a Constructor with Fields I only want to assign ages at this point let me concentrate of course you can also create a Constructor which takes laptop or you can create a Constructor which takes both but as of now let's stick to one particular Constructor so you can see we got age here we don't want this super method there just to reduce the number of lines we have in the code so you can see I'm saying this do age is equal to age but how can you pass this value because if you're not passing this value and by running this code let me just run this code of course it will take the value from the set up so in this case we are passing the value from set up but what if you don't do this so let me remove the property of course it will not be calling set age now let me restart the application you can see we got the value of age as zero okay so I want to pass the value the way you can pass a value here is with the help of tag which is Constructor argument but then what will happen so even before doing that let me just remove this default Constructor here can you see that we have a default fall Constructor as well so time being I will simply comment it so I will say comment save and let just relaunch the application now this time you can see we got an exception but why so it says no such method exception so we are not able to find alien in it okay but why we are not okay so let's try this it says no default Constructor found oh since we are creating this Bean we are creating the object of alien and by default it will call the default Constructor right and as you can see in Alien we don't have a default Constructor The Constructor which we have is a parameterized one so in order to achieve in order to run this code of course you need a default Constructor here otherwise you can mention here hey don't use the default Constructor use the parameter wise one and the way you can pass the value is by saying construct the argument in which you can pass the value so how do you pass the value it's very simple actually you can simply say value equal to and you can assign any value value here so let me say 12 of course you can use Setter or you can use Constructor let me just run this code now let me relaunch the application can you see that we got 12 now now this time we are not calling default Constructor we are calling a parameterized Constructor by passing this value but what if you want to assign for laptop as well what if you have a Constructor which takes laptop in that case you can specify one more Constructor argument here and that will work the only consideration is instead of having this value here we have to use ref that's how inject the Val with the help of Constructor so basically we have two concepts one is a seter injection and second one is a Constructor injection so set injection simply means when we use property this is a set injection and when you say Constructor that is a Constructor injection now which one you should be using now whenever you have a variable example when you go back to this alien whenever you have these properties whichever property is compulsory to be assigned or which is required you can go for Constructor but if if you think you have a property which is optional you don't want to assign the values to it if you want to keep it optional you can go for Setter don't use Constructor when your variable assignment is optional because Constructor will have will will give you error in this case if you don't assign the value for the age if you don't pass this Constructor age it will give you error right so that's how it works that's how we work with Constructor and Setter [Music] injection so before continuing let me just do something changes the example I don't want to use Constructor injection anymore let me go back to my property and for that we also need a Constructor here and then in the spring XML file of course I want to go back to the properties I will say undo undo undo and we got it okay now what are things we need example let's say if I go back to alien every alien needs a laptop so let's say if you're a programmer and if you want to WR a code you need a machine where you can write a code and you should be able to compile it now don't you think to write a code you don't need a laptop it can be desktop as well it can can be tablet as well so while you're saying a alien needs a laptop it can be a computer example let create an interface here and we'll say interface so we have a very simple interface which is computer here and in this computer interface I want to declare only one method so which meth I'm talking about I'm talking about method which is compile so I will say void compile now since we are only declaring it so nothing specific let's declare it here and laptop will be implementing of course you can also go for a class here which computer class and laptop implements laptop EXT a computer class but let's go with interface in this example so you can see we have a laptop class which implements computer now of course we can have one more class which is desktop right so I will go back here and I will say maybe I will just copy the laptop class and I'll paste it here and this will be computer oh we already have computer this will be desktop so you can see we have a desktop here if I open Desktop we already have a code which is compiled and desktop also implements computer so we have two classes uh last laptop and desktop both are implementing computer that's great let's go back to alien and now I'm saying hey I don't specifically need a laptop it can be any computer it can be desktop it can be laptop so I will say computer here and then here I will say com okay of course for those changes we have to change something here as well so I will say right click Source generate gets and sets for this particular variable com done okay and one more change instead of having laptop here we have to say comp that's it nothing much is all about computer now so if I go back to spring.xml what do you think will it work of course not right here we have to mention the property name as Comm so I will say com now okay now will this work let's go back to our application and let's relaunch it so the moment you relaunch it you can see it is working and okay just want to do one more change it says code compiled in a laptop and in desktop I will say code compiled in desktop okay so you can see we have one computer interface and two classes let's go back here and let's relaunch it and you can see it says code compiled in a laptop that's great so how it is connecting so it is connecting based on the property the property is comp and the reference is laptop and we got it now what do you think about desktops of course I will talk about desktop in some time but as of now it is working because in the bean tag we have specified laptop laptop since we have laptop here it will work with laptop if you want to use desktop out course you can create a bean for desktop and mention desktop here so I will copy this and this will be desktop and here as well the class would be desktop whatever you mention here in the reference if you just laptop it will create object of laptop you can see that it's working for laptop and the moment you set desktop here now it will work for desktop and you can see we got desktop so code compile in a desktop okay so I want to show you a magic here the magic is what if the ID which we are using here for laptop is also com so let's say I'm not specifying as a laptop I'm saying it's a com and here as well I want to use laptop so I will say Comm can we have same name of course we can have right we have seen that before so you can see we have Comm here and we have Comm here Let's ignore desktop as for time B imagine we don't have any desktop stuff we have a laptop here which whose ID is com and we have com here the moment you run this code you can see it is still working because of course you can have a same name but don't you think when your name and your reference is of same name why to mention that the amazing thing is you can skip it you can just remove this part and you you can say hey doesn't matter what the name is I still want to get it connected but how so I want to inform spring hey spring when you create object of alien search for the laptop object or search for the computer object you will find it but how so what you will do is in the bean property that you can mention one important aspect which is so let's go back here and say autowire so can you see that we have a concept of autowire and what is autowire now autowire simply means you are informing spring framework hey Spring if you want to set some property if you're looking for some property if the bean is available connect automatically only when okay only when the bean is available then only connect so you can see it says Auto wire it is searching for the object now so here I will say in fact we have Choice when you say this so if I say control space you can see we have by name or by type so we have two choice so it will try to search a particular element based on the name example here you can see the property name is com so the property name is com and the bean name is also com so it will directly connect it with the help of Auto Wire by name let me just relaunch it and you can see it is still working so I have not specified any property here but still it is working and that's the beauty of Auto wire trust me auto wire is amazing the moment we start working on Spring boot uh when we start working on GP and other stuff you will love Auto wire okay that works so let me show you one more now let's say I don't have this particular Bean here so I will just commented right so it is commented now and now you can see we have desktop here so as a alien you need a computer and yes we do have a desktop here so let me just relaunch the application and we got an error the error is because we have not specified the property but you will say hey we have specific autowire right in case of autowire you don't have to mention the property it will search for a particular Bean automatically but unfortunately it was by name by name simply means it will search by name oh that how do we solve it now so what we will do is instead of going for by name we have another way of achieving this concept now think about this when you say you want the object of alien and alien is depend on what so let's go back to alien and here you can see alien is actually dependent on the computer type so we are not dependent on the name actually we are dependent on the type as well so what if you can go back to Spring XML and you can say hey when you say I want to get object of alien alien is dependent on the object of computer so what if you can just don't search by name search by type so we can do that as well so here we can say hey search by type now here it will not look for the name example uh if you go back to alien it will not look for com it will look for the object of computer and in this case you can see we have it we have it here so we have the object of desktop with the name desk of course this desktop name is not important what is important is this desktop class implements computer that will be done let's try this so let me just save this code and let's relaunch the application can you see that we got the output and the output says code compiled in a desktop that's what we want right we wanted it to work and this time I don't want it to search by name I want it to search by type so let me repeat we have two options here autowire can go by name or by type so when you say by name it will try to match the name of the ID or name of the bean when when you say by type it will try to find the type of The Bean that works but then what if you have this object as well so let's imagine we got two objects one is laptop and one is desktop and when you say by type see of course when you say by name the problem is Sol right of course it will search by name but when you say by type don't you think it will get confused now we have laptop and desktop both so what do you think which one will get preference of course this should get first preference right because this that's a sequence this is the first one there one but then in terms of spring everything is object right so when you say everything is object of course it should be independent of the sequence and that's where it will get confused the moment you run this code you can see it is giving you error it says no qualifying Bean of type blah blah blah it says I was expecting one but then we found two so your spring framework is getting confused which one to assign is it laptop or desktop how do you solve this that we'll see in the next video how do we solve this error now you can see we have two candidates here we got laptop object we got desktop object and both are computers and then we are saying hey go by type so that's where your spring is getting confused hey should I go for laptop or desktop now sequence will not be considered now one way to solve this is by going here and going back to the property the moment you have a property here the way we did it before so if you say reference to desktop now this will solve your issue if you want to go with desktop you can see the moment you mention it will not consider by type or by name it will simply go with the property in fact it will avoid Auto wire so if you run this code you can see problem solved we got desktop and then if you say comp now this will be giving preference to comp right so problem will be solved so when I say comp it will give preference to laptop so this is one way but what if you don't want to mention this and still you want to go for auto wire by type yes so we have by name we can go with that or we can mention the property otherwise by type if you want to solve this see even if you have both this objects you have to give preference to at least one and the way you can do that is by using a primary attribute so you can say primary equal to true the moment you do that this particular Bean becomes a primary Bean so in case of confusion it will give preference to this one so example if I on this code now you can see it is giving preference to a laptop so even if you don't have anything assigned here even if you don't have by name it will go for by type and it will prefer this particular be you can see we got laptop now if you write primary equal to true in desktop it will be getting the first preference right so let's run this code again and you can see we got desktop so that's how you can solve this particular confusion so this is where you can use this Primary in fact the moment you start using annotations so we also have a concept there again we'll use annotation there so that's about this code we have solved the issue here welcome back aliens my name is ready and in this video we'll talk about spring boot jdbc the thing is if you want to connect your application with database in Java you have to use something called jdbc which stands for Java database connectivity and you will say we know that right so basically you can use jdbc few steps and then you can connect your application with database easily but the problem is as a developer you want to reduce your work right and also you have to make sure that the application is optimized so let's talk about the optimization part here see a thing is every time you send a request from your application to database it creates a new request and then if you have multiple connections imagine you already have few connections and now you are submitting a new request now you do have this connections right can't we just reuse the same connection instead of getting a new one now that's something you have to manually do or or you can use something called a data source here now data source will have a configuration and also it provides you something called a connection pooling so basically it will already have few connections and you can use those connections and again you can do this by using any external uh data source or connection pooling concept and you can make it work so that's one thing the second is about developers work so you have to write those beautiful seven or six steps to achieve jdbc and you can do that but the only problem is the more code you write you are basically increasing the number of lines which will have a bug so you know every new line is a potential bug right so reduce the number of lines which you write and use certain tools of Frameworks which will do it for you and these Frameworks and tools are been tested by everyone so if it works it works for you as well now that's where we have to use a template now there's something called jdbc template now jdbc template will help you with data source that means the connection pulling concept will be done and it will also help you to write your code in an efficient way and all these things are provided in Spring boot jdbc now why spring boot is because if you want to build Enterprise application and if you want to follow all the design patterns best practices and a framework where you can get the libraries and everything working together spring is one of the best framework and that's why we're going to use spring boot jdbc is because under the spring umbrella we have lot of projects and this is one of it so let's get started with the code where we going talk about how do you build an application and we'll learn by building something right so it will make much more sense so let's start the code the thing is if you want to build this project you need a IDE now you can choose any ID which you want maybe you can use Eclipse intellig vs code your choice I'm going to use intell idea and that to a community version because that's for free and it works so we'll be using that so to get the project for spring if you're using a spring tools on vs code or Eclipse you can directly get the project from there and if you're using the ultimate version of intell idea you can create the project directly from your IDE but since we are using a community version I'm going to use the spring inst viser so you can head to the start. spring.io and you can get your project from here the base skeleton of the project so the first thing you will mention is a project type I'm going to go with mavin uh language I will choose Java of course that's what you're doing now and then the spring boot version I'm selecting 3.2 next you have to mention the group ID so I'm going to select I'm going to type here com. telescope the project since I'm working with spring jdbc demo that's the project name I I will give and will not change anything here the packaging I want it to be type of jar and the version for Java I'm using in my machine is 21 now based on which version you have you can either just choose 21 or you can select 17 uh dependencies so basically we have to add uh two things only one is the jdbc API now this is your springboard jdbc and you have to add a database now which database we going to use of course you can choose any dbms which you want now we will be using a embedded one so if you have post msql Oracle mssql you can use that the only thing is you have to do some additional configuration and I will tell you where to do that configuration but we are going to use a inmemory database here and by default when you use inmemory like hed2 it will be pre-configured so you don't have to do any configuration for that so this these are the two libraries we need and that's it you can click on generate it will give you a project so as you can see I got the project downloaded which is spring jdbc demo I will just unzip it and open in any ID which you want I'm going to use intj so let me do that quickly okay so basically have opened that project in my IDE and it will take some time for it to resolve the dependencies now we have added two which is jdbc and the hed2 and we don't have to manually download the H2 driver because the moment you add H2 it will also have a driver installed so if I open the pal. XML you can see we do have H2 here and we got spring boot starter jdpc this is what we want and now we can get the work started so we'll go back here to main Java and spring jdbc demo application and we can start the coding here okay so one thing I mentioned before right if you want to use P or Mas or any other dbms variable to the configuration so if you can see we have a resources folder here and if you open application. properties this is where you have to specify those properties so what properties I'm talking about you have to specify the URL you have to specify the username and password and Driver now basically this variable belongs to spring. dat source. URL and you can set the value for the URL whichever you want so that's how you do it but now we are not going to use Mas or pus we are going to use H2 which is in memory reason I'm doing using H2 here is because you can directly take this project and run on your machine if I use P or MySQL then you have to also install those softwares you have to configure it and there will be some issues with that so let's go stick to H2 okay so what we want to do we want to basically store some data in the database and fetch it from database so we do have database ready so the moment you run this application you will be having Edge to database behind the scene running for you but then what you want to store so in that case what I will do is I will create a simple class which will represents a particular table so normally you can have a relation between a table and your class now you will say how is is it possible see the thing is if you think about a table table will have a table name table will have a columns let's say we are working with aliens so every programmer is alien right so a programmer or alien need an ID a alien needs a name and a alien needs a favorite technology so let's say for me my favorite technology is Java blockchain AI for you it might be something different so basically every alien will need a technology as well so three columns in the table and then this columns or this table will also have some data let's say one1 which is ID and naven Technology Java let's say two Gan technology blockchain so basically you can have this data in the same way if if you try to relate this with a class now in a class basically what you'll have is you will have a class of course a class will have a name a class will have properties right variables now if you try to match the class name and the table name look similar properties of this class which is ID name and Tech matches with the column names right and the objects for this alien class will be the rows each object will be one row now that's how basically you map between the class and the object and then if you if you're using any springing orm technology let's say hibernate in that case the table creation happens automatically now since we're not using hibernate here we have to create the table by ourself yeah that's something you have to do okay let's create a class here and I want to create a class for the let's say alien okay so when I say alien class what I want to do is I want to basically create few variables so I will say private in ID and then private string name and private string Tech so basically I need these three variables and for these three variables I also need Getters and Setters so we'll right click here and we'll say generate Getters and Setters I will just choose these three variables done I also need a two string method if I want to print the data itself so I will just right click here say generate uh two string method and done I will do one more thing I will move this particular class the alien class to a different package so this should be into a a package of model so every class should belong to their own packages so alien is a model class so I will just move that to a model package and since we don't have a model package I will say hey move to model package in the SRC yes and you can see it is moved now so that's done but if I want my spring framework to manage this particular bean I have to use add component now if you're not sure how spring boot Works uh you you can check out the video link in the description I will make sure that you have your spring boot video there okay so basic basically what we are doing with ADD component is we are asking spring framework to handle this object creation object managing and object destroying by Spring framework I don't want to do that okay so basically when I say I don't have to create the object how spring will do it so basically what I will do is uh this run method so I will go back to the main class so main class is your spring jdbc demo application and in this the you can see we are calling a run method run Returns the object of application context so I will say application context cont text now why I'm doing this is because I want to get the hold on a spring container again I'm assuming that you know spring boot here because if I start teaching your spring board then it will take it will make the video length of five hours I don't want to do that so again you'll find the link in description you can check it out now let me create the object for alien so I will say alien I will say alien one is equal to okay we have to basically import the package for alien which is in the model package done and now I want to create the object the way you can create the object by saying alien right but I don't want to create this object by myself I just want this to be done by Spring framework so I will say context. get bean I want the object for alien do class right uh but the thing is by default the type of this alien component is Singleton so basically the ban is Singleton I want to make sure this is prototype so I will say scope prototype so what will happen is for different request it will create new objects for you otherwise you will have multiple alien object or multiple alien references with the same object so you will have the same data doesn't matter how many data you have in your table okay so that's done and for this alien what I will do is I will add some values so let's say the ID I want to add is 111 and I want to say not get ID I want to say set ID alien do set name I want to set the name as let's say naven and alien dot alien 1. set Tech I want to set the tech as let's say Java now basically I'm adding this values and now I got my object ready I want to store this object in database how will I do that maybe I can simply say save this data in database of course this will not work so what you have to do and you know why this will not work is because you have not subscribed the channel yet so if this works on your machine that means you have already subscribed okay I'm just kidding so this will not work right we have to write Java code there now how will you make this work now basically this is where you have to write extra classes see what happens is in normal application you'll be having multiple layers you'll be having a controller so if you're building a web application you'll be having a controller layer you'll be having a service layer you'll be having a repository layer now this repository layer is responsible to connect with the database of course I can write my jdbc steps here in the main because that's a normal main method I can write all the beautiful jdbc steps here but then we don't want to do that that's why we are doing springboard jdbc here and this is not a good idea to write your JBC code inside main so this should be a part of a separate file now instead of creating a service layer I will be skipping that and I will directly jump into a depository layer so I will just create here a depository layer so I will say new class and I will this I will say this is a alien repo or you can also say da if you want now repo is responsible or repository is responsible to connect with database okay now this is where you will do this stuff but first of all uh we need to have some methods here maybe I want to have a method like public void save so it will save the object Which object the alien object alien alien into the database okay so we have to import the package I will also make sure that this belongs to a service or the repo package move to repo yeah okay so here what I will do is I will say at this point I'm not writing all the codes I will say added and then I also need a method which will return the least of aliens so let's say if I want to fetch all the aliens from the database now in this case I have I need a method called maybe find all method name you can change as you want I'm just saying find all here import the package and here let me return a new add list of aliens okay and I'm I'm just reading an empty object but we'll we'll code this in some time the idea is I want this to work first of all and since I want spring to manage it I will write add component so instead of adding add component what makes sense is to use add repository for for different layers we have different stereotype annotations for repository use repository go back here now if you want to store this data in the database I need object of alien repo I will say repo is equal to instead of saying new repo or new alien repo we will be using context. get bean the alien repo do class this is I want now once we got the object I can simply say repo dot I want to save the object let's say I want to save alien object alien one and maybe I want to print print all the objects so get all the objects from the table or all this all the aliens from the table and print it here we have not done any changes yet and we are not written any code to connect database yet but let's see if this works right click here and say run okay taking some time because I'm running this for the first time and you can see it says uh empty why is not calling oh I said added I wanted to print added what's wrong with me okay so let's run this and now now you know why it has not worked before why I said SL slash added is because I realized you don't like the videos for me anyway so you can see we got added here and then we got the square brackets which is empty basically empty at least now let's make this work so basically we have to make sure that we write a code in the alien repo this is the only file you have to work with now we have to write a code where we can store this data in the database so how exactly we going do that it's very simple basically we have to use a jdbc template now this is a part of your spring boot jdbc so what you to do is you have to get hold on there something called jdbc template you just have to create the object of this I will say this is template okay so you have to get hold on the jdbc template for this this is a property right and this is the inbuilt stuff if you can see jdbc template belongs to springf framework. jdbc Doc okay and and you will see how easy it is to connect and all the all the stuff right you don't have to write right connection con driver manager remember in jdbc we do that and we don't have to prepare this statement all this thing will be happening behind the scene but for this I need data sets I can just right click here say generate data sets for this JB template that's done and I want this object to be injected so because see we are just creting a reference of it right where is the object so spring will take care of it you don't have to worry because if you go to JBC template we can create object of this class but again for that you have to do all the configuration you can see we have to pass the object of object of data source I don't want to do that I just want my spring framework to manage it and the way you can do that is by just writing autowire your job is done you got the jdbc template object Now using that object let's say I want to I want to add this I want to add the alien object it's very simple now you can simply use the template now this template which is a jdbc template object you have multiple methods now in Normal DBC if you want to execute a statement now based on which statement it is is it a select query or is it other queries like delete update now if you working with select query which is the reading you basically use execute query as a method if you want to change something let's say update insert delete you use execute update here we want to save it right so we'll be using execute update execute update takes two parameters in fact multiple parameters depend upon how you're doing it the first one is the query of course you need a query with you so I say string St Str or string SQL is equal to what is a query now here I'm going to use a query like insert into what is my table name nowhere we have mentioned a table name right let me make it similar to the class name let's say alien and then how many fields I have so I have let's say ID I got name and I got Tech okay and then I want to specify values as well now where are the values so you tell me where are the values values are here inside this alien object you got the values we just have to pass this values here now if you remember the prepared statement we don't directly pass the values what we do is we put question marks there so that we can replace this question mark later so we are using repare statement by default just pass the query here or pass the SQL here after passing the SQL you have to also replace this question marks with the actual values so those are the remaining parameters you have so after mentioning the query just say alien do get ID comma alien. get name comma alien dog Tech that's it this will basically save it but will it work that's a question right so let's see what happens if you run this and we'll try to solve that later right click here and say run I'm expecting error but let's see what errors you get okay we got the error and the error is very simple it says the table alien not found of course right H2 will give you database not the table table is something you have to create by yourself now how will I create the table and that's where we have to add a schema in the resource folder so I have to add two things actually one is schema for the structure and what if you want some default values maybe you know every time you create a table you want certain values there to test it for that you have to create a file called data tosql and we'll do that in this so let's make it work so what we are missing here is we are missing this table and we have I told solution we have to create a schema file because hed2 when it loads we don't have a table there right now to create that file it's very simple you can right click on resources and say new and create a file called schema.sql and say enter now this is where you will write your script now the script to create your table so the script for creating a table or the SQL query is create table and we'll give the table name as alien and then you have to mention the properties or the the the columns so the column which I want here is the first one is ID now the ID will be of type uh int and we'll say this is a primary key uh then you'll be having a name name will be of type Vare and I will say the Vare is 20 in fact uh you know maybe we have done this before so normally in 20 characters you will name will be there but then if you talk about South indan names I'm a South so I have a very big name no na is not my full name my full name is tumar B I know that's huge so maybe I will need 50 characters and then here I will say marks and again marks will be of type in and here we'll be having Tech and Tech will be of again of Ware and I will say Ware 20 because Technologies can be shorten up if you even if you say artificial intelligence it is AI so let's keep it there so basically this is the file for the schema now we also need a file for the data again not compulsory but then I want to have some data so I will have some insert statement here so I will say insert into alen and I want to insert three columns ID name and Tech and what are the values I'm going to have here so I will say value is one1 then name in single code let's say kid and let's say technology is blockchain that's one and maybe I want two more so I will just copy and paste it here so this is one2 and this is hsh and let technology is AI 103 and this is social let's say iot okay so basically we got this uh three rows already created and now if you run this code so we have created two files schema and data SQL I will just go back here to my main application and run this this and let's see what happens now so when you run this let's see in the taking some time again okay so you can see there's no problem there's no error and we got uh empty here so so the reason it got empty is because if you see the repo we are still ring the empty data so we are not ring data from the table okay so the data is created how do I know that data is created what I will do is uh you know this update returns you the number of rows affected so you can see we got int so what I will do here is I will say rows and I want to see how many rows are affected and then I will print the number of rows affected so I will say rows plus space rows affected okay so it will print how many rows got affected now if I run this code of course we'll still the empty at list but at least I want to see is it getting stored in the database that's important so it says one row or rows affected here that's good so that means it is we are able to insert but now I want to fetch it how do I fetch it now to fetch the data again we can use our jdbc template because if you see the template dot there's one more method here which is called query now query returns you the data from table right so I will say query but what is my query so I will say string SQL basically I want to fetch all the records I will say select star from Alien so what it will do is it will return all the rows and then okay let's see what happens so if I say SQL it will return me the rows right but in which format now basically the query you can see we have multiple methods of query to fetch the data so we can use string which is the SQL and then you can use result set extractor or you can say row call back Handler or you can use we can use Row mapper for our example row mapper makes sense now what rowmapper will do is I I will show you one more time so we have to basically pass the object of row mapper so what happens is when you get all the data in your result set you want to fetch one row at a time right so row mapper will help you to give you one row at a time to do that we have to create the object of row mapper so let's create one so I will say row mapper and row mapper will be working with a type alien let's say this is mapper equal to now how do we create object of row mapper so we can say new row mapper but the problem is if you go to row mapper it is a interface this is a function interface which has only one method called map row and which takes two parameters one is a result set now this result set will be given by your query and then it also gives you a row number so in result set you might be having multiple rows right and you want to work with one row at a time so this row number will take care of it okay so basically I have to create object of row mapper which has a method called map row and then I have two things here which is this set and row number now basically this is the interface right so we have to create a Anonymous in a class to do that or we can also create a separate class which implements row mapper then create the object of it again that's your choice but here I'm using Anonymous in a class and slowly we'll convert that into Lambda as well so that it will be shter code but let's say we have this object here which is mapper and this is our Anonymous in our class so basically uh we have to do something here so what we have to do is this result set will have all the records right we have to take one record time so what map row does is it takes the entire result set and it executes map R multiple times depend upon how many rows you have so if you have four rows the map row will execute four times and it will give you one object at a time so basically we'll take that we'll create object of alien here I will say alien a is equal to new alien and then this alien object is empty right as of now and I've written the same object so you can see row mapper needs object of alien so I'm getting the object here and then I'm returning it but then this is an empty object right so it will not have values the values are there in result set so I can simply say a do set ID and now get the value from result set so you can say get int so how do you get data from result set you have to say get int and pass the column number so either you can say column number or you can say column name both work so here you can also mention ID a do set name this will be coming from the second row so get string I will say two here and then a do set Tech RS do get string three as I mentioned before you can also use a number or you can use a name of the column and that's it once you got the data from result set you are giving it to the alien object and then you are returning it so basically R mapper will take care of the data one by one now the same object which is of mapper you have to pass it here so you're not just passing a query you are also specifying what to do with the result set so if you click on query it returns you the object of list of aliens so the type is T alien so we have to accept the list of alien I will say aliens is equal to this so basically this query will return the list of aliens and that's what you are going to return not the new at least so what we did is we fired the query we got the data it's just that we have to map it we have to convert that data from result set into the list of aliens and that's what we have done with the help of mapper and that's it I hope this will work let's see let me remove some extra spaces there so with this if I run this code and again I hope it will work let's run okay can you see that it worked so basically alien 101 name kidan technology blockchain 102 H AI 103 suil iot and then the new entry which we have added 111 naven and Java this is what we have added right so basically these things are working out that's how you work with the spring boot with your jdbc and yeah that's it in fact you can do some more changes here if you want uh since this is a function interface we can use Lambda expression here so we can remove from here to here cut we can also remove one cly brackets here and you have you just have to put the arrow now let's remove the SQL exception let's put the arrow and then in Lambda we don't even need the type so we can remove both the types here and you know one more thing the row number is not not getting used right so we can even use R there or any other variable if you want let's say a again it will not make it readable right because a is already there let's say row number or we can say okay so this is the shorter code now you can also replace this mapper with this code again this is optional if you want to do it you can try to use Lambda not compulsory so we don't need to create the the reference here we can directly use this right so that's how we can do it in fact you can also remove this part you can directly write this particular statement in return even that works so if I run this code once again so you can see it works so yeah that's your spring Boot jdbc and if you observed everything is happening with the help of jdbc template so you don't have to basically do all this configuration by yourself writing the connection statement creating the statement everything will be taken care by the template you just have to specify the query and fire the [Music] query welcome back everyone in this module we'll talk about spring MVC now when you create a spring MVC project there are two ways of doing it so in the earlier days we were having only one way which was spring MVC configuration but now we also got spring boot so the idea of spring boot is just to make thing easy okay so in Spring MVC as well we can achieve all the thing but just that we have to do a lot of configuration and to solve that we have spring boot okay but what is spring MVC to understand spring MVC we have to understand what is MVC and to understand MVC we have to understand why we have MVC so let's go back to the old days when you talk about normal servit and JSP all let's talk about servlet so whenever a client sends the request to the server it is server's responsibility to process the request and give it back to the client but as a client what a client will get so of course client need two things the first thing is client needs data of course right if you go to any website the first thing you need is data but you want that data in a well formatted way example when you go to Facebook on Facebook you see data plus you also see a proper layout of course every one will get the same layout so if I go to Facebook and you go to Facebook we'll be seeing the same screen the same colors right what will change is data so your friend list different from my friend list your feeds will be different from my feeds what will remain common is design right so we thought as a s instead of creating everything because subet on the server side will create the layout and will create data yes you can do that but you know it becomes clumsy we feel that you know you should be have a separate task or we we should be having separate things for separate task example we need someone who will accept the request we need someone who will give the design okay uh it's like a normal template so design will remain common what will changes data and to hold that data we want something so basically we want something which will accept the request we want something which will be viewed to the client and we want something where it will hold the data so we got a controller who will accept the request who will send the response we want a view technology and then we want a model so model basically will hold the data and that's where we got this concept of MVC model view controller so if you have model view controller it will almost work same way as normal seret without using MVC but then the advantage would be we are separating the concerns okay we are separating the task so the controller will only accept the request in the earlier days we used to use salet for controllers and JSP for View and normal poos or normal Java classes for model right it's it's that simple but we got some issues here the issues were you know whenever a client sends a request request goes to a controller right and we are assuming that you only have one One controller but what if you have multiple controllers oh that's an issue so let's say in application we have user controller we have accounts controller we have laptop controller we have so many controllers right as a website as a web service you will provide so many services how do we create a multiple controllers very simple create multiple servlets one seret can handle only one request or one subet will be known as a single action controller okay so one server can handle only one request so we wanted something where you can have one class or One controller can handle multiple request that's one second we wanted something in front so we wanted something as a front controller what I'm saying is when you send a request instead of sending that request to the actual controller first you will send the request to the front controller now based on what request you are sending it will check and then it will send it to the actual controller then now you will think okay we have to create a controller plus we have to also create front controller don't worry the the moment you start using framework like spring where you have spring MVC in Spring MVC you don't have to create front controller spring MV will give it to you of course the normal controllers you have to build it uh so example normal controller is your user controller accounts controller laptop controller and this front controller will be your dispatch servet and dispatch servet will be given by Spring framework so now what will happen is the moment you send a request request goes to dispar outlet now let's say you want all the users you want to fetch all the users of course this can be done using user controller but who will call user controller and that's why disp says hey don't worry I'm there so disp Ser will send your request to the user controller but disp is something which will given by Spring framework right so how would disire knows that you want to call user controller and that's where you have to create a file where you will configure all this things oh now you have to also create the file it's not that difficult it's damn easy to work with but then again based on what you're using if you're using normal spring MVC then you have to do some sort of configuration if you're using spring boot this configuration goes down we will see that but we have to configure it we have to configure your disp salet to call a particular controller and that's how spring MVC works so yes you can also use normal MVC for Serv JSP but if you're working on Spring using spring MVC will help you in your [Music] project so let's see the Practical implementation of spring MVC now of course when you talk about spring MVC as well we have two options here we can go with a normal spring MVC application or we can go for spring boot MVC now of course there's advantage of using spring boot here you have to do less configuration so the way we did with spring core we'll start with spring boot now so we'll try to implement spring MVC with the help of spring Boot and then once you got the G of it we can go with the normal configuration yes normal configuration needs more setup but but then we can do that later so let me create a very simple spring boot application but this time I want to go for spring MVC so let's right click here and say new and of course right when you say spring boot application I want to say spring starter project now this is where you have to mention group ID here we'll say delisco and have the artifact ID as spring MVC Boot and of course the package name I will say teliscope do spring MVC see boot click on next okay now what to select from here now since we are building a web application you have to say web now when you search for web you can see we have an option of web here you can select this one you only need this one if you are going for reactive web you can just select this one which supports which uses web flux otherwise if you want to create a web service you can go for Jersey but as of now let's go with web and click on next we don't need anything else now we are not working with database or something let's click on next and and that's it click on finish the moment you click on finish it will give you a project so once you got your spring boot project and you can see we got our project okay still getting downloaded you can see it takes some time to get it from the internet so let's wait for it okay so you can see we got our project here which is demo I guess I forgot to mention the name for the project we have assigned the artifact now to verify that we can go to pom.xml and here you can see we have the artifact ID as spring MVC boot okay that's great we forg for to change the name from demo to Spring boot MVC but that's fine you can see we got one dependency which is very important here which is spring boot starter web the moment you add this dependency in your project this is your spring MVC now yes with the help of spring boot now first of all why do we create a web application of course there there will be a client and client will send you a request now based on the request you will process the request you will uh generate some data and you will send it back that's what we do right okay but in this scenario since we have not created anything which will accept the request normally we call them as controllers uh so we have a concept of MVC where we have model view controller now it is a responsibility of the controller to accept the request from the client and then it will do some operation it will give the data back to the user normally we save that data in the model object and then we have a view which is actually your the layout which you want to send to the client now now that view can be JSP it can be template as well at the start at least we'll work only on JSP and then we can see later okay so we got this demo here now what I want to do is I just want to create a controller which will accept the request let's say I want to call a homepage and I want to return a JSP now first of all we don't have a JSP you can see that I have not created any JSP here so let's open this SRC and here in this main I want one more folder so normally if you want to keep your JC Pages you have to keep that in a web app folder so right click here we'll send new I want to create a folder now and we'll name this folder as web app okay now once you got your folder we want to create a JSP page let me do that quickly so I will right click here I will send new I want to create this uh I want to create a JSP a JSP file click on next okay now let's name this I will simply call it as index.jsp that what makes much more sense right and you can see in the web app we got our JSP p in this JSP I will say welcome to telescope that's it I just want to say welcome to telescope and so whenever I call a homepage I want to just display this page but will this work okay let's we'll try that later now there's one thing you have to observe the first thing is uh we got JSP that's awesome that we have to put that in web app normally when you create a web application you always add the Tomcat server in your Eclipse right so in fact this is STS but you can imagine that is it is your eclipse and you can see in the server tab you can see we don't have any server here oh that means we have to also do the setup okay we'll see that later but that means if you want to this application we need a tomet otherwise it will give you error what if I say you can simply run this application and it will not even ask you for the Tomcat let's try so when you right click on this application and when you say run as the only thing is you have to run it as a spring boot app not on server because if you say on on server it will ask you for the external server we don't have it let's click on Spring boot app the moment you do that you can see it is doing something it will take some time so you can see if I jump to console it says spring boot app started it is doing some processing now okay now the amazing thing is it says Tomcat started on port number 8080 that's weird because we don't have any tomat server here right from where you're getting it the amazing thing is if you expand this me dependencies here spring boot application has a embedded Tomcat so you can see we have AED Tomcat here the advantage is the moment you move your application to some other server some other machine you don't have to configure your own tomcat and that's the the benefit here so in the application itself you have your Tomcat server so whenever you carry your application you are also getting doget server that's great right okay so that's something amazing but how do we access this now of course it says you have to use the port number which is 8080 so what I will do is I will open my browser so that's my browser I will simply search for local host colon 8080 and the you say enter you can see we got something okay we got this error that's fine at least we are not getting that weird full page 404 error we got this 404 but then this is getting returned by the spring MVC framework it says there's no error page because we have not built it yet uh it shows the timing and there's no message that's it at least something is working we have not got the output it is not calling index.jsp but why it is not calling index.jsp of course right when you send a request the request goes to the controller that's how spring MVC work so the first request goes to dispatcher serlet and from dispat server it will call a particular controller we don't have a controller yet how do we do that that we'll see in the next [Music] video so let's solve that issue the issue was we don't have a controller so let's create one now of course right when you say a controller we are working on Spring Frameworks it should be a Java file let's do that so let's go back here and here I want to create a controller oh how do we do that let's right click here and say new I want to create Java class let's select that and this will be of course you can name anything right from ABCD to some logical name but here I will say this is my home controller so logically when you say you have an application your application will have multiple Services let's say you have a Accounting Service uh let's say you have a user service you have uh let's say some some other service so when say you have a user service and you have certain request which a server will handle what makes most sense is to have a controller named as user controller if you're working for accounting so it should be accounting controller now since we are working with homepage let's say home controller and that's it nothing to specify much here and click on finish and that's your controller okay now you will say hey this will not work how can you just simply create a controller and you're getting the I mean the normal class is a controller kind of you don't have to extend or Implement any interface here the only thing you have to add to this controller is a annotation which is ADD controller this is what makes it as a controller now as we discussed your request will first go to dispatch a serlet and despite serlet will send it to a particular controller okay now yes in theory we have dispite serlet needs a configuration file as of now you can see we have not created any configuration file and that's why spring boot says hey uh you don't have to worry about the default configuration I will take care of it yes if you want to provide some extra configuration that's where you have to use some configuration file as of now it will work so we got a controller now this controller whenever you call this controller you have to perform certain operation oh when you say operation that means we need a method let's create one so I will say public and this method name will be let's say home of course uh you can have a different method name but that's fine let's give it home I want to call this method home which will simply print as of now I'm not calling any JSP let me just check if this meth is getting called when I call the homepage uh I will say homepage requested that's it nothing much I'm just printing homepage requested okay but will this work let's restart the application let's say restart the application it will take some time so let's keep our page ready and let's refresh and you can see after refreshing the only thing we changed is the timing okay we have not got the output yet but of course the output you be getting is in the console right because you're saying system.out.println and if you can see the console we have no way it says homepage requested that means is not working the reason it is not working is you know you might be having different kind of request you're requesting for homepage you might be requesting for some particular Page by saying slash accounts or slash users or maybe slash laptops here I want to specify Hey whenever I use a request for the homepage then only you have to call this method and the way you specify that is with the help of of annotation here which is request mapping so after say request mapping in bracket you have to mention the path first of all you have to import the package let's say control space package imported and here you have to say slash so that's our homepage right when you say the slash okay I guess it will work now let's relaunch the application okay let's refresh it and you can see oh we got a different type of error this time we got internal server ER we got 500 okay we'll talk about that later what is happening but let's see the console here are we getting homepage requested and the answer is yes can you see that we got the output it says homepage requested that's what we want it right yes we got some error we'll talk about that error in some time but it says homepage requested so yes we got a controller we got a JSP the only thing is this controller is not able to call the GSP page that's what we have to solve now now how do we solve it when you send a request for the home okay if you check your url it says lo host colon 8080 and then we are saying slash okay that's how you request the homepage so this we is getting called so we have to inform here whenever you call home method we have to call index.jsp now if you're coming from Ser background we used to uh create a request dispatcher using which you used to call the GSP here we don't have to do all those thing we simply have to return the name of the JSP which you want to call so you have to say index.jsp so what happens is see who is calling this home controller it is called by dispatcher servlet so if dispatcher Ser is calling this method so it is the job of dispat serlet to call the actual page so we just have to inform hey which page you want to call so we are saying hey dispat Ser I want to call index.jsp that's it the only thing is we have to say string here and our job is done let's relaunch it so as you can see it is relaunched let me just open my browser and say refresh oh okay something happened you can see okay I saw some animation there something happened uh we got the same error but if I expand this if you can see the downloads I just got a download here let me just relaunch it again and you can see we got another download what is happening the thing is okay so let me just go back again and let me just click on this I don't know what this file is it is says unknown DMS just open that in a finder so as you can see if I open this it is my JSP page oh that's weird I'm downloading the JSP page what is happening so let's try to solve this problem in the next video so the problem is we are not able to run the GSP we are downloading it let's solve this issue in the next [Music] video so what's wrong here everything is working except one thing which is instead of running JSP You are downloading it we don't want that we want to run that JSP on Tomcat so what happens you know when you talk about JSP JSP get converted into s and that sulet runs on Tomcat here nothing is happening we don't have something which will convert your JSP into seret now if you talk about the external Tomcat it has that power the Tomcat which we are using it normally don't convert JSP into seret okay in fact the spring boot is famous for rest Services you don't actually create websites in Spring boot that was not the main idea the main idea was to build rest Services if you want to achieve that if you want to use GSP yes we can use them Le that perfectly works but if you want to use JSP and if you want to convert JSP into salet we need a special Library which you have to download and that is Tomcat Jasper so let's do that so let's go to our browser and let's go to the favorite website of us which is MAV repository and here we have to search for Tomcat Jasper so this is what you need so I will just download this one okay now which version of Tomcat Jasper we need so let's go back to our application and here let's open open the m dependencies so you can see we have the Tomcat version it's 9.0.4 so let's stick to that so let's download the 9.0.4 uh let's copy this dependency I guess it is copied let's go back where you will add it of course you have to add that in a palm. XML file so open that and here let me add that particular dependency the moment you say paste and save it will download Tomcat Jasper from the internet it will take some time now since I don't have the local copy of it so you will get the repository from the official website so as you can see we got dependency here we got Tomcat Jasper now once you have added that let's see let's run this application once more and let's see the output so let's relaunch the application okay let's go to browser and refresh and now you can see we got the output so this time we are not downloading it but we are running the JSP so that's the thing if you want to run JSP you need a tomcat Jasper library to convert that so it is working we got welcome Tesco our job is done so whatever we have done till now is very simple we I have created a page which is index.jsp which is actually our web page and then we have this home controller where you are it's a controller basically and you can have a method which will accept the request for Slash and then it will simply call index.jsp but what if you want to do something extra let's say I want to have a page where user will enter some values so let's say I want to say I want to add two numbers yes we can do something big as well but let's stick to simple because we are working on we are learning spring framework right we are not just learning logic here so let's keep the logic simple and let's focus on the on the concept so in the next video we'll talk about how do we add two numbers using spring [Music] MVC so let's get the application so of course if you want to add two numbers after calling the web page it will instead of printing welcome to teliscope will also print the form where a user will enter two values so let's do that quickly so here I will say create a form for me and the form will have uh the the action and I will say the action is ADD so I want to call Simply add and then form ends and here I want to create two text field so I will say okay so that's our first field and we want one more so I will just copy this and paste and now you can see we got the second one as well so we simply say second number and here we'll have it as num two now what next I want to also have a button here right which will when you click on that it will call add uh let's do that so I will say okay that's done so we got our two put text for Num one num two and we also got buttons everything is done yes we don't have a perfect UI here but doesn't matter right we are learning spring uh so UI doesn't matter at this point yes it matters when you give it to your client as of now it should work now once you got that page of course when you click on this button submit it will call add but don't you think we don't have anything here which will accept the request for add now think about this what can be done of course you need a page where you will show the output but before showing the output I want to process that information information so let's say if a user says 2 + 3 I want to give it five as answer where we do that so of course you need to create a controller so if you're coming from Ser background so what we do is we create controllers but in Spring MVC you don't have to create a seate class every time what you can do is you can just add another method yes you can create a new class named it as calculator controller in which you can have different methods but as of now let's keep it here so in the same controller which is home controller we can have one more method so we'll do that I will say public this will return a string as of now let's keep it string and here I will say the method name as ADD of course you can have any method name you you don't have to stick with ADD method name here and simply return the page you want to call of course right if you add these two numbers I want so on some page in fact instead of showing it on a page you can also return this as a data but we'll see that later how do we return a data as it is I want to call a page and we don't have a page as of now let's create one uh so we will just simply say copy and paste and I will name this page as result. JSP and in this JSP I don't want the form and stuff I just want to print uh the output okay now how do we print the output so as of now simply thinging result is I'm not printing the actual output because we don't know the actual output let's go back here and in home controller you will simply say result. JSP that's it nothing much we have created two pages one is index.jsp and result. JSP okay so this looks fun okay I'm not sure if this is fun let me just try oh we're still missing something how do you know that you want to call this add method for the add request of course it will not match the name you have to use a request mapping as The annotation So when you say request mapping in this you have to say add so whenever you call add request it will call add method is that simple let's run this so I will just restart the application once so let's go back to our code and let's refresh once again so when you ref now you can see not just we got welcome with teliscope but we also got two text field here let's say two and three three as of now we have not done any coding for that let's click on submit okay you can see it is actually calling result page okay not in the address bar because if you see the address bar we have still have uh num 1 equal to two and num 2 equal to 3 okay uh we are not specifying which way is getting called here but result. JSP is getting called because we got the output as result is okay that looks cool but uh we want to perform the operation right so here I want to add those two numbers now how will you do it now let's go back to our salet and GSP stuff uh if you have learned it before so in salet what we do is if you want to take the input from the user uh we use this very special interface which is known as sttp solet request so using that you can fetch values so what I will do I will just go to this ad and I will say hey I want to use those methods if remember it's not just one parameter we us to pass two parameters so in this service method we us to pass the request object and the response object we don't have anything here so we have to create them right so let's do that so I will say HTTP Sate request and you can say req and we got that object so we'll say we we got this object now you'll be thinking how spring will know that you want the instance of this interface here that's the magic of spring dependencies injection right if you remember we have talked about spring ioc or spring dependency injection where it will inject the object which you need now thankfully this object is available with Tom cat so spring will say okay we have that object in Tom let's use it that's great now once you got that object we can fetch the values now so I will say int I where you will have the first variable value I know it's not a logical name to use but that's fine so we'll simply say request. get parameter you have to also convert this into integer right so you have to say integer. parcent Okay that's how you do it in fact for second variable as well we do the same thing let's copy this and paste and this time we'll name it as J of course and here I will say num two now once you got those two values you can simply add them right how do we add it uh it's very simple you can say int I will have the variable name as maybe num three I don't know if it makes sense I will say I + J so the value of I and J will be getting added to give you num three now I want to print this num three because this is my output in the result page oh how would I do that so we have to send this num three to the result page and to do that it's very simple actually you can simply say Hey how do you do that okay so in solet we used to use a concept of HTP session uh so when we use HTTP session we can simply add this num three in that object and in the JSP you can fetch it that's one way you can also use send disp where you can pass this value here you can say result. JSP question mark num3 equal to and whatever value of num three is you can say num three here that's URL rewriting right that's one thing you can do we have multiple options right as of now what I will do I will simply say uh I will add it in a session so I will say HTTP session session is equal to Let's create the object of it oh but how do we do that so we okay first of all we have to say we have to say control space let's import the package and here I will say request dot get session so we have this method which is get session which will give you object of session okay once you have that you can simply say session dot you can add not you can okay what's the method name I forgot the name you have to say set attribute and the attribute name we can have num three name can be anything right you can change it the only thing is whatever name you use here you have to use same name in the GSP page oh okay so will say num three in this case Okay once you got this attribute num three we are call result. JSP in result. JSP I just have to say uh we can use Java code here we can use session object and we can fetch the value or we can use jstl so as for the jstl syntax we simply have have to say dollar and the cly packets and here you will say num three that's it so whatever name you have in the salet or in the Java code need to have it here okay it makes sense uh but but sometime sometime your gstl may not work so maybe your code is ignoring it so we will simply say is e ignored false or sometime it ignores e language which is expression language okay uh let's keep it just to be on safe side I'm saying false everything looks cool I think it will work let's restart the application so let's go back to the page uh in fact I want to call the first page let's refresh in fact you can see the the address is Local Host colon 880 and then here I will say num one as three and num two as four or five uh so when you click on submit you want output as eight and we got it can you see that we got the output as eight so what is happening behind the scene uh so when you send a request the request is getting called for the homepage right so when you say homepage this method is getting executed which is calling index.jsp and that's why we got that page which we have where we have welcome to telescope and then we have a form when you click on submit it is calling add request is again going to dispatch a sulet where dispat subet sees hey for the ad we do have a mapping so this meth is getting called so not just method is getting called you also getting the object of request that's a job of spring dependency injection now once you got the object using that object you can fetch the values of I and J where we got three and five then we are adding them we are putting that data in a session okay so once you have dat a session we can fetch it any time in fact provided that session is running okay this is working but I still have some issues don't you think we are not using saet we are working with spring and this code looks bit ugly trust me it looks ugly can we clean it can we make it more effective and the answer is yes and that we'll see in the next video [Music] okay so let's make this code look good the first issue is we don't want this HTTP request object here don't you think spring should automatically give you this values in the parameter itself okay what I'm talking about is if I remove this part I want this two two values of int I and J here in I comma in J so we don't need to write these two lines okay so we are reled in the code right okay I'm just trying it out I'm not sure will it work okay the second thing is we don't need object of HTP session here so yes we can actually pass it here so we can say HTTP session session so we don't have to say request.get session so our job is done right because if spring can give you HTTP request object it will also give you session object yes it it it does that okay but will this work of course it will not work because you are not sending I and J you are sending num one and num to but how will you assign it here so we have to use an annotation here which is at request param and here in this request you have to mention whenever you send a value for n one assign the value for Num one to I is that simple okay let me import the package for request butam and I guess done and the same thing has to be done for J so let me just copy this code and paste it here okay and this we'll name it as num two so we got num one and num two so value of num one is going to I the value of num two is going to J so that's how you fetch the value okay this is actually coming from the address bar we'll see that and then let's try this let's restart the application refresh the URL and you can see we got this page here I will say three and four now you can see this data is going to the server so let's me click on submit in the address bar you can see the values of three and four are going in the URL so it is switching from there and that's how that request P works that's great this is working yes you can also use uh post request year and it will still work so that's the benefit of using request param so we are reducing the number of lines we are going for we don't want request object we don't need to assign we don't even have to pass it that's awesome right it it is working but still but still we don't want this HTTP session object here can we remove that we'll see that in the next [Music] video let's try to solve that let's remove that HTTP session here the way you can do that is with the help of a concept known as model and view okay how do we use that now if you talk about model model will have data right we have discussed that in MVC so in MVC model is a data so your data here is this d three so we have to just we just have to put this num three in the model object and we also have to specify the view right and the view name is. JSP okay how do we do that so we have a inbuilt class model and view right we'll say MV we can have any name doesn't matter and we'll say new model and view now once you got this object here instead of using session we'll add this in a model view so I will say MV dot there's the option of add object you just have to say add object Which object you want to add so I want to add num3 as num3 itself the way you do that with session right so we got motor object the amazing thing is you don't even have to return this string format you can return MV here because MV will not just have the data but also the view name oh we have not assigned that let's do that so I will say MV do set view name and the view name is result. JSP remember it is view name because most of the time people make mistakes here they say they they write set view it's actually set view name okay uh let me just remove the extra lines from here yeah so you can see we have added the value we have assigned that to a model object it's just that the return type here would be model and view that's it but then we don't need this thing here so we don't need model session object there our job is done okay let's try let's relaunch the application so let's go back to the page and here let's refresh it it should work there's nothing which we have done extra click on submit and you can see we got eight so the power of model view is it will take two things the data which is your num three so we don't need HTTP session object and it will also have the view name so you don't have to return string format so this works and we are happy whatever we have done till now okay but we have some issues what if what if you want to have this page in different folder what if you don't want to mention JSP in the code because you might want to change your view technology in future now you're using JSP in future you want to use something else can we make it Dynamic let's see that in the next video so till this point everything is working right we are able to add two numbers as well so if I say four and five I click on submit you got nine the only thing here is if you look at this code we are mentioning the extension as well right that's something we want to avoid because maybe in future I might be going for some other view technology maybe velocity or free marker in that case I don't want to mention jsb here that's one thing the second thing is if you look at the pages here all these pages are there in the web app folder now web app folder is actually a public folder which means anyone can access this page throughout the world you can they can simply go to web page and can then they can mention the website name slash the page name it will not even call the controller it will directly call the page and it will work we don't want that right we want this pages to be secure that's one thing or maybe you want to keep it somewhere so that no one will know the path so if you want to make it secured one of the folder we can use here is web INF so you can keep these files in web INF they are private the only way to call them is through the controller and that's how you can make it private but what if you want to make it public but in a different folder that's what we do right we put all our pages in different folders so let's say I will say new and I will create a folder here and the folder name is view or views and I want to put this file inside this view so if I drag it here that's done so we got that into views now and second thing I don't want to maintain the mention the extension here so let me remove that and remove this this one as well so you can see the the extension is removed and the path is also changed and after doing that if you restart the application and if you go back to browser let's let's see if that works now if I click on submit you can see we got an error it says 44 so maybe it is not able to find something but what is that something if I go back here so that something is the page right so of course it is not able to find this result. JSP now in this case you have to mention to your framework hey uh we are using spring framework and then spring framework is using disp servet so you have to inform this spet Hey whenever you are looking for the view you have to find that in this folder which is views and then you have to also add the exchange of. JSP of course it should be configurable to configure this we have a very special file here which is application. properties this is where you will do the configuration but how because when you say property files we have to mention the key and a value so of course values we know it will be the part of the JSP which is views and the extension which is JSP but we don't the the property of it right and so normally when you work on the project you do remember those things but if what if you are doing for this for the first time in that case you can simply go to this website which is the first one if you search for spring boot application properties go to this website and this is where you will find all the properties if you can just go down you can see we have this properties for logging we have aop we have identity then admin banners you can also change the banner right using this what we want is spring MVC so if you can just scroll down so we got spring M you can see that and then here if you want to do that so if you want to assign the path and the extension we have to use this two things the prefix and suffix now prefix is what before result which is the path and suffix is what after result okay so which result I'm talking about so if you go back to the code uh you can see here we are mentioning result right so before that we have to mention views after that we have to mention extension and to achieve that we have to put those two properties here so we can go back there we can copy these two properties and we can paste it here of course we have to change this so what should be the prefix so prefix would be views of course right that makes sense uh so when you search for the file now it will be views slash and the file name we have to also mention the extension so you have to say dot JSP that's it once you have done that I hope this time this will work let's restart the application and let's go back to the browser and here if I refresh this and if I click on submit oh it's working can you see that so that's how you can achieve you achieve this thing which is removing the extension which is JSP from the code from the Java code and putting that into in the property files the second thing is we have to also change the views in fact if you want to make it private you can also say views belongs to web in folder and that will make it private so that's how you achieve prefix and suffix see you in the next [Music] video so till this point everything is working right so we have added these two numbers in fact the numbers which I'm talking about is this so if I say if I pass let's say three and four and if I click on submit you can see we got the output as result is seven so that's what we wanted to achieve that what we are doing is we have created a controller so whenever you request for homepage it will return the index.jsp and then when you request for so in fact in the index.jsp if you go there what we are doing is when you click on submit it is sending a request for add oh so where we are handling it so you can see in the controller itself we have ADD method which accept the request for ADD and everything is working you can see it says request Pam we got num one num two and things are working okay but there's one thing which we have done in the last video which is the introduction of model and view so the reason we went for this is uh when you want to call a view of course right you want to send some data to a client and client will expect a HTML page so what we are doing is we have Creed the JSP which will get converted into HTML after rendering but then when we say you want to call a JSP JSP also need a data and data in this case is this n three so basically we have to set up two things the first one is the view name and the second one is the data which you want to pass so data in this case is the num three okay so for that we are using this model and view because it can hold two things model the data and the view name which is uh result in this case result. JSP so what I want to achieve now is do we have some alternate option example let's say this is best model view is best we can set the name in fact there's one more thing about model view you can uh actually set the view name here itself so you don't have to uh use another method so you can simply pass the view name in The Constructor and that works okay so the point is can we use something else apart from model and view example let's say I don't want to return model view object here I I want to stick to the string part here okay uh that means in the return we have to still mention the view name so I will say result what I have to change is I don't want to use model view so then the question arise how will you add the object that's what we want to do right we want to add this data in the model in that case if you simply want a model you can create an object of model and it will still work but where so the idea is I want want to put this data in the model object so model is something which will be available for the view as well so in the parameter in the bracket itself you can say you can specify the reference of model M okay now what this model is so model is basically an interface which will behave same as model view except that we don't have a view here so you can say we are saying model and here instead of saying MV dot we will be saying m dot but you can see we got an error the thing is in M we don't have add object method we have to use another method which is quite similar you can see the method name is ADD attribute so instead of saying add object we have to say add attribute and then we can pass the two stuff the same thing as model view uh it's just that we are not specifying The View separately now which one to use it depend upon you you can still go with model or you can go with model view it's just that when you send a request and if you want to have that data in a model doesn't matter if it would be used for view or not or you want to call a specific View later you can still use model but if you want to specify model view both I would say preferably you can go with model and view okay you still have a choice that's great that's how we can use model here now other than this model we also have an option of model map so you can say model map here even that works you can say control space and enter you can see the this the code will still work so let me just run this code and let we also see the difference between these two so let me first run the code with model map uh let me relaunch the application and it is still launching so let's refresh and you can see we got the same page there's no error that's great this time we'll add some different numbers we'll say 6 and 8 when you click on submit you got the answer so you can see we have not done any changes and at the output and the only change we have done is in the controller so instead of using model and view we are using model map then what's the difference between model and model map so if you click on this you can see this is documentation for model map model map as name suggest supports a feature of map so by default when you add the attribute will get added in the map format so you are adding data in a map which is getting added in the model object so it depend upon you which one to use if you want to use Simple object I would say go for model if you want to add map if you want to have the data in a map format of course the best option would be model and map I me model map okay let's go back here so this still works with model and model map both at least this code so try it out uh so it will it will of course work so that's it from this video where we have talked about model and model [Music] map now once we have talked about model and model map it's time to look at another annotation which is model attribute now before doing that I want to make some changes the first change is instead of adding two numbers let's do something else now in Java everything is object right so if you want to represent data we represent data in the object format example if you want to represent a laptop if you want to represent a mobile phone or maybe Account Details or user details everything will become a class and then we instantiate them right that's what we do in Java so here as well I want to create an object of alien okay now what is alien basically so alien is a programmer so I refer programmers as alien so what I will do here is I will go back here and I will say new I want to create a model class so let's say class and we'll name this class as a alien and the only thing is I just want to make this into a different package okay so here I will say the package is model it's better to have all your model classes in model package now this alien will have certain variables right so let's say I want to go only for two variables here so I'll say private in a ID and private string a name of course in future we'll add some more attributes once we start talking about Advance concept as of now these two are enough and now I want to have gets and sets for this I will say Source generate gets and sets select all and generate so once we got the class once we got the variables and geta sets done let's go back to index.jsp now what I want is as a user or as a user you will add your own records uh so you'll be having the option of adding the aliens so of course the I want to have the action as ADD itself but then the data which you will ask is different so we'll say enter enter your ID so we're asking for the ID and of course this name will change I will say this is Aid and then here I will say enter your name so we're asking for two things a ID and name let's have it a name so you can see we got this two details so now when you click on submit it will send the add request in fact then just to make it more interactive I will say add alien it makes much more sense right so now when you click on submit it will call add alien but in the home controller you can see we don't don't have anything as ADD alien so we'll modify this and we'll make it alien in fact you know what I will do instead of changing the existing stuff let me add another method here and say okay let me just say this is so I will say public string and the method name would be add alien of course you can go with any method name and now to call this I have to map it a request mapping right so I will say request mapping and here I will say add alien now we have to create this method now of course we have to accept some parameters right so the parameter which we are passing from the index.jsp is these two details we have a ID and a name so whatever we have learn till now is uh if you want to accept the values we have to use this two annotation here in fact two variables with two annotations we have to say request param so we can accept that with the help of request param the only thing is here it will say a ID and the variable name is also a ID and here I will say instead of num two we'll say a name and here as well I will say a name okay there's one change a name is not of type int it's of type string so they have to take care of that as well so that's done now this method will return something right I want to call a page I want to call the same result page I will say result uh because I'm lazy to create one more page so let's stick with that of course you can create your own Pages later but as of now I want to call result but this result page need to have something right I want to print something here so what I will do is I will not print num three I will print the object of a I want to print alien here but will this work of course if you go back to home controller we are calling result that will work but then it will not print alien because we don't have that object in the model as you can see we don't have any model in this method so the first step is you need a model object so I will simply give a comma here and I will say model M so that at least I will be having the model object with me and in that model object I want to pass the object of alien the amazing thing is we don't have the object yet so let's create one and the way you create object you you can say alien a equal to new alien and then here you will say let import the package and here you will say a DOT we have to assign the values as well right whatever is coming from the client so as a client you will be sending data right so we will say submit by passing let's say a ID is 101 and name as naen so this two details will be received here I mean here in this two variables we have to assign that to the object so we say a do set a name I will say set a name as a name in fact once you got the object we have to add this object in the model object so I will say m do add attribute and here you will simply add the attribute as a but if you add the attribute as a it will not work because in the result. JSP we are asking for the alien so the name we have to refer here is alien so I would say alien so whatever data is there in uh in a will be represented as alien in the result. jsb that's what we want now will this work see till this point we have not done anything extra it's just that we got a class in fact whatever we have done till now we know about this stuff let's relaunch the application just to verify if everything is working so application has been launched let's go back to the page let's refresh so yes of course we need to see some changes instead of having uh num one num two we'll be getting ID and name so I will say the ID is 101 and name is naen I guess I missed something in the code let's see that what I missed when you click on submit you can see we are getting the the default uh package name class name and the hash code we don't want this we want to print a data right to achieve that we do one thing right we always create we override the two string method if remember in normal code Java we do that so I will simply run generate a two string method here which will print data this is what I missed let me just save it and let's relaunch the application again so when you click on submit here you can see we got the output so this is what we wanted right so whatever user has entered will be coming here here now you will say hey everything is working then what's new here the thing is if you look at this code which is request param and request bam something is not matching right we are accepting individual values and then we are assigning them to the object here it doesn't look good right what I want is instead of having request but I'm here what if I can simply mention the alien object here itself this is what we wanted that means I don't have to mention all this thing so the moment you pass the object on the moment you pass the values you know in a ID and a name now since the names are same right it's in a ID and a name don't you think it should be assigned automatically to the alen object is it possible yes and the way you can do that is with the help of model attribute so once you have mod attribute with you you can directly assign the values here but how to do that that we'll see in the next video so let's make it work to make it work we have have to use another annotation which is model attribute so that means here instead of this all this request param this stuff we don't need this a ID and a name separately what I want is I want to say alien a right this is this is what we wanted right and then to achieve that we have to we don't need this stuff here we need to remove that the only thing is we want this object to be received from the client to achieve that we have to use an annotation here which is ADD model attribute that's it simply save this so whatever is coming from the client will be assigned to this a and our job is done let's verify let's relaunch the application so this time I'm sending one2 let's click on submit and you can see we got the data this is what we wanted right and the amazing thing is on the client side I mean in the code we are not using two request Pam annotation and then checking it one by one creating the object of alen nothing like that it's very simple right the amazing thing is model attribute is also responsible to add the data in the model that means we don't have to do this manually whatever object you got is by default there in the model and that's why it says model attribute right so we don't even need that model object it's quite simple okay but what if if I go back to result you can see here we are saying alien right and here we are saying a what I will do now is let's go back here and here I will say A1 will will this work so I'm not using alien I'm not using a because a is the name of the attribute and alien is the class name itself or you can say the decapitalized class name but here we have a different name we have A1 as a name in this case let's try what will happen so let's click on submit the moment you do that you can see we have not received the data there the reason is we have a different name here which is a and then here we are saying A1 okay how do we solve that so model attribute says okay if you want a different name that's fine in the bracket you can mention A1 so now whatever data is coming from this a will be assigned to A1 is that simple let's relaunch so when you click on submit here you can see we got the data so that's the beauty about model attribute so it's an annotation which also accept the parameters so if you pass the parameters it will take that name otherwise it will take the name of the class which is alien okay but what if I have alien here okay let's stick to alien and here in the home controller what if I what if I don't write model attribute will this work the answer is yes if you don't have a different name if you still want to go with alien you don't have to mention model attribute but it's a it's a convention that you should write it otherwise it will still work but then you know there will be a confusion because here we are saying alien uh so it is working what if you change it what if you say A1 it will not work so let's relaunch it without using mod attribute it will still work so when you click on submit you can say it is still working so without even without M attribute it works because we are using the same name in as alien here here okay now this is mod attribute which we have used in the as a parameter to the method can we use mod attribute at the method level we'll we'll see that in the next video so let's say here in the alien I want one more data so let me create one more class and I want some data about it or maybe not even class let's say I want to print the message let's say welcome message I will say welcome back aliens so I want to print this message the one way you can do that is by printing as it is or maybe I want to change this alien to something else every time I maybe maybe I want to I want to do that in the code so what we can do is we can say welcome back and here we'll say name so by default you can see we don't have any name here right so name is just an attribute which we are using here that means before calling result I have to assign name where should I do that of course you will say you have to do that in this method because this is what is calling result but what if you have multiple methods which is calling result in this case you can see even this method is calling result so of course we should not be assigning the name here what you can do is you can create a separate method a separate method which was responsible to assign the value to the name so let's say I will say public void I will say model data of course you can have a name doesn't matter and here you will simply assign the data to the model okay so how do we do that so we have to use a model M now in this model you can say m dot and you can say m add attribute of course with the name uh so we'll say the name here is name and then the actual name which I want to pass is aliens of course you can pass anything here but let me just pass aliens now what will happen is in the code in the output I want to welcome back aliens right so the name will be replaced by aliens but will this work of course not if you want to make it work we have to use and annotation here which is model attribute the moment you say model attribute what will happen is your spring framework before calling any request mapping it will first call model attribute this model attribute will create a model object and then in that model object you are adding the attribute which is name with the name aliens now it doesn't matter whatever controller you call whatever method you call with the request mapping everywhere this name will be available so in the result page you will get that name right so we can use motor attribute in the method parameter or on the method level so let's relaunch it so let's click on submit here and you can see we got result is okay we have not received the result I don't know why what went wrong there but at least it is working you can see we got welcome back aliens so let's figure it out what went wrong there why we have not received the alien okay because we are saying alien here and the attribute here is A1 that's bad uh let's remove that I guess that was the issue let's save that okay but at least that this is getting right we are getting welcome back aliens at is that is working uh let's relaunch it okay so let's click on submit and so that that is working we got a result is this so we got this data and then we also got welcome back aliens so that's how we can use M attribute on method [Music] level so whatever we have done till now is awesome right uh we have created this project and it works like charm so we can send data to the server and server will give you the response the amazing thing is instead of using normal Ser JSP we are using a spring MVC framework and that to we are not using normal spring MVC we are using spring boot here the reason we are using spring boot is when you work on Spring MVC project or any type of spring projects we need to do a lot of configuration and spring boot says hey you don't have to worry about the configuration so if you look at the project we have not done much of the configuration we were focusing only on the logic yes we did some configuration in the property file so if I open that file for you so you can see this is where we are mentioned that we want to work with this folder views and then the extension for the uh page is JSP other than that everything is quite simple right we are doing everything in the Cod but then before spring board we should still work on Spring MVC I mean a normal Maven project where you have to do a lot of configuration until this point we have not seen that so what if you are working on a project of spring MVC but without spring boot so in this video Let's create a project with the same features I want to have alien I want to have homepage so with the same feature but with the help of normal spring MVC configuration so yes we will be using mavin here we have to add dependencies we have to do some configuration so what config I'm talking about now first of all when you say spring boot project so this project knows that this is spring boot MVC so whenever you send a request request directly goes to dispatch s but when you create a normal mavin project or normal web project we have to navigate we have to mention hey if you want send your request first to dispat a salet so we have to configure that second thing you have to configure is we have to mention that this is a spring MVC project where all the configuration I mean all the controller have annotations okay that's we have to mention second we have to do this configuration as well but then what type of file we'll be using to do this configuration so in general when you talk about configuration one thing come to mind is XML so we'll be using XML to configure this okay it looks lot of work right and technically yes we have to do a lot of work but it's it's worth learning so what I will do is I will go back to my application I mean go back to the project and instead of using the same project I'll be creating my own I mean I'll be getting a new project here so let me right click here and we send new and of course in the new project I will be just copy pasting the existing code to save our time so here I will create a maven project okay so not a spring project or something let's create a m project and in this MAV project so click on next I want to create a web application select that click on next so it is asking for the group ID here so I will say com. teliscope artifact ID would be spring MVC okay nothing much specific here click on finish now once you click on finish it will create a project a simple MAV web project where you have to add dependencies okay so we have to add dependencies first right so we'll do that or maybe I will simply first copy paste I want to understand what errors you get and there's one more issue here the issue is yes it is giving you index. JSP but unfortunately it is not giving you a internal Tomcat server so you have to add the Tomcat server externally okay that is something we are remaining with so what I will do is I will simply close all this thing code now and I want to add the external server so the way you do that is by clicking on this no server available I want a aat server and in this I will select 9.0 click on next okay so it is saying do you have this setup need to to check so browse and let me search for the location of Tomcat so yes I have the Tomcat 9 available in my download I will select that and click on open I don't remember when I've downloaded it but in case if you don't have the opposite tomat you can just go to Google and search for it it's not that big deal and then click on next just shift your project here so that you don't have to add the Tomcat in your project manually so click on finish and that's done so you have added the external Tomcat to your project and soon this error will be gone I don't know why it's not going there uh let me just check once I will go to properties uh code build path okay there are some Library issues uh it says what is missing something is missing here the jav folder is missing okay we do that and I just want to make one more change instead of having it to 1.5 I will change it to 1.8 click on finish okay that's done and I need to create so you can see it is giving you error because you don't have the Java folder okay even the to is not set up here so I will just go here and say add external server there there should be a server option add Library I want to add a server R time Tom Cat yes finish click on apply and eror Sol can you see that everything gone okay so once we have solve that issue I want to copy all this controller in fact I will just copy paste the packages here of course I want a different name we can change just change it change them later so I'll just copy that and paste in my main folder or Java folder okay so you can see we got projects here and then the change I don't want the same package name I want to have a different package name here so I will say refactor rename I want the package name to be spring MVC not boot click on okay and the same thing here okay done so once we have done those two Chang just it is still giving you error okay can you see that we got error here now first of all when you talk about spring MVC we don't need this main file because this is used only for the spring boot let's remove that and we are getting eror for controller as well now what is the error here the thing is whenever you work with spring framework you have to add spring dependencies and you can see we don't have it here oh from where you will get it you guess it right we have to add dependency in the pal. XML file so here we have to add spring M dependency so save uh from where you will get it just go to the maven repository mvn repository and here you have to search for spring MVC and you can see we got spring web MVC just select that okay which version I will go with this table one which is 5.1.4 uh click on that and copy this just go back to application and paste now once you say paste it will take some time to download okay it was quick that's great uh because maybe I've used the same version before so you can see we got all the dependencies here the moment you got dependencies I will go back to home controller and can you see that gone all the errors gone okay that's working so what do you think if I run this application oh how can I run this application because we don't have the uh pages so I want the pages as well so these two pages I will just go back here and index in fact I want them in views so let me just copy the entire folder and let me paste inside the web app folder okay so when I say web app inside this we already have index.jsp we don't need it so I will just right click and say delete I don't want that folder or the file so in the view we have two pages index.jsp and result. JSP okay this looks cool what else what else we can do here I guess this is the configuration which I wanted let me just run this code so what do you think if I run this code will it work so the answer is no actually because the moment you run this application it will search for the index.jsp oh before that I have do one more thing I want to shut down the internal server which we were using for spring boot otherwise it will give you the conflict for 880 port number so I will go back here index.jsp let's run this code or in fact I can run the entire project right so I will say run as has run on server because this time we are using external server click on next and click on finish yes it takes some time I guess it will use the internal browser and can see that we got error it is not able to find the index. JC page how do we solve it yes we have to do a lot of configuration but that we'll see in the next video so it's time to solve the issue so the issue is we are not getting the page right because the moment you run this application and when you say you are calling for the homepage it will look for the index.js page in the web app folder as you can see we don't have it there that's one issue the second issue is I want the the request should be sent to the controller not directly to the page to achieve that we have to configure our XML file so now normally when you create a web application you get this amazing file here which is web.xml and in this if you go to Source this is where you have to configure that Hey whenever I sent a request the request should go to dispatcher salet if you remember in Spring MVC all the request should go to dispatcher solet first okay how do we do that so it's quite simple you just have to go here in web app we don't need that and we need to create a subet tag that's how you provide the subet mapping right so we'll say subet tag and we also need a subet mapping okay once we got these two things in subet tag we have to specify two things as we do in all the subate projects the first one is a subate name uh let time me let me just have with the name asisco and we need one more thing which is your servit class okay now what is the class we'll talk about that in some time but before that I want to map with this Serv mapping here okay so first of all when you say Serv mapping again we need two things here we need a servit name in this case you have to make sure that this name is same as this name because that is how you link this to uh outer tags here and then you have to also map with the URL now why do we need URL so to mention for what type of request you you want to call this particular class which you have not mentioned till now so I want to call dispar seret for all the request so for all you can use simply use SL that okay so but then we have to mention the class name right so what is a class name so as I mentioned whenever you walk with spring MVC request should go to dispatcher salet okay so that's a class so we'll simply mention that here so we'll say dispatcher svet or it's not coming directly here dis declaration oh it's giving you entire stuff we don't need that okay that's awesome that's awesome we don't need to actually create all this thing it is giving you everything but time me let me just type it here and then we can get it from the uh control space I simply tape it here so it belongs to Spring framework o. springf framework. web. saet do dispatcher suet so dispat belongs to this particular packet that's how you map it in fact the other way is you can simply if you don't want to type all this thing by yourself you can simply say disp saet you can see it says you can auto complete click on enter it will give you almost the same thing which we have done here you see that that you can handle uh but I I would like to go with the same text which I have written let's let's stick to that now we are saying request should go to disp salet but then it is disp salet who will send the request to the controller here right so how would disp saet knows that it has to send the request to the controller and that's where we have to configure the dispay Serv it as well because if it don't do that it will give you an error okay what error it will give you let's see let me shut it down and let's relaunch the application r on server yes click on next finish okay so you can see there's no error as of now but then the moment you try to call the homepage we got a different error this time and the error is file not F exception and the file it is trying to find is telescope hyphen S letter XML or that's weird why only this file name and what this file will do okay so first of all this file is responsible to configure your dispat saet okay that's one second why the name is The lco Hyphen seret so basically the name should be something hyphen servet now what is that something the name of your subate name so the since it is Tesco here and that's why in the error itself it is saying telescope that's the reason we we call telescope hyphen subet so if you give a different name here if you say a it will be a hyphen subet XML okay so we have to get that file but where in the web in so we'll right click here we'll say new okay so we need to create a XML file so set XML here click on next and okay what should the name as we discussed the name should be teliscope hyph sulet do XML click on finish and you can see we got this file here and here we have to add a bean tag instead of typing that let me just copy paste it this is where you will specify the the DTD for the beans we'll be creating and we'll say beans okay okay that's done we have assigned the beans tag and now we have to we have to do some configuration the first thing is we have to mention to the dispay saet where are my controllers uh second we have to mention hey we are using annotation for the controllers and third is the path so let's go step by step the first one is we have to specify the context we have to specify the package in fact the base package so for that I'll be using component scan it is CDX colon component scan and you can see it is asking for the base package so you just have to mention in which package you have all the controllers okay so in general I can specify com. telescope you should be very specific here but that's fine this should work the next thing you have to mention is you have to mention that we are using annotation configuration so you have to say CDX annotation config that's it okay will this work let's try so after doing all this configuration let's try what let's see what happens yes restart the server okay just calling that page oh this time we got a different error okay okay uh maybe we are running very wrong file here let me just run the application yeah so you can see we got a different ER this time uh it says something not found okay 44 now what is not found it says there's no mapping for get index oh there's no mapping for the index it's because we are calling the controller you can see is calling the controller there's no mapping for the index it is there okay it is there the only thing is it is not able to find the extension and the path because if you move this index.jsp to web app and if you write JSP here it will work but I want to specify the path as well and to do that we have to create a bean and then this will Bean will have a class so the class who is responsible to do that is internal resource view resolver now this belongs to a particular package which you have to mention so you have to mention the entire path here that's bad okay this is the entire path we have to mention and then we have to close this that's your bean tag now in this Bean tag now this is this here which is internal resource view resolver is responsible to search for The View and to give the extension and to specify the path and to achieve that we have to specify two properties the first one is prefix as we are talking about prefix the prefix I want to have is we say value so there are in the view right so will say slash views slash we have to specify the path the only thing you have to mention now is the extension and to do that you guessed it right we have to use suffix so we will'll just paste it here and this will be suffix okay the value will change so the value here will be JSP that's it so whatever you have done in the config spring boot the same thing is done here we might have to add one more property here for the gstl now since in our code we are using gstl let's make it supportive so we will say property and we'll say view class so the view class I want to go for is gstl View and to achieve that we'll say org. springframework so this is the path so is org. springf framework. web. s.v. jstl view okay I'm tired now I guess it is done let's let's try I hope this should work now let's relaunch the application okay something worked let's go back to the page here and say Hey I want to call the homepage and oh oh oh now we got another eror it says class not found justs is not there that means we have to also add the jstl now okay so let's try this let's go back to let's go back here and let me just remove the property for time inin relaunch go back here okay it's working without that let's try uh so go back here and say for is my ID and my name is naen click on submit oh it is working can see that we got Aid 4 and name is naven so we got welcome back alien as well so everything is working and this time we are using normal MVC we are not using spring boot so let me just go for a quick recap what we have done till now so basically we have this home controller but as we are working with spring MVC the first request in fact we are working with web project the first request will go to the web.xml this is where we are saying whenever you send a request request will go to disp salet and to configure that we have this file which is telescope hyphone salet so this is the configuration for disp seret where you are mentioning two things the component scan annotation and the most important is the view resolver in which this part is not required if you want to write that you have to also add dependency for gstl let's not do that here and you can see we are specifying prefix as views and surfix as. JSP and everything is working it's do simple so that's it from this [Music] video now once we have seen how to create a simple spring MVC application let's get back to Spring boot because that's where we want to learn some more advanced concept now this is a project which we were talking about before right the demo project which is spring boot application basically so you can see it also says it's a boot application so what I want here is I want to have some more features so before that let me just run this code once again just to verify what we have here so I will say run as spring boot app okay so let's run this and you can see uh it is asking you for two things the name and the ID so I will say name is I mean ID is one one uh let's say rows and if you click on submit you can see it's working we got one1 we got rows It also says welcome back aliens okay what I want now is we are only sending data and that if you see the data has been sent using a get method so normally when you send data from HTML page to the server it goes in you you can call it with the help of get method or a post method so in the get method you actually pass the data in the URL okay that's something we don't want because whenever you send data to the server you should always use post whenever you fetch data from a server you should use get so here in this scenario if you look at the pages which we built the index.jsp here it is asking you I mean it is sending the data to the server so in this case instead of using get we should be using post and the way you can do that is by mentioning method is equal to post so now you will be sending data in a post format so let me relaunch the application so let get back to the browser and here you will say refresh okay where is that refresh and now I'll be sending let's say 101 and row and if you click on submit now you can see in the address bar we don't have it we don't have those values so is get is going there with the help of post request but what is happening on the server side so if I open my home controller this method which is ADD alien accepts both the request the get and the post both okay now the the amazing thing is request mapping does both right but then what if you want to support only one you can do that you can mention that so what I will do is I will say instead of using request mapping in fact there's one more way you can have one more attribute here so you can say value is equal to the value which you have and then you can give a comma here by specifying the method and you can see the method is so you can mention method equal to request method. getet now when you say Get it will only support get now if you send a PO post request this will not work and if you want to use post just mention do post it will only work with post now so that's how you can restrict so you can say post or get you can be very specific in this case this is one way the another way of doing that is instead of using method stuff here we can actually change this request mapping to we have one more anation here which is known as get mapping now get mapping is same as request mapping with method get so it will only work with get now if you want to use post we can also use post mapping so basically we have three things we can use request mapping which supports both we can use get mapping which only supports get and we can use post mapping which supports post so that's how we can be very specific in this case so that's how we use get and a post in the scenario in fact we'll also use get in the next [Music] video so till this point we are only using post mapping right we were sending data but what if you want to receive data so what I'm talking about is let's say if I go back to the synex JSP and this is where we have this button which will send data but I also want a button which will receive data okay or you know instead of having a button let's go to URL and say uh I want to fetch a data so let's example if I go to Safari again and here instead of uh having the colon 880 I will say I want a data about an alien so I will say get alien now when when you say get alien or maybe I can say get aliens which will give you all the aliens so I will say get aliens in this case so this get aliens oh just reacting to me to Google you don't want that but that's fine uh so I want to return the alien names the list of aliens of course I can use static uh list here uh so let's do that so I will go back to my home controller and let's create a method which will accept get aliens uh in fact I don't need this ad anymore with just to reduce the number of lines we have in the code so I will go back here and I will say public string and the method name would be get aliens again as I mentioned you can use any name doesn't matter and we don't want to return string do we want to return string yeah let's return a string so let's return the list of aliens in a string format and there's one more thing if I go back to alien we want to do one more change here the change is inside this alien I want a Constructor because I don't want to create object and assign the value using sets gets uh let's use Constructor here so I will say Source generate Constructor using field both the fields okay so once we got the Constructor let's go back to the code now here I want to create certain aliens before that I just want to return some blank string so that you will can you can just avoid the error and I want to call this whenever someone request with a get request and the URL which I want it to work here is get aliens so here let's create a list of aliens so I would say list of aliens I will say this is aliens equal to okay let's import the package for list I will say control space the list and here let me create arrays do as list because I don't want to say uh new at a list this is shortcut way of getting Multiple List here and you will say new new alien and let's say 101 comma naven that's the first alien we got then we give a comma and again we'll say alien we can have one more alien here which is one2 and I will say Rose so you can see we got two aliens we got a list of two aliens and let's return this list here so I will say return aliens dot to string because I'm sending that data in a string format so we have to convert that into string format again I'm not even converting this into Json okay my bad I know I'm just converting that into a string okay so uh let if this works let me just relaunch the application okay it says restarted let me just go back to the thing here okay now I want to request for get aliens so SLG aliens enter and oh we got an error it says oh there's a problem here can you see that if you look at the error properly it says it is not able to find so is there's a 4 not4 error it says not able to find View and we got the answer you can see that we got a list in the text format and it is searching for that page. JSP you know why because by default in Spring MVC whatever string you WR return it gets returned in the format of name whatever you return it will be trying to search for the name of it we don't want that we want to call a page so we did that mistake we should not be rning a list here we should be ring the page which will display this uh so let me just return a page I will say show aliens we don't have this page as of now we need to create one so let's quickly create a page of JSP so I will say new and I want to create a JSP file with a name show a aliens. JSP and here I want to display that result so I will say dollar aliens I want to print aliens here that's it or maybe I will just simply print result because I don't care what we are sending I just want to print whatever we are sending from the client side I want to have one more thing I just want to say is El ignore just so that in case if my tom cat says Hey I will not be supporting elel let's do that so let's say is e ignored false okay everything looks good now I hope this time oh it will still give you eror because we have not added that in a model okay let's do that so I will say model M and here I will say model. add attribute as I mentioned you can also use model map here we discussed that before right and I will say this is result and this is aliens that's it let's relaunch the application so let's go back here and say enter can you see that we got the output so we got the data from the server and this time we are sending a get request so you can see this is your post request and this is your get request so if you want to work with get we say get mapping okay but let me just add one more twist what if you want to fetch a particular alien not just all the aliens you want only one alien let's say one1 or one2 how can we do that and that to I want this data to be coming from database oh we are getting so much of restrictions here right okay so first of all let's try to get one data and we'll see the database part later so that we see in the next video how do we fetch one particular data spring project has lot of modules right we have talked about spring core which talks about dependency injection we have spring MVC which talks about how can we Implement MVC pattern with the help of front controller in Spring and then we have also work with spring rest that we'll see in in the future videos but what about the database what if you want to connect your spring application with database now even it doesn't matter as it spring we normally if you want to connect with Java a normal Java application with database we have to use jdbc right now we all know the pain of jdbc because in Java world we work with objects right so we have to convert our objects into data which will be stored in a table format to solve this problem we got o RM which is object relational mapping so imagine a class here and a table here so we can map it right because the class name will be your table name uh we have some fields in your class you'll be having of variables so you can map those variabl with the table column right but what about the data what about the rows there now for one class we can have multiple objects in the same way one table can have multiple rows and each object here will represent one row there think about this let's say we have alien object and alien will have three variables a ID a name and let's say age so we can have all this in a table columns so if you create the first object for alien with three different values that will be one row there if you create second object with three values that will be second row there and that's how we can do the mapping and that's where omm is very famous but how do we Implement omm and that's where we have jpa which is Java persistent API now the actual implementation for jpa maybe you can use hypernet there so that means we have to build the application we have to work with uh hypernet and lot of configuration can we use all those things in Spring and the answer is yes because spring provides you dependency injection so the instance for hbnet can be created in Spring framework and that can be done with the help of spring omm so basically you are using a spring application you are using hibernate which connects with myql which connects with database so we have a huge chain there so we have spring we have hibernate myql and database in between you will also have spring omm which will connect this two right now the advantage would be so the instance creation will be done by spring omm for hypernet the configuration will be done in the spring configuration next what about Transaction what about begin transaction commit transaction will be handled by Spring transaction uh so how exactly that works we will see in the Practical [Music] video so let's try to implement the database connection here so what you're trying to do is when you run this application and that we are doing it in normal spring MVC we are not doing it in Spring boot now so yes when you talk about normal spring MVC on normal spring we have to do a lot of configuration so just be with me because in this video we are going to do a lot of configuration so let's get started the first thing is when you say you want to connect with database we have two option you can use normal jdbc or we can work with hibernate now since we work on big applications it's better to go with hynet because it can handle your transactions in a good way you can achieve omm here so let's do that so what I will do is the first thing we need is we have to go to the pom.xml file and we need to add certain dependencies so basically we have to add approximately four more dependencies here in fact five I guess so let's let's get started the first one we have to add is now see when you work with spring MVC and if you want to work with hybernate you need hybernate code dependency that's very important now when you say you need hypernet code where you will go so you have to go to the m repository so you have to go to this website which is MAV repository. comom and here you have to search for hibernate code so in search for hibernate code you can see the first first link is hbnet code you can select that and you can pick up any version I preferably go with the lest one because it has a good number of usage uh let's copy that and go back to your application and paste that's the first dependency we have added we have to add some more because when you say you want to connect spring and hibernate now hibernate being a omm framework you need to add a dependency for to connect spring and hybernate here which in this case is spring orm so or will be sitting between hibernate and spring so let's open that I'll go with 5.1.4 because that's the spring version we are working on copy this and go back and paste when this is done now what next now when you say you are working on Spring and hybernate whenever you work with hypernet we have to handle transactions so you have to begin transaction you have to commit transaction but what if you want spring to handle it and that's where we can use spring transaction so we just have to add one more uh okay we have to to add few more but let's go step by step so if we have to add spring transaction because this will handle the transactions for you so let's paste it we have added hibernate because we want to use hibernate here we added spring omm because we want to connect spring and hibernate we want spring to handle the transactions and that's why we are saying spring transaction okay now we need two more okay last two okay last two uh the first thing is uh when you say you have a database of course you need to install some database right now in this machine we have already installed MySQL again we'll see this setup how to install MySQL and how to configure it so we'll see how what is MySQL and how to open myql workbench and where from where you can download it but then in this machine I've already configured it so the thing we need here is the mql connectors so the the setup we are using is mql so we have to use mql connector and for that we have to download M connector so in case if you're using some other dbms let's say po orle uh make sure you use that particular connector so I'm downloading myql connector let's copy that I will go with this version which looks stable copy and go back here and paste okay when this is done now all this dependency takes time to get downloaded from the MAV repository but since in this machine I've already done that before so it is getting from the local repository now when you work with hnet when you work with databases we need to create some connection pool maybe 10 connections at a time five condition at a time and we have to configure them uh so to configure this type of pooling and to use pooling in the in the project we need to use certain external jar files or maybe a third party pulling service so to use that to to use third party service here we will be using C3 p 0 again you can choose whichever service you need this is my favorite C3P0 because it works uh let's use this one I will prefer the latest one copy and go back and paste so you can see we have added all the dependen IES and job is done just to give you a summary we have added these dependencies the hibernate code spring orm spring transaction myc connector and C3P0 okay once we have added all the dependencies what next now we have to configure hypernet because just by saying we are using hypernet it will not automatically connect with database right we have to configure it and to do that where where do we configur this and we have done that before right so if you want to do any configuration we have this very favorite file which is telescope hyphone salet this is the dispatch salet configuration file you can add the configuration here itself now first of all we are using spring transaction right so in the in this beans we have to also add TX you can see we have MVC we have context but nowh we have TX so let's add that now this TX we can simply copy this line and paste it here MVC will become TX and here as well we have to say TX that's done we need to add the thing here as well so we have to we will be copying this one and let's do that here itself in fact we can add it anywhere we want but let's do it here uh so instead of MVC again we have to say TX instead of MVC we have to say TX I could have easily copied from somewhere and pasted it here but I wanted to show you how can you do it by yourself without copying so this is done next we have to add certain configuration now when you say certain there are a lot of configurations so what I will do is instead of typing it typing everything now it's time to copy paste so you can see I have copy pasted a lot of stuff don't worry I will explain each and every line here now the first thing you need is we are saying let's go bottom up so the first thing we need is we want to handle transactions right so we want someone to take care of transaction in this case we are saying hey Spring you will take care of the transaction spring will say okay I will do it but I need some configuration so you're saying okay if you want to handle transaction I will create an object of my transaction manager in fact uh this is a object of hybernate transaction manager so how do we create the object of this so it's very simple you just have to create a bean uh so we are creating object of hibernate transaction manager and then we are saying the ID name is my transaction manager so this one is dependent on this Bean but if you want to create this Bean here we need to create object of session Factory now when you learn hibernate we have a concept of session Factory right so we work with multiple sessions and all these sessions are created by session Factory that means we have to create object of session Factory here so let's set the property of session Factory but unfortunately we don't have the object yet so we are creating the object of session Factory here so you can see it says be session Factory now this session Factory is the object of local session Factory Bean the amazing thing is as a programmer we are not instantiating them I mean we are not creating object of them it will be created by Spring framework thanks to dependency injection so you can see we got the object of this one but it needs certain properties the first property is data source now this data source will have all the configuration of database what is a database I mean which dbms you're working with uh what is a username what is a password how many size pool you need all those stuff will be done in this my data source that's one the next thing you have to mention is in which package you have all the entities because normally work with hnet you have to specify entities every table need one entity next you have to set some hibernate properties one of them is M dict So based on which dbms you're working with you have to set up this one so if you're using Oracle uh you have to use Oracle dialect if you're using pogis you have to use pogis dialect we are using MySQL so let's say MySQL dial and we also want to see the SQL query in the console so once you run the code I want to the query in the console and for that we have to set this particular property great but then we have still not created the object for my data source so it is still missing and you can see that's we have done here so we are saying we need my data source this is the object of combo pool data source now from where you getting this remember we have added this C3P0 in this we have to do certain configuration the first one is we have to set up the driver name jdbc URL the way you work with normal jdbc and the username and password I know I should not be revealing password on the internet but that's fine uh this is the local machine password so that's fine that's my MySQL password in this machine okay so this is the configuration which you have to do I know it's clumsy but that's how you work on Spring right now this is not something you'll be doing every day right so this is something you'll be doing only once the moment you work on Project every day you will add some new features to the project but this thing will remain same yes you will be doing some minor changes in future but as of now this works okay but then what else we have here so will this work of course not this will not work let's go back to home controller and we need to do certain configuration so if you remember we were supposed to accept the request for get alien and then get aliens we need to add that but how will you do that that we'll see in the next [Music] video now when you say you want to work with database of course you need Mel for that right of course depend upon which dbms you work with so in this course we are working on M skill so how do you install M SK it's very simple actually just search for MySQL download this will take you to the official website of MySQL now based on which OS you working on maybe Windows Linux or Mac it will give you those options here okay so you can see we are into downloads and here we have option of Community Edition so basically if you are using it for free so make sure that you use Community version in the community version you can install two things in fact we need two things here the first thing you need is a community server and the second one you need is the mascle workbench now mascle workbench will behave like uh what do you say the GUI for your for your application so I will say download on this one and download on this one as well so two downloads this is the community server now based on which OS you're working on maybe Mac OS or Linux you can select that now which one to choose of course based on which OS you use you can select that click on download now this will download the masol server so you can see it is downloading the second setup we need is Mas work bench and this will be your what you say the GUI so now click on download so you can see both this will take some MBS so it will take some time okay so you can see we got both the setup the first thing you have to set up is the mql server when you click on this one now depending upon which OS you use we will have different steps but it's quite simple you just have to say next next next nothing fancy uh so you can click on continue you can click on continue continue agree continue install the same steps and you have to enter the password here for your machine if you're using Mac okay setup done now once you have done the setup it will also prompt you for the configuration now since in my machine already have the configuration it will it is not prompting me so in the configuration the only thing it is important here is the password so make sure that you give a proper password now once you have done with the setup you have to install mul workbench so you can see uh if you're using Mac it will give you this step but again if you're using Windows it's quite simple right so you can see I installed Mas let me open that now so I'm opening my Mas workbench this is the first screen you will see and then you have to click on this one if this it is available otherwise you have to click on plus connection here where you have to mention in the host name as Local Host and whatever port number you have configured during the configuration by default is 3306 and then you have to enter the password click on okay and test the connection if it is successful that's great you can go ahead click on okay I have to give a proper name I will say this is try okay so you can just click on this and you will see your masal workbench now here we have to to create a database right so the query you have to type here to create a database it's very simple you have to say create database teliscope the amazing thing is I already have the database here so I will not be executing this the next thing you need is the table so I will say create table alien okay before this we have to also say use theis because that's how you specify that you want to use database so we'll say create alien this will have two Fields one is a ID of type integer and second one is a name which is of type vcat 2 or vcat 20 which is the size and then we have to also insert data so we'll say insert into alien values uh you will insert let's say insert two values one 1 comma naen and the second one you can simply give a comma here you can say one2 comma and you can say this is let's say row so we got this two data here and then you can also hit this select query so you can select Stu from Alien the thing is I already have this thing in my database if you want to execute this click on this symbol I just want to say select star from Alien because I know it is there so I will first say use and execute you can see we have one1 nine and one2 row in this database now once you have your Mas already let's fetch this data in your [Music] code once we have added the dependencies which is uh all the dependencies here and then we have Al on the configuration and then uh on the other side we also have a back end ready with us which is your mask your database in fact mask your dbms in which we have created a table which is aliens so how do we connect it now first of all if you want to connect with a database you need a layer so in fact it's not compulsory but then that's a general thing we use whenever you want to connect with database we normally create a dow layer so the way you can do that is very simple you just right click on your project in fact right click on your package here and say new I want to create a new class and this will be a alien da now for every particular table so let's say if you have multiple entities in your database let's say you have alien table you have accounts table you have user table what you can do is you can create a separate layer separate Dow for each of the tables there in fact you'll be having different entities as well so the moment you talk about hynet we have to put entities as well so that's your alen Dow here which is a dow layer in fact Dow stands for data access object so this is a class where you will have all the methods using which you can and access the database normally I'm talking about the C operation create read update and delete okay I want this to be in a package name as da again not compulsory you can use any package name there in fact the moment you start using spring boot uh in Spring boot we don't refer them as Dow we refer them as repositories your choice you can still use da or you can name it as repository let's continue with da and click on finish and you can see we got a dow class here now what we want here now we want certain methods right the first method we need is in fact what I'm trying to fetch I'm trying to fetch all the aliens you can see here we are trying to in the home controller we have this method which is get alien in case if it is not there in your code make sure that you copy this from the code which we have done in the earlier sessions you know the spring boot videos so you can copy that the only thing is in you in the attribute we have not assigned anything because we don't have the values so what I want is I want to fetch values I want to fetch the aliens with the help of get alien method so here as well inside the Dow let me create a method as get aliens so I will say public which will return me the least of alien and then this will be method name as get aliens of course you can have any method name but uh this makes much more sense let me UT the package for both so list and alien done and here we have to return the list of aliens now first of all where from where you will get it now since we are working with hypernet in hynet we have this amazing feature called as session Factor fact because to fetch data we have to use sessions and to use sessions we have to use session Factory so let's use it here so I will say private session Factory and here will be session Factory okay that's done now once you got session Factory here we have to import the package for that package imported now once you got session Factory here let's use session Factory to fetch the data so before using session Factory we have to get session object equal to now how do you create a session now first of all you don't have to create this session you just have to use the existing session created for you so in the container we have the session available just use it and the way you can do that is by saying session Factory the object of session Factory dot this is a method which is get current session so it will not create the session for you it will use the existing session okay once you got the session object I can simply fetch the values but how do you face the values in fact this session object here which is if I search if I type session here and the moment I say dot you can see it has so many methods one of the method we have is get now you if you want to fetch one particular element you can use get but what if you want to fetch all the elements and that's where we will be creating a query so I will say create query in this create query you have to type the query I will say from Alien this is jpql or you can say hql and you can specify the alien. class the type of data you're trying to fetch so what this will do is this will create a query which will return you which will talk to the database which is telescope and will fetch all the aliens now if you want to convert that data into list you can simply say Do List here so it will give you the list from the table and then since it is giving you the list we have to save the list somewhere so I will say list of alien let me call it as aliens equal to so whatever data we are receiving from here will be saved in list alien and once you got it once you got your alien you can simply return it that's what we want to do right we want to return and all the aliens okay that looks fun I mean I hope it will work but there's one more thing when youone you say you are creating object of s Factory we have to provide Auto wire here but you want be thinking how spring will create object of it automatically actually not if you see the configuration in the configuration we have created the be named as session Factory So when you say autowire you will get this particular object okay so nothing is happening automatically here uh things you are doing some stuff let's go back to alien da I guess Everything is Everything Will work actually no everything will not work the thing is okay in fact first of all let's run this code and let's see what errors you get now to run this code first of all you need object of uh Alien da here because you want to fetch all the aliens so I will say alien da and then I will name this object as da I don't want to say new alien da here because I want to use spring feature and to achieve that we have to say add component here if you want spring to create object of it and here we can use our favorite autowired so that it will give you the object existing object oh we already have it here I guess I've done that before so this is what you need you need object of alien da and once you got it you can just go back here and say da dot the method name is get alien it will give you all the aliens and your job is done hopefully let me just relaunch the application and let's see what happens in fact I should run this once okay something is happening it is trying to connect to database I guess that's why there's nothing wrong here now if I try to fetch if I say get aliens uh the method the request I will say enter oh we got an error it says could not obtain transaction synchronized session for current threat what went wrong here the thing is when you say you are trying to work with session you're trying to fetch data the thing is whenever you work with database we need to handle transactions okay it simply means uh before anything doing any stuff you need to begin the transaction and after you have done everything you have to commit the transaction so in hybernate we do that manually we say begin transaction we say commit but since we are using spring here spring orm you don't have to do that by yourself you can simply use annotation here so The annotation name is transactional so the moment you say transactional it will say hey I will take care of all the transaction beginning and commit that's great our job is done let's relaunch it let's see what other different dat we get this time and let's say enter so so as expected I'm expecting an error here but let's see and we got an error it says can you see that we got a different error this time it says alien is not mapped the thing is when you work with hibernate or om tool we have to make sure that your model classes are actually entities that's how you can connect your table with your class and the way you do that is by using add entity on top of it and here as well you have to say at ID which defines your primary key so you have to specify two things the table as entity the class as entity and the primary key as ID that's done I hope this will work this time I'm not expecting any eror but you never know let's go back here and say enter and we got what uh nothing is coming yeah so we got the output can you see that we got all the aliens now in database we only have two rows and that's why we got two data here but if you have more rows it will give you that the amazing thing is in the console you can actually see the select query so we have done that right on the properties we have mentioned that we want to see the SQL if you don't want to see that simply say false it will not show the SQL in in the console let's verify the data database do we have it there so you can see that these two records are there so that's how we can fetch all the aliens but what if you want to fetch one alien what if you want to add a new alien that we'll see in the upcoming [Music] videos now once we know how to fetch all the aliens we want to fetch one particular alien can we do that in fact we also want to add a new alien to the database so if you see database now we only have two records we have one1 Na and we have one2 Rose so in this video first we'll try to add the alien and then we'll see how to fetch one particular alien the amazing thing is we already have this add alien method here we just need to say hey Spring orm just add one particular alien for me now in fact if you see the homepage as well which we have it here when you say submit will call this particular method here and then which also accepts the alien object okay that simply means we need to save it somewhere in fact before that I just want to do one more thing instead of calling result page I want to call show aliens page because let's stick to it let's use this one JSP file here in this JSP we are simply printing result that means if you want to call it you also have to assign to a result in fact we'll be using the same stuff uh maybe will not need it that we just need to add the model object which we have it here but that will be with name a right I want to have the name as result so what I will do here is in the bracket in double Cotes I will say this is result okay that's done but then I also want to save that in database right so of course for that we have to call Dow dot save so you have to say Dow dot there should be a method named as ADD alien unfortunately we don't have any method which is ADD alien here so that means we have to go to our alien da and this is where we have to create a method named as ADD alien let's do that so let's go back here and say public are we expecting something here no so I will simply say void and I will say add alien this add alien will accept a particular alien so I will say alien now since we are accepting here we'll also have to send it and we have done that here let's go back to our code and now how do we set it so of course we have to repeat this statement so we have to fetch the session object let's copy and paste and then with the help of this session object we can save it now the amazing thing is this session has a method named as save you just need to pass the object your job is done uh in fact before saving we have to start the transaction and we have to ended the transaction and that can be handled with the help of add transactional annotation and job done right it's so simple we are simply adding one extra method with this feature and job is done let's go back to our control just to verify everything looks cool let's run this code let's see what happens so I guess server got restarted so what I will do is I will try to call the homepage refresh and that's our homepage here I will try to add one more alien which is 103 and this time the name would be let's say Kieran and if I click on submit okay so there's no error you can see we got the alien as 103 and name is Kieran now if you look at the console it says insert into alien that means we are able to fire the insert query just a final confirmation let's go back to database because we are still not sure right if you refresh this code uh you can see we got 103 Kon that means it is working so it is so simple to add the element in the record so we have done with all fetching all the values we're done with adding one value or one alien how can you fetch one particular value so let's do that so I will go back to home controller and here I want to have one mode using which you can ask for one particular alien now for that I just want to do one more thing in the index.jsp we forgot to add the line which was there earlier so if you remember we added this in the uh spring boot project the same thing we need here paste so it will ask you for one particular alien and when you click on submit it will call get alien okay so that means in the controller we need one mode which will be get alien so I just copy this code paste it here and this one will be get alien and this is also get alien now when you say get alien you have to also accept a particular ID right so what I will do here is I will say add request Pam in a ID now since they have the same name so we don't have to do it multiple times I mean we don't have to mention that name here okay once you got Aid here in Dow it's the method name should not be get aliens it should be get alien by passing one particular ID so we're not passing all we are passing only one ID unfortunately we don't have this method in the Dow yes of course we can go there we can create the method or you can use shortcut here you can click on this error and say hey create a method name as get alien so you can see we got a method name as get alien this method returns particular alien so I will say one particular alien but what should be the code here so the code is very simple of course you need a session object here as well so I will say copy and paste in fact we should be saying code reuse right okay so let's create a alien object I will say alien a is equal to now this alien will be coming from database I will say session dot the method name is get so we have two methods you can also use load here or you can use get so let me use get in this get we have to mention what type of object you're trying to fetch the alien object and the ID is whatever is coming from the user which is a ID in this case so you got an alien here and then here we have to say return a so we are returning the alien but again uh we have to transaction we have to commit transaction and for that we'll be using add transactional and I guess job is done let's verify once let's relaunch the application and here in the output window Let me refresh the page we got two forms here the first form is of submitting the alien second one is the fetching for one particular alien I will say one2 and click on submit and you can see we got rules so it's that easy to work with database you don't have to write complex queries you don't have to do jdbc setup everything is done right and that's the power of spring with hybernate and the combination of this two is spring omm so I hope you are enjoying this there's so much you can try it out you can fetch one particular alien you can add alien you can also use load okay so that's great so that's it from this [Music] video so till this point we have seen how to connect spring and hybernate with the help of spring omm and everything is awesome right but the only problem is the number of configuration which you have to do first of all we have to add dependency that's fine that actually makes sense to add all those dependency but still look at the number of dependencies which we have added we have added this all this dependency by ourself that's one issue the second issue which I have is the configuration look at the configuration and that to in XML yes you can convert this into Java configuration but still as a programmer we are focusing on configuration we don't want that we are programmers we want to focus on logic and that's where spring boot says hey you know you focus on convention which is logic I will focus on configuration so let's do the same thing whatever we have done here in Spring boot now of course we need to specify all these properties because this is not something spring boot can guess right spring boot don't know that we are using Mas uh spring boot don't know that what is your IP address and the database name spring boot don't know your username and password so in this case we have to configure all this thing but not in XML in Spring boot we have this awesome file which is property files right it actually makes sense to write everything there and it is bit easy so let's go there and let's do all this stuff you don't need to do that because spring boot will take care of it so let's close this and we don't need this anymore let's go back to Spring boot application which is demo in this case and you can see we have home controller from where you are you will be accepting request the only thing is till this point we were returning the static content we don't want that we want to return the dynamic content coming from database and then uh we have a model class here which is alien. Java now what is missing here is the repository right the Dow layer that's something we have to create so we have to get a dow layer what else we have to add dependencies as well but do we have to add all the dependencies technically we don't need all first of all we need MySQL because you are connecting the application with mySQL so let's do that first so let's go to Safari and now we want a msql connector so just go to MAV repository. comom and search for mq connector you can pick up any version you want of course we are doing this from a long time now in this series so now we know which one to copy so let's copy this version and paste it here so myql done now we need one more not hypernet not uh spring transaction not all those dependency but one which is spring data now spring data is something using which you can connect to database using spring application and it actually makes sense to use spring data when you work with spring boot again I will tell you why so what I will do now is let me add that dependency first so I will go to again Safari and here I will search for in fact you can open any browser uh let's search for spring boot data now when you search for spring boot data this is the dependency which you have to add spring boot data JPS starter so let's click that now which version select so I will pick up any version because anyway we'll be removing the version number it is mentioned globally uh so let's paste it here after spring web I don't want to mention the version number here because that's something we have assigned here so not have to mention in every spring dependency so you can see I have added two dependencies here the first one is data jpa and second one is MySQL connector now once you have done with that what we will do is let's go back to application you can see nowhere I have configured anything for database okay but still what do you think will this work we just added two dependency normally in normal project even if if you are dependency it is not compulsory for you to do the configuration if you want to use them then only you have to do the configuration but that's not the case with spring boot example if I run this application now so let me say run as let me just stop the other executions I will say run as a spring boot app so you can see we are getting an error it says fail to configure data source URL oh that's weird because nowhere in the application you can check the entire source code here no where I'm talking about the database connection we just added dependency and that's where problem starts because the moment you say spring data jpa in your project it is compulsory for you to add I mean to configure the database connection otherwise it will not work oh that's a compulsory let's do it then so let's go back to the application of properties and here we have to provide the configuration now what config you ask the first one you have to mention the URL in fact first of all we add the dialect so when you work with hnet see behind the scene hibern is running it right so or jpa hnet is running it so we have to specify the dialect here so let's say spring. jpa do properties why I'm typing all this thing when I can do the shortcut so you don't have to type it you simply have to say hnet do dial that's weird I'm not getting the entire stuff let me just type it here spring. jpa do properties. hybernate D now once you got the dialect you have to specify the dialect name which dialect you actually working with so I'm working with myql but that belongs to org. hybernate do dialect. mysql5 dialect okay so as your dialect changes this will change so if you're using post this becomes post dialect that's the confusion to do now the next three important the first one is URL now when you work with jdbc you know what we need we need URL we need username and we need password so let's assign that here here so I will say data source. URL the first configuration uh so that is your jdbc colon MySQL colon SL localhost colon 3306 slash database name is teliscope the next one we need is username and password so I will say username so that's for data source. username where is that it's here so username we want is root and data source. password and in this case is 1 2 3 4 5 6 7 8 that's my database password that's done we have provided the configuration we just want to solve this eror first so let's run this L let's see what happens the moment you relaunch the application now you can see we have not got any error so the only thing we have done is we have added dependencies and we have configured the application. properties Next Step would be we need to create a dow layer then we have to go to Home controller we have to use Dow layer uh we have to also make sure that your alien class has entity annotation and at ID but that we'll do in the next [Music] video now once we got our dependencies and then we have also configured our application. Properties by specifying this four properties here let's create our Dow layer now if you remember in Spring MVC as well we were supposed to create a alien Dow class but what we will do here is instead of creating a class me create an interface I'll right click here I will say let me get an interface and we'll name this interface as alien rappo now you might be thinking okay so we got the interface here we'll be writing some methods here you know the Cod operations add alien get alien maybe you can also try removing the alien and then we will create some class which will implement this particular interface that's what we are thinking about let's see that later but what I will do is I will simply extend this class or this interface with another interface named as jpa repository so this jpa repository will ask you for two things one is it will ask you for the class which is a in this case and the second one it will ask you for the type of primary key in this case we are using integer let me import the package so you can see we have created the repo here okay so the point is if you got the repo which is your da in fact so once you got the repo you have to create a class which will implement interface we'll do that in some time uh let me just go back to home controller and now I want to instantiate the repo and the way you do that is very simple you say alien repo uh let's name it as repo itself the way we have done for da and let's s it autowired now one thing which is very important if you want to instantiate this alien repo you need a class and we have not done that till now okay let's keep it for later now once we got the repo we can simply perform certain operations here right we can call those methods we'll do that but before that I want to go back to alien and in Alien we have to specify that this is an entity right and then we have to also mention the at ID annotation here so that's done let's go back to home controller now once you got the repo let's first try to convert this get aliens here so I don't want the static names so let me remove that so how will you get the aliens it's very simple you simply say repo dot now of course in the interface you can see we don't have or not this one if you see the interface here we don't have any method but see the magic the moment you say repo dot can you see that how many methods you got in fact we also have a method which is find all find all is a method which we have not declared and we are not even defining it okay so definition part is later but how it is coming we have not declared it now that's where this jpa repository is there so when you say your interface extends jpa repository jpa repository is a interface belongs to the spring data jpa Library which we have added and if you go there you can see it has so many methods all are declared you can see we have find all we have find all uh we have find all by ID we have list goes on you know we have uh get methods we have remove there are so many methods available here if you can't find methods in this particular interface even this interface extends another interface here some methods belong here there's no method which is available here some methods are available in code cude repository and you can see we have some methods here okay that's fine uh you can just check them out later so there are methods which are there in JP repository which you are getting in Alien repository you will say okay but all of them are declared right none of them is defined that means we have to Define them now is it a good thing or a bad thing to Define all the methods okay let let's see that so without defining the class what do you think will this work of course not right in Java we have learned if you have not defined stuff of course this will not work let's try let's relaunch the application and let's see what may go wrong I will say run as in fact I just have to restart the application so you can see we have not got any error maybe we will get an anomic eror let's go back to application refresh there's no error till now let me fetch all the aliens so I will just go back to the address bar and I will say get aliens I will say enter oh we got the error okay it's not something we have not got the error but then this is not the error because of that alien repository this is the error because it says no default Constructor for entity the thing is when you have this class alien which has a parameterized Constructor it also needs a default Constructor since you are making it entity we forgot that point so let's create create a Constructor here I'll just type it we don't want to write anything in that that's why I'm just specifying this let me restart the application once again go back to the safari and hit refresh and can you see that we got the output I know that's weird right because nowhere we have written the code for fetching the data we have not implemented the interface which is alien repo and that's the magic the library which we are working with here is is the data jpa now data jpa says say everyone works on C repository right everyone works on CR create read update and delete so when you have those operations what if I can do it for you so spring data jpa says Hey developer you don't have to worry about those operations I will do it so it has certain classes which implements JP repository and it will take care of all the operations the basic operations and that's why we specify JP repository which will work with alien so all the operations finding uh deleting saving everything you will get just because of this line which is JP repository and that's the beauty so yes there is a class which implements this repository but we are not defining it it is done by Spring framework and that's why I'm in love with spring boot so just let me just go for a quick recap what we have done we have created this layer which is alien repository you can imagine this is a dow which extends the jpa repository here now jpa repository is responsible to to give you all the basic operations in the application. properties we have specified four properties one of the URL username password and the dialect and that's it we have one more in Alien we have to make sure that this is entity this is at ID and we got a default Constructor now if you compare this code with the code which we have written here look at the Dow layer code look at the code how many lines are there in fact we just have three methods here what if you have save method what if you want to have remove method or save is there what if you want to have update so this code will go up in fact the idea is the more code you write the more bugs you adding in the software right and that's why spring board is so awesome you write less code and you get less errors so that's it from this video in which we have talked about spring data GPA so now let's implement the get alien which is fetching only one particular alien and how to add a new alien so we already have those methods here in the controller right we have get alien and then we have ADD alien now first of all we don't need this list anymore let's remove that and here instead of rning the static code which we have done let's return the data from the repository now we don't have to add any method in the repo because everything is coming from JP repository so what we'll do here is we'll say repo dot now which method we use to fetch that so we have a method which is get so the method name is get one in which you have to pass the ID which is a ID in this case and that's it your job is done it will give you that particular element let's also implement the same thing or maybe let's run this code first so let's relaunch the application and in the Safari let's go back to the homepage say refresh now into fetch let's say one3 let's click on submit and you can see we got K that's what we have in database right so this is working and just look at the code we are not doing much of the code but everything is given by Spring data jpa so let's Implement add as well now what do you think what should be done here it's very simple actually you just have to say session not you have to say repo dot the method name okay we have to search for it now what if you don't know the method name so technically it should be somewhere around save or add or something uh so if you see we don't have any method like with a so let's try save so it saves there's a method name as save we have we have to pass a so now we are trying to save the alien object uh let me repeat we are not doing any commit and roll back part here everything is done by Spring uh so let's relaunch the application in fact in normal spring MVC as well we were using add transactional so we were not doing those uh begin transaction commit transaction part so let's see in the Safari let's refresh and okay this time we adding it right so let's say 104 and this time we'll add let's say May now if you click on submit uh you can see we got 104 mayure it is displayed in the page but what about database so let's go back to our myql and let's refresh can you see that we got 104 and mayure I guess I have entered the wrong spading for mayure that's fine but we got the data right that's important so that's how we can add element and that's how we can fetch a particular element in fact you can also try with remove that will be assignment how can you fire the query to remove something now what if you want to fetch something based on some other parameters so what I will do is let me just go back to the index.jsp and here what if you want to fetch not based on the ID but let's say based on the name or maybe a table has a different parameter when you say a ID that's a primary key right so it is very easy to fetch something with the help of primary key and that's what your repo also suggests in the repo itself you specified the type of the ID you have which is integer but what if you want to fetch based on the name normally what happens is in a table you can have multiple aliens with the same name how can you fetch something with name because if you go back to home controller and if you let's try this here so if I say let's say I want to fetch something and we have this repo the moment you say get you can see it is asking you for the ID it is not asking you for the name can we do that can we fetch something with the help of name that will see in the next video so let's try to find a particular alien with the help of name now if you go back to your alien repository we know that this extends jpa repository and this jpa repository by extending some other interface or it has certain methods in it all the methods are related to the ID because that's how you find the element from the database right with the help of primary key but now we want to fet something not with the help of primary so what we are trying to do is if I search for a particular a ion I'm not passing the aid I'm passing a name so what I'm saying is let me just copy this code let me create another method and this method name would be get alien by name of course you can have any name doesn't matter but let me just go with this name so get alien by name and here as well the request would be get alien by name I know it doesn't make sense to have in the URL but that's fine uh we can use the same thing but you can have any URL it doesn't matter here okay so it's not that you have to follow the this rule at this point now here instead of having param as int I will be having string and this will be a name right so we are fetching with the help of a name now here with this repo when you say get one unfortunately we cannot pass a name here because a name is not a primary key so what to do so what if I say you can simply remove this part and you can say repo dot you can see we have a method which is find by ID so when you have have an ID you can find by ID but now you have to say hey I'm not finding by ID I'm finding by name just type it there so you can say find by a name that's it and just pass a name here but you can see we got an error of course right we don't have this method anywhere and this is your own variable even jpa doesn't know that you want a name there okay so what to do now now since this method is not available in jpa what if you create this method in your own repository let's create that let me just click on this and say create method find by name in the alien repo you can see we got the method and this will return you uh okay now the thing is it is possible that in a database you might be having multiple aliens not one so in this case let me just go with the list of aliens and here as well we need to return the list okay let's go back here and now will this work now of course right if you talk about all these methods which are available in JP repository if jpa says I will Implement all the methods that's fine because it is its own uh declaration they have their own definition but now this is our declaration right so that means now we have to get a class which implements alien repository to Define this particular method okay let's not do that let's run this code let's see what happens we maybe we'll get some errors let's run this code and let's go back to Safari so this time I'm fetching an alien not by by ID but by name or but we don't have a form there we also need a form so what I will do is I just copy this code and paste it here this time I will say enter your name and this will be get alien by name is it the same thing we have yeah get in by name let's go back here and anything mention yeah so we have to mention this a name in this case let's go back to the controller just to verify yes we have a name we have a name let's run this now so let's relaunch the application go back to the homepage refresh okay oh if you still not got the form why oh I'm in the wrong form my bad let me just copy this and now since we have multile project that's where the confusion starts right so let's go back to index.jsp here and paste so now let's relaunch the application so refresh and you can see we got enter by name so I will enter name naven let's see if that works click on submit oh it worked maybe it will not work with the second one let me just type row okay it worked let me type some weird name I will say ready we don't have that in the database okay we got empty Le that means it is fetching from database and the amazing thing is it is working and that's the magic of spring jpa or spring data jpa now this thing here it has a special feature and it is known as the query DSL now DSL stands for domain specific language which means uh depend upon what variables you have depend upon what properties you have jpa will provide you the queries but that doesn't means you can use anything you want you have to follow a particular thing here so example it should start with find by you can't simply say anything else here so it can be get by or find by and then here after that this should be your variable name the first letter has to be Capital you can't say a name or you can't say anything else you can't even say name because we don't have a variable as name the variable name is a name so you have to follow that protocol the first thing is you have to say find by or get by then you have to mention the variable name you have to also pass the name which you are searching for so that's how we can specify our own stuff but what if you want to have something more let's say if you want to provide descending order you can also do that you can also say order by if you got a list and you can if you want to reverse it you can do that you can say order by a name let's say control space okay but let's verify this once and go back to your Safari I'm fetching by name I will just submit and you can see at least we got one name but what if if I go back to database let me just insert some more in fact why from database we can do it from here uh let's say it is 106 and let me add naven one more time oh we have how can I say order by naven because we only have one record right my bad let's try so I will say submit and you can see we got two nain here and first is 101 second is 106 so I will say order by a ID a name uh here as well have to change it so I have to say order by Aid let's relaunch and let me say refresh here they are in sequence let's also say descending order so that's what we do in the SQL right we say order buy and then we also say DC if you want to see in Reverse okay this is what we wanted right so you can see we got a 106 as first and 101 as second so that's how we can play with uh with this in fact we can also apply or and and here so you can say find by a name and you can pass the first parameter you can also say or find by a name with some other name so you can try that so you can have multiple methods available so that's the beauty of query DSL here so that's great we have query DSL we can have multiple methods here but what if you want to have your own query can we have our own queries here that will see later till this point we have seen how to work with jpa repository now jpa repository provides you certain features right you can create resource read update delete but all this thing works with the help of primary key but what if you want to fet some data with some other parameter which is not a primary key in this case so yes you can create those methods just by writing find by and mention the variable name you can also specify is it order by is it live or not like or something like that you can mention all those stuff but what if you don't want to use methods but you want to use the query okay I'm not talking about SQL query here we can do that but what if you want to use jpql what if you want to use hql uh so this JP repository or spring boot data provides you that feature so what you can do is uh if you want to use a query it's very simple first of all what I will do is I will just come in this section uh so I want to fetch the alien based on their name but not using this method so I want a different method name here so I will say list of alien and I will have the method name as find just to you know just to show that it is not matching with any of the method which we have done before and then in this I want to accept a particular variable so I will say string a name the way we have done in the about code okay so once we have done that we have to do one more change if you go back to your home controller here as well you have to mention the method name as find uh so it's not anymore find bar a name order buy something we just have to mention find go back to Alien report and now here this will not work right because we have not specified in the name what you want to fetch in fact what you want to pass and is it aut by or something so in that case you can specify the query here itself and if you want to specify the query we need to use a query annotation so I'll be saying atate query uh okay you have to make sure that you're coming you're taking it from JP repository now once you got the qu annotation in bracket you can specify the query which you want now in this case I'm using jpql so I'll be saying from Alien so I'm not using SQL where you have to say select Stu from stuff you can simply say from Alien where a name is equal to okay now this uh the actual value is coming in the method itself right so whatever value you're receiving here from the controller will be passed in the query so in that case you have to use a placeholder now if you are coming from jdbc you know we have used uh what do you say prepared statement where you pass question marks here we don't have to pass question mark we have to use placeholders we can say colon a name right but how do you specify that whatever is coming in the a name will be replaced here or maybe I will say simply say name just to show the difference so I want to specify this name is a placeholder for this a name and the way you can do that is by using a param annotation you have to make sure that is is coming from uh the repository data repository again and in the bracket you have to mention the name which using which you're fetching it so it's name so whatever is coming in this a name will be assigned to this name at the run time okay that's what uh we have specified here let's verify if it is working let's relaunch the application go back to our safari and now I'm fetching a particular alien click on submit and it worked can you see that it worked so yes we can use the methods or we can use Query in fact the advantage with query you can pass complex queries okay not just this is simple but try different complex queries here so that's about how can we use Query in jpa So when you say client and server we have two different entities here and normally we imagine that client will be a human normally that's what we do right so as a human we go to different websites and we access the different resources available online so basically there's a server there now server is responsible to send your data and as a client you consume data now as a human we want data in a proper presented format we don't want our data in XML or Json or a plain text we want our data with a good layout like HML we have images we have Mars we have headers we have all these different amazing designs there so basically server is responsible to serve you HTML but then now things are changing initially when you say client server we were having machines and humans right so human will be client machine will be server but now we have two applications let's say if you are building two different websites or web resources or web services now one web service want to access data from the second web service now mind you here the first machine is a client the second machine is a server so in this case even the client is a machine now as a client we don't want HTML right I mean as a machine we don't want HTML we only want data so if one service want to access the second service data it's only about data it's not about the design so let's take an example here so let's say you have a Android app which will be showing you the current weather okay so now the thing is your application will not know the exact weather for this particular area or maybe in the weather in us or maybe in specific area in India so you're not sure exactly the values right so now here you will request the servers so we have different services available so you can send a request to them by saying hey I want to know the current temperature I want to know the current humity and This Server will send you the data not design only data because this app has a design inbuild So when you say you are sending data so how it will request so we only want data but then you will simply say okay we can do that so we can use Sur JSP we can send request will return data in fact we can do that in normal spring MC as well but we have second issue here the second thing is when we talk about Sate or JSP we normally perform actions if you remember when we create forms in forms we say action and we specify the name now that is your URL and you also specify the action which you want to take so one action will be having one URL but then what if you don't want to depend upon action you want to depend upon noun okay something like this so let's say you're going to a website you have a website which provides you a lot of resources it provides you all the employee Nam it provides you all the account details and different stuff now if you want employee details instead of saying ww. abc.com query is equal to employ you can simply say ww. abc.com employ now which makes more sense having Q equal to employee or uh Slash employe the employees right so you're specifically mentioning that you are going for a noun here you want employes so that's what we are going for so we are going for a good URI with with the help of nouns and and we are going for only data now this data will be basically the object State okay so let's say on the server we have lot of objects and you want to send data in fact the Java we do that so every object will be having data and this is represented by some pojo class so if you just want to send the data which is the current object state so you're sending the state of the object and that's why this can be done with the help of representational state and we are transferring so it will be transfer and in short we call it as rest okay so that's why we have this very famous API or the service named as restful web service or API so this is a API which will give you data that will be representational State okay that will be we are sending to the client in fact client can also send data to the server the amazing thing is when you talk about MVC when you talk about Ser GSP we maintain session so server is responsible to maintain some data but that's not the case with rest in rest scenario what we do is uh every request is stateless okay so even server don't know that this client has sent the request before so that means every request from a client will have all the data you can't simply Say Hey I was there last week you can't do that because server don't know you every request will be stateless so in that case you know if you want to let's say if you want to maintain login if you want to maintain some data that you want to inform server hey I was there last week you have to send a token okay so that means when you have sent the first request to the server server will be given giving you some token so next time when you go to the server you have to carry that token so these are the some things which are important when you talk about Rest full web service so the first point is we are not working with actions we are working with nouns okay so you simply saying ww. abc.com employees slash aliens SL laptops or even if you want to pick up a specific laptop let's say the laptop of Lenovo so you can simply say/ laptops SL Lenovo it will only give you Lenovo laptops uh second everything will be stateless so you can't simply maintain session on the server and we are talking about the state of the object here but how will you implement that so we are not going to use Q equal to something so we are not going to pass parameters in the address bar and the client will not be a normal browser oh there's one more important thing when you say rest it uses the inbuild methods of the sttp protocol so we have certain methods right so we have get post put and delete so we'll be using this method so if you want to create a new data on the server so normally we call them resources data will be resources so if you want to create a new resource on the server you will say post if you want to update the existing resource you'll be using put if you want to fetch the resource you will say get so we can use all this normal HTP methods in rest API but how exactly we can do that that we'll see in the next video once we know the theory of rest let's try to implement it but hold on we have a issue here when you say you have a normal web application so we have a server and then we have a client so normally when we imagine client we are talking about humans but on the other side when you talk about rest it's more about a client server but then here the server will provide you some content but not a design or not in a format of HTML So when you say a client it will be a machine or it will be an application now this application we have to build it maybe Android maybe a website but then since we don't want to do all those stuff we don't want to do something extra let's use a client which is provided by someone else the client which we are going to use is specially used for rest testing or rest API use the client name is Postman in fact we have so many rest clients available but Postman is quite famous I will say Postman initially I was using it with as a extension for a chrome you can see we still have it I guess it is deprecated uh so they recommend you to to use the application so it doesn't matter is it Windows or Mac it still works on all the OS available so if you click on this so you can see you will get an option of download so depending upon which OS you're working with you can select it so click on download and it a simple installation say next next and your job will be done now in this machine I already have Postman with me so I'll be using it so it will take some time to download so let's not use that let's use the existing version so I'm launching the postman which I have and it will ask me for login okay I will simply sign up with Google or sign in with Google so I'm starting my Postman here okay so it says the Chrome app is being dicated okay I know that close that and close this one now you can see this is how your Postman looks like I guess the I'm still using the Chrome extension but that's fine I will just install the new one for the next video but as of now this will work the UI is same for everything so you can see see we have a history here for all your uis and this is where you will fire a particular request time being let me request google.com and let's see what happens so I will say google.com enter so I'm sending a get request to Google send okay so you can see we got the data here normally this is your HTML page but then when you say rest API you will be getting your data in XML or Json so this is where you will see your XML or Json we have an option here but then we are not returning the XML or Json that's why we will not be seeing that so this is your get request if you want to work with post it's very easy you can click on post specify the URL or URI here and click on send if you want to use put for updating data you can also use delete for removing the content if you want to send data with some headers you can do that here uh if you want to send actual data you have to put that do that in body we have certain options here again we'll be seeing that everything one by one so I hope you will be installing Postman for after this and then you'll be doing a proper setup so that you can be ready for the next video now once we have our Postman ready let's change the code now till this point we have worked with normal spring MVC where where we were sending data we were fetching data but not just data we were fetching the entire page that's what we do in normal MVC right and all the requests were actions so we were sending post request we were sending get request but here we let's use noun so what I will do is I will open my Postman and here we'll send a get request now this time I want to fetch all the aliens now if you see the earlier one if you go back to your code and if you open index.jsp what we are doing is we are sending the we are saying submit and the action is ADD alien so in the address bar it was add alien in fact to fetch all the aliens we were using get aliens but why you have to say get aliens which sounds like a verb here we want to use noun example if I go back here I will simply say Local Host colon 8080 that's the protocol we are using slash so instead of saying get aliens I will simply say aliens and here you can simply say send and this should give you data now here you can see we don't have any mapping there which will handle aliens and that's why we got an error so the error is it says 44 not found okay that's great we got the error and if you see the we got the error in Json format that's weird I know uh okay let's let's go back to the application now here what I want to do is now first of all we don't need the t. JSP anymore in fact we don't even need any JSP because we are not using views here we are simply working with Json or XML so let's go back here and now in the controller I want something which will handle my aliens mapping now in fact I can create a new controller or we can change the existing controller now I'm quite famous when it comes to changing the existing code so let's do that so I will go back here this is the same thing we want right we want this particular method so I will just copy it uh and I will say paste here itself or maybe it will make much more sense if you create a new file right let's do that so here let me create one more controller I will say new and let's name this as a alien controller of course you can have a different name but since you working with alien let's do that so I will say alien controller okay nothing much I want it to be in the same package that's fine click on finish now you can see we got our alien controller and in this I want that particular method which is the mapping so we got this method right here and then I want this to be a controller so let's do that so I will simply say controller nothing fancy right now we got our class let me put the package first okay but don't you think it should not be get alien should be aliens so I will say aliens here okay and then we have one more we don't need model anymore because when you say model that means we are working with session we are sending JSP here we are not sending JSP so we don't even need a model but we want to fetch data right and we have done that before we wanted repo as well uh if you remember we also need a alien repo which will fetch data from database let's copy that as well okay so once you got your alien repo let's fetch data so I will simply say report dot we want to fetch all the aliens I will say find all as simple as that but this will return you the list of aliens now we have a choice you can save that in the list or you can simply write returns report. findall that's your choice I will go step by step I will say least aliens let's fetch aliens here so control space and control space for this as well okay once you got all the aliens here we are not supposed to specify the page name because we're not using JSP so let's specify aliens but will this work think about this this aliens this aliens is a list object we are sending data to string we don't want that right so what I will do here is I will convert this into two string let's try let's see what happens now once we got this let's run this application and let's see what happen so let's relaunch the application here and go back to postman and here you will send request here and we got an error can you see that it says again not found that's weird we have a mapping for aliens right but look at this output it is giving you the message which is all the aliens that's weird we got all the aliens but still it says error the thing is that's how your spring m is built right spring MVC says whatever you return I will search the JSP with this name because that's how you we have configured the application if you see our application. properties here also we have said that whatever name you will send I will simply add JSP and we'll search that name in views so whatever you're sending here is actually your JSP name we have to mention hey this is not a JSP name which I'm sending I'm sending actual data the way you can specify that you are sending actual data is by using another annotation which is response body so when you say response body here it will send that data as a data not GSP name let's relaunch the application go back to postman and here you will say send can you see that we got our data but with small error okay it's not actually the error but we wanted Json right we wanted XML but we got a plain text this is not Jason we want data and Jason format if you want to achieve that if you want to send data and Jason format don't set two string because when you say two string it will convert your data into string we don't want that so we'll send aliens and here as well will not send string let's return the list as it is now you might be wondering will it work as is yes it will work let's try let's relaunch the application and here if I say send you can see that we got our data in Json format that's what we wanted right we got Json so that's how we can have a get request now what about post request what if you want to fetch one particular alien that we'll see in the next video so we have done with the first step of rest where we were able to fetch all the aliens and that to with a URI so if you want to see this is a URI which we are using here so we are saying local colon 880 alens so when I say all the aliens when I click on send it will send a get request to the server and from a server side we are returning all the aliens but that to a decent format the amazing thing is we are not using Json anywhere I mean as a programmer we are not converting our Java list into ad Json and that's where your spring boot provides you this amazing feature so if you can see your M dependencies we have a converter so you can see we already have a converter here which is Jackson so Jackson is responsible to convert your Java data into Json now since it is inbu so we don't have to change the things by ourself it is that easy now what if you want to return XML can we do that with Jackson that will one question but then at least the Json is working and that is happening with the help of Jackson here if you just go to Google and if you search for jacksson so we are searching for Jackson documentation and that's your official website of it so they have it on GitHub let's go there okay so Jackson basically convert your Java data into Json so you can see it has Json Passa in that we have Jason generator so Jason using Jason generator you can generate Json files and vice versa as well so what from a client side you receive a Json so it will convert that data into Java object that's how you can use Jackson and in fact you don't have to add in the project will be done by Spring boot now once we know how to fetch all the values in J format and that with the help of Jackson now I want to fetch one particular record normally what we do is in rest API so when we create our uis what what makes much more sense is let's say I want to fetch one particular alien here which is mayure if you want to fetch mayure we will be saying 104 and the way you do that is by saying first of all this is your actual website in fact you we can replace this with the website name then you will instead of saying aliens you will say alien because you're fetching one particular alien but now you will also pass the AI D which is 104 so instead of saying Q is equal to 104 this makes much more sense to have a u like alien sl14 you know this is more like fetching a particular resource from a computer so if if you have a Windows machine or any other machine you know when you go to any particular folder inside that folder if you go to particular folder and then if you search for a file the entire path will be something like ABC pqr SL maybe text.txt that's how you have the path right because it shows the path of the file or the source in the same way we have this here so when you click on send you can see this is not working yet because we don't have any request mapping which will accept alien sl14 now if you want to achieve that we have to create a method here so we'll say public now this method will not be rning the least right it will return only one particular alien and let's have the name as alien now this alien will accept a particular Aid so I will say a ID here now this has to be mapped so I have to say get mapping since we are using get here and then we'll be having mapping like alien but then we have to also mention 104 here right because that's how you are mapping so we have to say 104 and here we have to return the alien so the way you can do that is by saying alien I want to fetch first fetch the alien alien equal to using repo so you can say repo do get one so get one is a method which belongs to JP repository which will give you one particular alien and let's pass a ID here and I guess our job is done we can simply return alien but do you think it will work what what is your first reaction now see it makes sense because whatever we have done is almost same with this method we are missing one thing we have to also say request response body before the method but we have one problem so this might work for 104 what if you have 1 Z3 what if you have 101 okay we have to make it variable right you can't simply say 104 here because it will only work for 104 in fact you know let's try this for 104 ons and let's not accept or maybe I have to accept it so it will give you because we are expecting something here and we are not passing it let's see what data you get so let's go back to the postman and let's say hit and you can see we got an error it is your status 500 and it says optional a parameter a ID is present in the parameter but it cannot be translated to a null because we are not passing the values so path is there's no 44 this time but then we have to assign the Val to here right so this 104 need to be assigned to Aid so what we have to do is since this 104 will change let's put that in a cudly bracket so you have to say a ID so that's how we work so if you think the URL will be different it will be alien for sure slash it will be some number we are not sure about the number so we can simply put that in cly brackets a ID and here we have to use an annotation which is path variable now let's restart the application and let's see what happens let's go back here click on send and we got an error okay okay I guess we have missed something here so we have to also mention what is the name which you're trying to map here so you can see I'm mapping this a ID with this particular Aid so we have to use path variable Aid here and now let's relaunch the application I hope that was the issue so let's click on send and okay we are still getting same eror it says type definition error so let's try find by ID because maybe that there's a conversion issue with get one okay so find by ID gives you a optional aliens that's what happens in Java 8 you know in Java 8 just to make sure that you don't get null values we get an optional value there so the only thing is you can do here is we can say to uh we can say or else and let's return the blank alien object so that we can say we have not received we don't have that alien in the database so we can say new alien and we can have a value which is let's say zero comma double quotes Okay so this should work now I hope let's go back there let's send Okay so is working so that there was something wrong with get one okay so that's how you can do it so we can say find by ID what if this ID is not available in database and that's where you can pass blank value so what I've done is if I go back to my hit and let's say if I search for one Z we don't have this value database so if you click on send you can see you will get empty data and you can say you know we have not found it so that's how you can work with one particular data so till this point we know how to fetch all the aliens and then we can also fetch one particular alien and for that we have defined two methods here one is get aliens which will give you all the aliens and the way you can do that is by saying get mapping which is very important but then if you can see we are also mentioning one more annotation here which is response body not just here here as well because both these methods will be working with rest I mean they will be returning the data in just format don't you think in future as well we'll be having some more methods which will be let's say fetch one particular alien based on some other parameter or maybe you know you might be having five to six method in the same class now when you know that all these methods are actually rest methods and which will return Json or XML what makes much more sense is to not write it everywhere it should be common right we can mention on the class LEL itself that whatever you return will be Json or XML and the way you can do that is by changing this controller annotation So when you say controller it will behave like MVC controller but but you can mention that hey this is not a normal MVC this is actually rest and the way you can do that is by simply mentioning a new type of controller which is a rest controller so we can say control space let's import the package and when you jump to this controller so you can see it is actually based on controller but uh it's more for response body so this is a controller also response body let's restart the project and let's hit the query once again just to see if it is working let's click on send and you can see it is working so whenever you are sure that you want to build a controller but that will be a rest controller always use rest control instead of using response body on every method but yes let's say if you have five methods out of which you have three methods as rest two methods are normal MVC uh then you don't have a choice you have to go for normal controller and response body on those methods which returns Json XML okay so everything is working but now what if you want to send the data because till this point we have seen how to fetch data but now I want to send data so what we will be doing is if you see database we have four records here right we have four in fact we have five 106 is also there n so we have five records I want to insert a new alien and that to in from Postman so we don't have a form officially we don't have HTML page where you can send data we have to do that from this particular Postman instead of saying get we'll be using post now get is responsible to fetch data and post is responsible to send data to the server and the URL now since we cannot specify the URL by specifying the query string here so we not be doing that in post so we'll send the request to alien itself okay but we have an issue don't you think we already have alien for get mapping the advantage with rest is you can have the same URI but with two different methods one is get one is post so get will be used for fetching data post will be used to send data and that's how you can differentiate between two methods okay we'll see that once we write the code will make much more sense so you can see we got we sending a request with alien and when you click on send it will send a request but hold on we got an issue it says 404 why because we don't have a code on the server right and then we are not even sending data how do we do that if you want to send data you have to click on body and here you can type the raw string you can have your own Json format here or we can actually use this form data in this form data you can mention key what you're sending so I'm sending a ID and the value for me is let's say 107 and I'm also sending a name and the value for me is let's say KL so I'm sending this two data 107 and KL here and that's it just click on send and it will send your data but yes we still don't have the mapping here let's do that quickly so here in the code we'll have one more method which returns same object alien and we'll have this method name as ADD alien now will it accept anything and the answer is yes we will be accepting an alien let's have this as alien itself let's return the same alien again but before doing that we have to add that data into the repository so we will say repo dot the method which we use is save so we can say save and we can pass the particular alien our job will be done that's it oh okay not that's it we still have to do the mapping right uh so that will be done with the help of you it right so the way we have get mapping we also have post mapping so let's use it post mapping and here we'll say alien we don't have to specify anything after that no slash nothing because data will be coming in post so we can directly assign that to alien so you can see we have both as the same URI what is different is the method name so this is get and this is post so there will not be any confusion let's restart the application let's go back to postman and here click on send and you can see it worked at least we got our data here and uh there's no error let me just go back to database to verify let's hit refresh and you can see we got it we got 107 and coml see it's that easy Once you have everything set up it's very easy to add more methods and then doing stuff so that's how you can fetch all you can fetch one particular data you can also delete data we can also do that we can also update data with the help of put mapping but before that I also want to talk about the content negotiation example if you look at the code now this is your Json format what if you want XML you can see we still getting Json format even if I say XML in fact the idea is you can say I want all the aliens and you can hit a get query you can see you'll be getting all the aliens and even if you say XML you will still get Json so how will you get actual XML here the way you can do that is by going to headers and inside headers you have to say I will accept so I have to mention I will accept only XML so we'll say application slash XML hey I'm not expecting Json here I'm expecting a XML format by doing that you have to send a request now and it will return you XML but you can see this is not working and we also got a code here which is 406 not acceptable what's the issue it should return XML right but unfortunately what if I say okay it says cannot find acceptable representation so what is happening is by default in your code we only have a support for Json we don't have a support for XML and to get that support we have to add one more jar file what is that we'll see that in next video so let's do content negotiation here so when you say content negotiation it's quite simple from your client client can mention do we need XML or do you need Json in fact you can accept both that's your choice but here we mention something in fact in the last video we tried to fetch XML but then it's not working you can see we got an error the thing is by default your spring boot supports Json and that's why you got Jackson Library here but if you want to get XML support you also need to add one more jar file now which is that so let's open our MAV repository and here let's search for Jackson data format XML so since we are using Jackson let's continue with that and then we are using XML now okay now make sure that you pick up the version which is suitable for your application so whatever version you have for uh normal Jackson so you can see it is 2.9.1 let's pick up the same one or which is the latest one here uh let's copy that and go back to application we want to do that in a pal file let's do it here let's add it here paste and now you can see we'll have one more jar file for Jackson XML format so yes it will take some time to download and I guess it's done so let's restart the application go to postman so when we did last time we got an error which is 46 but this time let's see what happens and this time you can see we got all the values in XML format that's how you can do content negotiation I mean not just for fetching data but also for sending data you can also mention the server side if you want to restrict what type of data you can return example you can just go to server side and here you can mention hey I don't want to return Json even if a client ask you for Json will not return that will only return XML you can do that here so here we have talked about get request we have talked about post request and the amazing thing is from a client side now you can actually mention what you need example if you go back to postman so let's say you're sending a request for aliens so if you click on send you can see we got all the aliens here okay but we got got it in the format of XML because we are mentioned here so if I remove this part and if you send a request you can see you got Json and if you say XML uh in fact you can request XML as well so you can just go here and you can mention you want XML there it will fetch XML but can you restrict it so let's say from a server side you can specify hey I don't want to return the Json I want to return only XML you can do that so here you can say path and then you can give comma here and you can specify what exactly your server is going to return is it XML or is it Json so you can request whatever you want from a client side if you request for Jason you will return you Json by default it will return you Json but if you specify hey I want to accept only XML then it will go for XML so if you click on send you can see it will return XML but what if I don't want client to request for Json so you can mention that here so you can go back here and say say producers so we have a attribute as producers and you can in the curly brackets because you can specify multiple types here uh so you can specify that in this cly bracket so you can give a comma and you can specify that but here I want to accept only one so I will say application SL XML so this is the only thing I want to return let's see if this works so let me restart the application go back to postman and you can say send so you can see there's no error when you request for XML but what if you request for Json now the request for Json uh you can see we got the same eror which we were getting for XML so it says 406 not acceptable right so you can specify from a server side what you need or what you want return you know it makes sense to return return both XML and Json but sometime server has a choice you can specify hey let's not work with XML let's work with Json hey let's not work with Json let's work with XML so you can mention that with the help of prodes in this server code likewise you can also accept data from a client and in the post mapping you can specify what you want to accept we'll see that in next video so with the help of producers we can specify what a server is going to respond is it XML or Json or some other format but what if when you send data from a client to server with the help of post request let's go for post here and let's sending a new alien now and this time let's specify the body and let's specify the values as 108 and let's have a name as braine now if you specify the data here let's go back to header now in header as well you can specify the data in different format in fact in body we are sending a form data okay but then we have not specified will it be Json or XML so you can go to header and you can specify here content type and if you say application let's say Json and and instead of going for form let's go for raw so you can see in raw you can mention are you sending a Json or XML so let's send Json first and the way you can send that first of all we don't have a j format let's create one so we'll have cly brackets open the first attribute is a ID let's give a colon there and in double codes we can mention the value uh so the value let's say it is 108 we'll give a comma and here we have the name as a name and the name is pin so now we want to send this data and we are sending a Json data so in headers we have specified that we are sending Json the body type is also Json let's click on send and it worked is it no we got an error what's wrong there's nothing wrong on the server side we got 204 let's verify from the database I guess I'm not sending the data properly let's verify let's go back here click on refresh oh it says null that's bad okay is it something wrong WR with the format let's use an annotation here which is request body I guess that's the issue let's try once more and it worked okay now what was the issue so normally when you send data from the client to the server of course we have two ways we can use a form or we can send data in Json or XML now when you send data in Json or XML we need something to will convert the data into the Java object format and that's where you can use request body so remember we have also worked with response body before so response body is responsible to convert your object into Jason if you want to do reverse we have to use request body so that was the issue that's our Jon format and we can send it so this is working you can just go back to database we have added some two null values Let's ignore that and we can see if I say refresh we got the answer now this is working but what if on the server side I have to mention hey This Server will only accept Json because now this will accept both Json or XML or maybe I will I want to specify only XML so in that case uh you will specify so we'll say path equal to aliens and alien and then here we'll specify consumes in cly brackets in fact we have two options here we can specify in double codes the way we have done for producers otherwise we have an option of media type so we can say media type dot uh we can specify the type here we can say application so it depends what type of data you want to work with is it you want to work with this type where you have double codes or you can use this type I prefer go for the double codes part because it makes much more sense to me but that's your choice so you can say application.xml and then let's work with Json here so let's say it only consumes Json because we are doing XML let's consume Json and let's restart the application and you can see if I give a new data let's say 109 let's stick to PR now and let's click on send so it works there's no issue but what if you want to send XML will it accept that now first of all to to get the XML I need to know the XML format okay so that's our thing so let's say copy this I just wanted the format so that's why I'm doing all this stuff so I'm sending post request now I want to specify the content type what I'm sending so I'm sending XML uh in the body I will mention the XML now so we'll say paste uh this is XML that's right but then the data which I want to send is 110 and the name will be let's say Asha and click on send so you can see we got the error it says not supported so content type application XML not supported because we are accepting Json here so that's how you can negotiate with the content that's how you can specify uh what you want to accept and there's one important thing which we have done in this video is request body so if you want to convert the data from the Json to the Java object you use request body so that's from the content negotiation where we talked about producers and consumes so basically we are going to build our project in jav Java now see it's not about how uh big your project is or how complex your project is it's all about understanding how do you think when you make a project okay it's all about thinking it's all about uh working on something okay so we are going to build a project and this will be a Project based on a product so how do you manage a product now basically U you know in my office there are so many things in fact when I us to when I used to work alone uh at my home or with my office I I had a small team okay so I was only one and then someone used to be there there to manage everything especially H was there and now since we got a good office we got uh lot of machines in the office now for different purpose for some for development some for videos uh some for editing and some used by uh the admin team right uh so there are so many things in fact you know I wanted to show you the entire walk through of my office office and so many things what happens you know at this point now uh there are certain things in my office which we use regularly example the laptops uh the sound system uh the AC remote these are frequently used things right but let's say there are certain things which we don't use often example cables the C Toc cable HDMI cables uh there there are one or two laptops which are lying uh in my uh in the drawer which I use sometime depend on if I want to travel somewhere I want to carry a lightweight laptop or if I if we want to do some testing of some uh some software some service we use a different machine there right and those are there in the drawer now what happens is the moment I need something I will ask hsh hey hush I need that particular thing and then it took it take normally uh 10 15 minutes for us to search in the end all the draws where that thing went and I thought let's solve the problem okay so hsh are we solving your problem today Yeah so basically let's create an application a console based application because we have not learned a GUI or web development till this point so let's create a console based application where we'll try to track the product so where that product exactly is is it in the drawer is it on the table uh and we can basically search a product where it is we can basically search for a particular type of product let's say I want to search the laptops in the office or in that particular drawer what other things are there so you want to know and let me know if it happens with you as well at your home you know uh before before my marriage I used to keep things here there and then I used to find it very easily but after I got mared now everything at my home is that exact place nail cutter should be here uh the the clothes should be here right and then since they are properly organized now I find things difficult to find yeah so let me know if it happens with you as well so that's what we are going to do in this session so what exactly we are trying to build here so let me share my screen okay uh just need to log to my okay so basically we are going to build a product management application now in this application what we want to manage is products right now let's say if if a client comes to you by saying okay I want an application to manage the products now what you will do uh so basically you will have something like this right okay let me just hold on a minute okay so uh if if a user comes to you by saying okay I want a product based application right now what you do is especially in language like Java and when you are working with the database Concepts you need to basically create entities now what are entities if you heard about Java everything in Java is object right so if you want to imagine something always imagine them in terms of object now if you talk about a product even product is object right so a product will have certain things so example if I talk about a product here now this is one of my entity uh product yeah this is is my entity basically okay let me just write in the bracket this is my entity right and for this entity of course every object or the entity will have some properties of course it will also have Behavior example if you talk about your computer that's that behaves some way right uh in the same way if you talk about uh let's say something like a camera camera also behaves but it also has a property right so if we talk about a product it will have certain properties now what properties I want of course uh we can have different properties to a product but just to classify for this particular example uh let's say we have a name for a product so a product will have a name a product will have a a type of product let's say if you talk about uh my machines those are computers right the Mac Studio which I'm using here is a computer if you talk about cables the type type- C cable that's a cable right uh we can also have something like uh a mouse example if I'm using this particular Mouse here which is Apple Mouse now this is a mouse yeah so that's a type next we we can have is uh the place where it is stored okay so example if I want to find something I want to find where is the Asus uh laptop Asus W book basically that's one of my old machine which I was using but it is now used for the development purpose so if I want to find where that machine is I should be able to find it right it can be on different tables it can be in different draws so I need a place and also uh I also want a warranty know this is the problem which you face when you have multiple device when you have only one or two device with you you actually remember okay for this particular mobile phone I have this warranty for this particular laptop I have this warranty but let's say if I have so many machines in my Mach in my office I want to know if I go for a product I want to know the warranty of it so that I can claim it what if uh what if the laptop is not working at least I will know if the if it's under warranty or not okay uh yeah we can also have color we can also have the owner who owns that particular product right yeah makes sense right we can have all this properties but just to keep keep it simple let's work with this for and then you can also mention uh when you do this project you can also add some extra things to it so that's my entity right okay now if you want to implement this in programming how will you do it so let's get back to our code uh this is our coding screen right and in this particular coding screen what we're going to do is uh we will write the code so that you can achieve uh the thing you can manage the products right okay so what I will do is I will go back here and this is the IDE which I'm using and one of the question which I got yesterday was about uh which IDE to use and the thing is you can use any particular ID which you want uh you want to use uh intell that's your choice in idea uh you can use Eclipse you can use net beans you can use vs code your choice okay ID is not something which should you should stick to I use different IDs on different machines uh if I want to make a YouTube video I normally go for vs code because that's very lightweight and most of you are already using it so if I want to build a project normally I use intellig idea to build to build a project because it gives you the different features yeah and so again you can use any IDE so what I will do now is I got a project which is product product demo and in this I have this SRC file which is at empty at this point uh let's create our first file and this will be a file of main now why it is main is because the execution will start from this particular class and of course when I say this is a main class I also need a main method because I want to start the execution right uh that's something we will do here let's say if I want to manage a product so how will you do it of course you can create variables right we have mentioned all the variables here if you can see we have a name we have a type we have a place we have warranty now this can be your variables as well so I can uh simply come back here and I can say string uh product or maybe I can say string name one since we have different products we can have name one and I can have my first product as Asos uh VI book which is the laptop and then I can say string uh type one is equal to what is this type this is a this is particular laptop and then I can say string uh Place one and this is is sitting on uh brown table so basically in our office we have colorful tables I'm using a white table uh the the table which I use for the development purpose that's a black table uh the development team uh developer sets on the brown table right and editing team I'm not sure which okay it's also Brown yeah okay so is it white white and brown white and brown Okay cool so I can say a brown table right and then I can say string uh what is warranty uh warranty one and I can say this is uh okay warranty should be integer right so let's say this is 2024 or 2022 I guess the warant is already over so we can have this values here okay this is not string this is int yeah and then for a different product now this is one product right what is a different product now we can have what do you think if I want to have a different let's say a mouse how do you specify that of course I can just simply copy this and paste it here and this will be product two or name two uh type two place two and warranty 2 of course you can change the value to uh Apple Mouse and then M type is Mouse uh where it is sitting it is sitting on the white table and then uh the warranty you can mention that so do you think this is a good idea to have different variables like this or we can create a separate class where you can have uh the types and instead of having four types here we can have one type which is product of course right in object un programming it's all about uh creating a object or a class right so if you want to create an object you create a class first so in Java basically if you want an object you create a class so I will right click here and I will say new class and I will I will name this class as a product which also represents an entity okay um so and I forgot to do one more thing so today we are focusing on uh building this product as a console based application so because whatever output you will get is on the console but of course in this current ERA we don't do that right what we want is a web application a GUI or some some way to interact with the application so what we'll do is today we are going to build this on a console tomorrow we'll build this on the uh tomorrow we let try to connect with database because today we are not going to use database we are going to Simply have the data somewhere in the application itself tomorrow let's convert that into a database and then uh if we have enough time we can also create a server to it okay so yeah so we can have a website development so we will shift the Entre application to a normal application to a web application okay so here what we can do is we can create those variables so what are the variables we have we have name type uh place and warranty so I can simply come back here I can say private string uh name and then we can have private why I'm using private is because in Java we have concept of encapsulation uh so all your variables or properties which you say should be private and the only way to access them so what is the only way to access them so the only way to access them is with the help of gets and sets so I can say string uh name then we have string type then private string place and private int uh warranty see for warranty we can actually go for exact date as well but then just to keep it simple let's go for the year only okay so you can see we got a class here which is public and the name is product and we have four variables and if you want to if you want to follow the concept of object oriented programming we also need a Constructor so maybe I want to create an object by passing all these values and maybe I want to create an object by not passing any value so I need a Constructor so I can you know you don't have to type the Constructor you can simply uh right click here and you can say generate and you can click on I want a Constructor so if you want a default Constructor you can say select none and you can you you got a Constructor but I also want a Constructor which can take parameters so I can say generate uh Constructor with all the variables and click on okay and you can see we got a Constructor with all the variables uh next we can we also need a Getters and Setters so basically these are the methods using which you can assign the values and fetch the value uh because all these things are private right so the only way to access them outside this class because see this is a different class many is a different class so if I want to access them I can simply create get us and Setter so I will say generate get a Setter for all the variables and okay right our job is done okay if I go back to main now I don't want to assign values like this right so what I can simply do is I can uh come back here and I can create an object of product I can say product p is equal to new product so we got uh the object and then I can simply print this object so tell me uh yeah of course AKA says we can use here Lok as well so let me try to do Lok tomorrow so once uh this is over because this will take a lot of time uh tomorrow we'll try to do long walk as well so if I try to print this what what will be the output of course let me just run this code to see what happens we can just click here say run main so you can see it is printing something weird uh it's printing some hash values and uh the name of the class we don't want it right we want to print the values so whatever value this P has the product has I I want to print it and to achieve that we can use something called uh a two string method so in Java if you want to print an object and if you want to print the values of it you can just right click here and say generate two string for all these values and now what you're doing is every time you try to print the object or reference it will call the two string method and it will print something something like this so let me try I will just run this code once again and you can see uh we got the object and the output is something like this you know what I will do okay let's keep it there itself so you can see it is printing the product name is null type is null place is null warranty is zero is because we are taking the default values so basically when I create this product I should add some values here right so we can do that okay but let's do that differently let's say I have some products with me and I want to add them now we can do that in main as well but then there's a in software development there's a concept of Delegation what you do is you don't do everything by yourself in fact let's say if you're working for a big company and you know we always have these questions if you let me know if you're working on a company and you feel that you do all the work and your manager manager is doing nothing do you have the same uh thoughts so if you feel your manager is doing nothing then you're wrong is because okay there are some managers who does who does nothing but then managing people is very difficult task in fact in my office if you even if I have less number of employees you know it it it consumes a lot of my energy just to manage the team just to manage the work uh but then we have this idea of Delegation right so from the upper management if there's a decision made it should be assign it will be assigned to the lower people who are lower on that uh tree and then the these people who are managers assign the work to the people who are actually working example developers testers uh admin people right so uh that's how you do it right so we we have the hierarchy so that's delegation so we delegate the work in the same way if you want to do all those things we want to add the products we want to search our product I don't want to write all the logic on Main so main should be simply saying Okay add this product main says give me the product the actual logic will be somewhere else and that well where we'll write the actual Logic the actual logic of all this thing will be going in a separate in a separate file so I will right click here uh on source and it will say create a new class and I will call this class as a product service okay now this class is responsible to do all the things so logic should be here of course you can write the logic in main but then let's follow the concept of Delegation here so let's delegate the work how do we do it how do you delegate a work so of course I want this to do everything right uh so what I what I will do is I will say create the object of product service here so product Service uh let's name this as a service itself equal to new product service yeah we can also say this is a service layer so when you move to the web development or Enterprise development thing this is called a service right a service layer okay now to this service I will say hey service I will say hey service add the products okay uh how will you do it so I can simply say service dot add product so I want service to give me this option of adding a product but Services unfortunately I don't have uh any method in this class which which ISS add product so what we can do is we can go back to service and let's create a method so public void add product and in this it will add the product but then where is the product and which to where to add now basically since we have multiple products right we don't have one product in my office we have I don't know let's we'll calculate one day how many items we have there are so many items which are which I think is of no use there is a air pufy air freshner sitting there okay we have used it from a long know last time we have used air a a uh fresh now maybe last month so uh so let's say we have so many items in the office right so many products how do you store them where do you store them of course it should be stored in a database but then in today's session we are not talking about it so one place where where you can store all the products is a list so we can create a list of products now people who are finding this difficult to understand see we are basically building project right I'm not teaching you basic Java here if you're still confused there's an entire playlist on Java so I can say list I want a list here control thing yeah okay uh so we have to also import the package the list package or List class list interface from this package and let me name this as products equal to and I can say new okay now list is a interface so the class is at a list so we got a list of products here okay so I will add the product in this uh thing so how do I add it's very simple get the products and in this now since product products is a list here right so we can have we have so many methods there so we got products do add and we can add a product but which product we can't simply say add product and it will add we have to basically pass the object of a product so here in this add product I have to pass the object so what I can do is I can say product p is equal to new product and and this product will have some values now what values I want uh the first one I want is uh let's save uh Asus let's go for that Asus Vivo book and this goes in a type this is of type laptop the next thing we have to mention is okay we got yeah place where it is where it is set so it's on the brown table and what's the warranty is 2022 so if you can see this is the product which you have created now once you got this product object I can simply pass it here so what I'm passing is I'm I'm passing this P so if you can see I'm passing a product reference to this add product now the same product will go to product service and we have to accept it here so I will say product P again and just add a p our job is done does it make sense now okay so we have added one product but then we have so many products right uh so we have to create those many number of objects here and add it right so we can do that uh just to uh simplify the work what I've did is I've created this Sublime file okay it's not opening Yeah so basically I have okay before doing that one I can do one more thing so don't you think of instead of adding P here we can simply take the entire thing from here and paste here so we don't have to create a reference we can simply create object with the help of new keyword and add it to the add product does it make sense and since we have so many products here what we can do is we can I can simply copy the entire stuff and replace with this okay it will save our time right so I've added all the products and you can see we got all the products here now if you try to get this list this list will have this number of products I I'm not sure how many products we have 15 maybe yeah these are the anonymous object so what are the things we have we have a type c which is a cable uh which is there in the black drawer we have a Mac Studio which is a computer sitting on the white table we got this mixer the audio mixer which is there it's audio system white table then we got VI book which is laptop brown brown drawer then we got Asus Rog machine laptop it's on black table then we got MacBook Pro laptop brown drawer the vacom pad which where we can write it's a writing pad black drawer and then Leist goes on you can see we have uh keyboard Logitech keyboard HDMI cable Java blackbook uh Logitech mouse Apple Mouse Lenova Mouse and blackb now this blackb is basically a machine which is used for the editors editing machine we we have named it as a black beast it's a computer on a white table okay so cool does does it make sense we got all the products here now let's say once I got all the products I want to uh let's say I want to print all the products how will you do that how how will you print all the products it's very simple you can say hey service hey service get all products is as simple as that okay if you want something just ask for it we are saying service get me all the products but Services I have no idea what I'm talking about okay so if I if you want to do that go back to product and here create a method which will give you all the products right but my question is when you are returning all the products what should be return type when you return all the products what should be the return type so the return type is list of product right we are not returning one product we're rning all the products so we have to say public uh list product and then we can say method name is get all products and then we have to return here now what we want to return so where do we have all the products in this products here right so simply return products our job is done right so I can just go back to main and here now you can see there's no error it is just a warning now since get all products will give you the least of products what you can do is you can accept it here so I can say I'm getting a list of product and we'll name it as products so basically get all products will give you the least of products does it make sense now once we got this list how do you print it there are multiple ways of printing a list you can simply say s out and you can say products and it will print all the products let's try let's try so I will just run this and let's see what it prints it prints the least so you can see this is the first product and if I have to scroll this is my second product and if I scroll this is my third product and scroll and this goes on but instead of printing that in one line I want to print it one by one one so what we can do is we can use a loop here and specifically can we can use enhance for Loop just write all these terms okay so if you're not sure what I'm talking about if you want to watch the video later uh the concept video so example let's say if you don't know what is enhanced for Loop just write it down after this session I want to learn about enhanced for Loop then after this session just go to YouTube and search for enhanced for Loop and uh you will get my video there watch it and hit that like button okay here also and in that video okay so I will say product this is how you use enhanced for Loop so I will say product P from products so what it Bas basically does is this product will have lot of different values right a lot of different products it will pick up one product at a time and that particular one product will be there in P just print it so take one product and print it and let's run this code and let's see if it is printing one by one yeah you can see that it is printing one by one now these are all the items we have so basically we were able to uh save the products we were able to add the products here right this number of products there should be a way to you know minimize it uh yes in this 10 days we are also going to see examples of hibernate okay cool uh so once we have printed everything uh what next what what I want here is let me just print uh not so to let me print the line here so that we can divide the output right so this is the first output which we got all the products next what I want is I want to get let's say a specific product not all the product but a specific product okay so let's say out of this I want to see where is the Logitech mouse okay I want this particular product it's very simple what you can do is you can just go back here and say hey service let me also print what we are printing so we are printing a particular product okay so you can go back to your service by saying hey service get me a product get me a product whose name is so I'll say whose name is uh logy Mouse will this work what do you think will this work um so if I go back to service we don't have that method right so what we can do is we can create a method so I can say uh public now this time it will return only one product so I can say public product get product which will return one product right but then it will also take a name which product I want to fetch so I can simply say string name right but the question is how will I get that how will I get that one particular product what do you think okay so basically from this particular list of products I want to find one product which name matches with this so if you want to search and that's where the searching algorithm come to picture right but we don't we will not do that we'll do a linear search okay so what I will do is I will just go for a for Loop and I want to iterate between all the products so I can say product P from products so iterate between all the elements and check if a particular product which we got get the name of it so whatever name you get from this list of products compare with the name which you got here how do you how do you compare two strings in Java you do that with the help of equals so equals and you will say okay not a name but a name right and if you find the product simply return it your job is done right or you can simply return can not inside for Lo if you don't find anything return null of course not a good way but then yeah just to get the just get getting rid of the error we can say return null otherwise you can return the empty object if you want okay um and then you can see get product there's no error and of course it is returning a product so I will simply say product p is equal to this and then here we can print the product let's see if this works let's run and you can see we are searching for logitec Mouse this is the output which we got here because of this in fact you know what we can do we can commment this section which we don't want now let's once run once again and you can see this is what we wanted we want a Logitech mouse okay and yeah now you will be thinking in the code we have to mention Logitech mouse what if you want to ask the user yes you can do that you can take the input from the user and if you don't want console you can build this as a web application that's your choice okay so this makes sense right okay so what we wanted now is uh searching with a particular place let's say okay in fact that be your assignment okay that's your first assignment thing which you do so there are two things which you have to do as assignment for this particular project which is what if someone wants to search based on the uh Place let's say I want to I want to find all the things at this particular place let's say on a white table I want to know all the things I want to know all the things which are there in the drawer uh so that's your assignment first one the second assignment question you have is I want to see what are the products which which are out of warranty example all the products which are which has a older date if you can see we got 2022 here 22 done 2020 all this warranty is completed right so I can't I can't uh get it solved now or repair from the company for free right so I want to know all the things which are out of warranty so these are the two assignment question at this point uh I will mention that at the end of the session as well but two assignment questions one find all the products in particular place and find all the products in U uh which are out of warranty uh now what next we want to do is we want to search for a particular name example the moment I type black so it should say black Bas it should show black book it should show Vivo book oh sorry I'm searching for black right so it's blackb black book black table anything which is there on the black table so it should print all the black stuff so how do we do that uh so basically I'm searching with the text right so I just want this line copy and paste and we are searching for searching for a particular product no we are searching for a particular text and next I will say okay so when I say give me all the products which has black in it so will I get one product of will I get list of products of course right list of products so I will say list of product that's what I will get and I will say this is prods I will get all the prods and I will say hey service get uh products with text and I will mention the text here so text is what I want to search based on a word like black uh okay but unfortunately we don't have this uh this word right so what do you think what can be done here okay I can get some yeah good good job so there there's option of substring we can do that uh yeah we can convert it into upper case and lower case okay we'll do that so what I will do is I don't have that method so let me click here in fact you can also click here you can say create method get product with text and you can see we got the method here but at this point the method is not doing anything so basically I want list of products here because that's what I want to return so I'll say products equal to new at the least okay and at the end I want to return this return products okay so let's let's run this and let's see what happens of course I want to print all the values so I will just use this for Loop which is already mentioned here it's not copied copy paste okay this is not copy pting right this is code reuse what we say from prods prods yeah prods okay so basically let me just name this as a product because we're already using p somewhere we are using p here in fact you know what we can do we can also actually comment the entire section here so that we can focus only on with a product with a text okay so will this work let's try if I run this code of course you will not get any data here you know why we got empty is because the list is empty we want to update this list from the all the products we have so we got all the products here from that product I want to fetch few products in fact you know I will just name this as prods here so that we can have different names prods okay so let's add here but how will you add of course we want to use a for Loop here because we want to iterate right uh so what I can do is I can use a for Loop and I want to iterate between the products I can say product p is equal to products so I want to iterate between all the products and find and add only those products in this list which has a black in it it can be with a black in A Name Black in a type black in the uh place right so how do we do that so it's very simple you can simply use a if condition here and you can say p do get name do we have uh how do you do it can I use substring that's one option or if you talk about a string it also has one more method called contains you can see that we got contains because we don't want a string we we just want to check if the text is there if it contains the word okay why is black here it should be uh text whatever text you're searching if that text is here or check in the place so I will say get or maybe type check in a type does type contains this text or does my place contains this text contains the text so basically I'm checking everywhere I'm checking in the name I'm checking in the type I'm checking in the place as well right if it is there I will add this particular product in the prods so prods do add p and of course you might have to rewatch this session right by yourself because when you're just watching on the go it's difficult to remember everything right so yeah before we go ahead hit that like button everyone come on sometime you have to keep YouTube Happy you know YouTube how YouTube promotes the video they basically see the number of likes they see they see the engagement they see the comments uh so do that let make YouTube Happy YouTube will make me happy and that's how it works that's the EOS that's how the ecosystem works why okay i' okay there's a equal to here there should be a colon my bad okay cool uh I think this should work now let's see if this works I will just go back to my main or I can just stay stay here run okay still not working I think the problem is when you say black exactly this text black and if you see the products we don't have any black with small B you can see all our capital capital B Block B here okay that will not work so what we can do is we can actually convert this into a lower case so whatever text we are getting I will simply say string s Str and so if you want to compare two strings it's always better to have in a lowercase so I will say text do to lowercase so and then don't compare with text compare with string because that's a lower case and not just this you have to also convert this G get name I me whatever get whatever name you get here whatever type you get here also into lower text so what you can do is you can create some local variables here for the name in fact not here inside the loop because the value will change every time so I can say string name and this name will be P.G name and then we want a lower case of it so dot to lower case that's how you compare basically then we got string type is equal to P.G uh type to lowercase and again string uh place is equal to p. get place. to lowercase and now instead of comparing with the data which you get from the product just compare with the variables which are already converted into a lower case you got the point right what is happening here and how do you solve that yeah so basically you have to convert it into a lower case or upper case it should be common case let's see if this works now yep it worked can see that we got all the products which has something black you can see the place is black black draw uh here it's also black table uh wcom is in Black drawer Logitech keyword is in black table uh HDMI cable is in Black draw Java black book blackbook then we got black table black drawer and black Bas it worked so we are going to enhance this product more uh in terms of connecting to a database in terms of connecting to making this as a web application and then the way we have built this product we don't have to change much okay and the class this class will be used as it is you know you will see that once we start uh doing this okay what else we can do here so assignments for you let me just write down the assignments on my iPad so that you will remember the assignments uh okay sure not Shing my iPad okay so assignment the first one you have to search by place okay that's one uh next is okay what what are the next thing you have to search by warranty search products out of warranty so the first assignment is search by play second is search product out of warranty next you need to implement a stream API here okay so if you if you see the uh the thing which you have done here let me show you that once again the thing which we have done here searching it with this this is basically uh a normal noral iteration right I want you to convert this into a stream API convert this code into stream API how will you do that so on my channel uh there is a video for stream API watch it you'll understand how to use stream API and Implement that so that's your third assignment so this is the work which you have to complete before the next session yeah and I I I love to see your pose by by saying this is all about learning it's not about MacBook and that's what what that's what we want okay because see in your life if you know if you know something if you master something and if you're consistent you will get thousands of MacBooks it's not just about one okay it's of no use if you get a if you get a Macbook without knowledge and I have learned that in a hard way so basically in my entire career I've never focused on how much money I earn how many gadgets I have which car I drive it's more about can I what what I'm learning every day if I don't learn something every day today I feel I'm not done enough okay so do that okay focus on learning not on the things cool so what's the agenda for today the agenda is very simple uh we are working on a project which is a product uh so basically we are managing the products which I have in my office and for that we were able to add the products in the list and then we were able to fetch it we were able to search it and I have seen your assignments uh you know I don't know I never thought I can make such a bigger impact in just it all started with a small thought of giving giving away then we got a thought hey can we just do a challenge of 10 days and now looking at your response on different platforms is amazing uh every time I open LinkedIn now I get tons of uh notifications and then in the entire day I'm actually spending now two to three hours just checking your messages and uh notifications in uh in LinkedIn and that's quite fun so it was it was an amazing experience uh looking at your uh messages and notifications and one thing you can do if you want to improve your rich on LinkedIn because it's a very good platform to con create networks create connections uh getting a job so what you can do is uh when you're are posting something try to make it more interesting uh maybe by putting an image of what you're doing example I saw one of some of the post uh let's say if you talk about Pascal triangle so having a pascal triangle in your LinkedIn post as a photo will give you a better reach because when people scroll on LinkedIn they get excited looking at the images not the just the post and when you start with your post or the article or not the article but the post on LinkedIn uh the first line is very important that's how you can hook users there or your connections okay so let's talk about what we are going to do today it's very simple basically we are going to focus on a project which we are doing and then we'll convert that project into spring project now we will introduce a framework you know let me give you my experience when I started my programming journey and then when I looked at uh different uh uh Frameworks in fact I started my career with Java then I worked on Spring swing uh tool which is the GUI Tool uh then I moved towards uh web development Sate and jsps and I was very happy but then someone suggested hey you know instead of working on sub JSP should work on STS framework and when the when I when first time when I heard about a framework or a tool I was like I I'm happy you know it's it's all working here why you have to learn a different framework to do the same thing of course the project which you build in without using a framework is same as when you when you build a project using a framework it's almost same right the output will be almost same then why to learn a new framework and do it it's because framework will reduce your work of course of course you have to learn something but with less efforts you will get more and that's what you get from the Frameworks and today after this session I'm sure you're not going to learn spring framework because it's very difficult to teach spring framework in one hour but you will realize the power of spring you know lot of magical things are happening behind the scene and you will love it okay so yeah you're going to learn about the postgis as well which is a database I wanted to go for some uh database like myql but then you might have on my skill I wanted to give you a new flavor and that's why we have P okay so let me share my screen and let's try to understand what is a problem here so this is the this is the this is what we are building here right so if you can see uh we have our code which is the main file uh in which you have basically what we have is we want to add products what now what is a product here so if you talk about a product class it has uh four uh private variables we have name type place and warranty uh example let's say a product can be anything example here if you see we have a product which is Type C cable which is a type of cable uh and place his black drawer this is the warranty I'm not sure if you do really okay we don't get the warranty for uh cables but it's it's just that uh then we got Mac Studio which is a computer on white table and that's a warranty so basically what we did is we created different products and we have added all the products in the list and for that we have used a product service class and then uh in the product service class we have a list of elements here list of products and then we can add the product we can uh get a single product by a name we can get the product by a text we can okay we I have also given you some assignment on warranty and stuff I hope you have done that okay this this is what we have done right and we were able to add the products we were able to fetch the products okay but then what is a problem we we are trying to solve here so before that let's understand the architecture of the project which we already have uh so we have a main file right so this is this is your main file and then uh if you want to add a product basically we have a product service class which is doing all the logical part so from the main basically we are saying add product right but then how exactly product will be added the logic is maintained here so logic is there inside the product service now how to uh get the product so again we are just calling it right from the main we are just calling it and the logic is mentioned in the uh product service and then how do you represent a single product we don't want to use variables and that's why we are uh basically using uh a class called Product now this product class represents an entity for you right now this will have a name and name Place type and warranty and basically in the product service what you have is now since we have multiple products we have to store it somewhere and where we are storing them is in the list so basically you have a list and inside this list you are storing all the products the problem with this is it's a very common problem where uh every time you run this application so whatever application you have here every time you run this basically you are creating a new list and then you are initializing the list with this values and then you are fetching value right I don't want to do this I don't want to uh every time I run the application it should add the list like this ultimately we are working with a code right example if you have a new product basically we have to change the code here we don't want to do that so where do where do you store data because at the moment I close this application I will lose all the data right and to store this data we have to store it somewhere and that's why uh in this particular project what we're going to do is we will be using a database here okay now this is your database and currently we are using postgress uh SQL which is one one of the most amazing open source project open source database okay I think you can you cannot see that that's yeah basically we are continuing with the EXT code so we will be expanding it yeah and this is what I was talking about uh Wades has mentioned an important thing here uh which is code looks difficult uh yesterday but after doing it by myself it seems easy now that's that's important you have to try The Code by yourself because when I type I have a clear picture in my mind because I'm typing it I know what how the flow going but when you watch a video or when you watch it live uh sometime you're not clear what is happening and that's why we have to write The Code by yourself so basically what we are doing is we are creating a database now and we want to store this data in the database so basically whatever list you had here uh this list this list instead of storing it in a least we have to store that in the database directly now we can directly write the code so question arise how do you you write a code for database some we have to use something called J DBC now jdbc stands for Java database connectivity right so we need to use JBC here now the problem is if you want to use jdbc we have to write some steps to work with jdbc and the question is where you will write the steps and that's where we have to use something called a layer so don't don't write your Dy code inside service service is meant for logic processing logic if you want to work with database what you can do is you can create another class here called Product uh product DB now this class is responsible to communicate with the database cool so that's what we are going to implement here I just need to check if everything is proper in my machine just a second everyone yeah basically we have to get a different class right so what I'm going to do is in this code basically uh we are going to write database connectivity database connections and to achieve that uh we have to make some changes now what kind of changes we want here uh the changes are very simple if you want to connect to a database of course first of all we need a database here right now if you want to work with database what you can do is uh first of all we start with jdbc part uh if you go back to okay so in my machine I have installed something called a postgress so you have to install postgress first now how do you install postgress very simple uh so go to browser and search for pogress this is where you can download it and pogress and mql is almost similar the difference is po is being open source now and Myas is a part of Oracle it's also open source I I guess there's one version of Myas which is open source I'm not sure but uh if you totally want to use the the um the open source version I would prefer to go for poquiz and you can see you will also get the source code of it not needed but anyway so you can download the post in your machine and uh once you install it it's very simple to install it you just have to set the password you will get something called uh PG admin so once you install postgis you will get PG admin which is postgis admin and you can use myql if you want in your assignment uh so you have to set the password I have set the password which is 000000 so four time0 and click on okay now this is how your post gist database will look like or post gist admin will look like and in this you will have an option of server so if you expand server okay just asking me for the password enter yeah so this is how your poin will look like I know this looks some fancy tool you know you can see all the dashboard uh the benchmarks what is happening with your system uh and if you can see we have databases here by default you will get this post database test is what I have created uh but you can use post G and if you want to create a database which we are going to use what we can do is we can right click here and we can say create database datase and what database name I want to have here is teliscope so that's my database name if you want to see the SQL query for this you can click on SQL and that's the query so create database telescope uh we don't need all these things but then owner is postgress doesn't matter click on Save and you can see you got your database here so this is your postgress the database which you created here and in this if you look at schemas now this is where your table you'll having your tables uh so I'm assuming that you know the concept of uh SQL database tables okay otherwise we have to take next six months to talk about SQL it's a huge topic but yeah we got we got the database with teliscope and here you can see we have tables and by default it is empty okay I want to create one table here so what you can do is you can right click here and you can click on create table and what table I want so my table should be a product table okay so we want a product table and in this we want to okay so we want a product table and how many columns you want so basically you can just click on the column here and you can create the columns so the First Column which I want is ID so I know we don't have the ID in our remember when we started the when we started talking about our project of product management I've mentioned that your product is actually an entity right so every entity will have one table so let's say in your project you have five entities every entity will have a table right uh so in the table basically you need to have your uh primary key so every table will have a primary key so in this case I'm creting a primary key here see name cannot be a primary key because I can have two laptops with the same name yeah possible right okay so I have ID uh what is a type of it I want the type to be uh integer do we have integer here yeah we want integer of type lens scale doesn't matter this should be a primary key I also want this to be uh to have autogenerated values I don't want to generate value by myself so every time I enter a new product it should give it a new ID 1 2 3 and the way you can do that is I'm using pus after a long time so there should be an option of uh identity generated no identity identity always increment by one start by one and that's it I think it should work let's see the SQL query for this uh generated as always as entity uh identity okay increment one start one cool if I click on Save done so you can see we got table here but I want to check if this this table works what I can do is I can just go to the query tool where I can write the SQL queries so this is a place where you can write SQL query I know I'm going bit fast but you have to rewatch this session this this is not about teaching you uh SQL this is more about how do use how do we build a project right so what I will do is I will first of all say select star from product let's see if you have any product there uh it's empty uh relation product does not exist is it I created this table is it case sensitive from when no something is not working so I'll say use the lisco uh select star from product is okay something is not working what what's wrong I've clicked on this I'm using this database let me just refresh it once and run okay something is weird how about this if I say this belongs to delisco this is not working columns is there okay we got only one column oh I forgot to enter all the different columns here you know what I will do I will just drop this and let's create this table by query so I will say create table product uh the product should have an ID and if you want to have a SQL query you can also mention uh serial okay you have to mention this of type integer serial if you want the autogenerated and it should be a primary key and as dbms changes this also this also changes yeah now table is not Tesco database is Tesco and yeah I'm in the right thing doesn't matter and then we can say name is name is text I think uh po supports text not string or vcad so we got name uh then we got type which is also a text then we got place which is also a text text and then we got what we have one more thing which is warranty yeah so warranty which is of type integer let's run this and let's see what happens okay did I mention the wrong query do don't have to mention integer is it so it says near integer okay yeah it worked okay so we should not mention integer if you mentioning cial because it is uh Cal is also a type okay cool uh we have created this table I will just say now let's say let's let's say if this work select star from uh product I I want this to be empty yeah you can say it is empty now so we got a table and this Auto increment now this will increment automatically I will not even insert this but then I want to do that from the Java code now so if you can see I'm adding a product here but if you see we have we are adding all the products and then in the service we are adding in the list but what we are going to do is we'll not be doing the changes in this particular file the problem is when you are working with a database connections we need to create a different uh we have to add some libraries we need to connect with jdbc right for jdbc we have to add a jar file okay and then you can you can actually do that in the external files you can just go back to external files and can add or EXN libraries you can add a jar file but to better manage the product we can use something called Maven now Maven is a build tool which also helps you to handle the jar files so what I can do is I can right click here I hope there should be option of converting to Maven okay do we have it here refactor now I can't see any option let me know in the chat if you know where to because I have used this on Eclipse because in Eclipse you get this option here no problem what we can do is uh we can simply create a new project and let's say new project okay and then instead of selecting a normal project what we're going to do is we're going to select a MAV project and we'll name this as product demo DB because we're working with with DB database now and then uh everything is as usual so okay there's a question m versus G which is uh which is best or which is better both are good it's just that I'm more comfortable with Maven because it is easier to teach in Gradle there are lot of uh new things and then indentation is one thing so for projects grle is good for teaching MAV is good yeah and for beginners MAV is good Okay click on Create and it will create a m Pro project I will say new window because I want both these windows okay and then in this I'm going to Simply copy the files so if I go back here I want all these files so I can just simply copy and paste over it all and I'm also going to change so packages com do sorry o. example let's stick to that now what you get in the maven project is this if you expand this or if if you go to pom.xml so whatever dependencies you need you just need to mention those dependencies here so what you can do is you can put a thing called dependencies and whatever dependency you want you can just add it here now what dependency we want we want a dependency for uh for this what is uh the connector right now how do you get the connector how do you get the dependency do you have do we have to BU heart it uh not exactly so what you can do is go back to your browser there is a website called mvn repository and in this yeah so in this you just have to search for post GIS SQL jdbc and if you can see we have it here this is the library which we have to use and make sure that you get the recent ones okay don't go for the versions which are way old uh and which one to choose here so I I always go for second last version because all the latest version will always have some issues okay and it also tells you which of the library has VAB abilities and you can see there are certain VAB abilities here this one is safe but we'll go for this one 42 . 5.4 select this one and copy this go back to your project and just paste so what you got there is dependency okay and every time you make the changes depending upon different IDs you will get different options yeah okay here add framework support Maven okay we got the option cool hey done oh it was so fast we don't even need to have a create a project so what you do is you can just click on this right click on the project thank you so much Prine add framework support and select Maven here and you will get this option oh that was cool so now let's go back here specify the dependencies and paste it here okay so good thing is you don't have to create a new project thank you and now the moment you add this what you have to do is you have to refresh your Maven so if you can see there's the option of Maven changes click on this and it will download the jar files for you if you want to see where it is click on the external libraries and you will find a jar file for po cool so this is what we wanted now once you have added that support it's very simple now what you do is instead of adding to our list we are not going to do that now so what I will do is I will just come in this section and in every way we have to make the changes okay let's not comment because our code is dependent on it we are not going to use it so let's say if I want to add a product so what do you do is instead of adding to a list because not we are not going to do that we're not going to add to a list we will add this to a database now how do you connect to a database it's very simple what you do is you simply say I want a database connection so I can say I can say DB dot uh DB dot add a product or maybe I can say save db. save and pass P just by writing this line the data should be stored in the database right will this work of course not right it's not Rajan to know everything we are not sure which database you're using uh what is your database name I mean which dbms you are using what is your database name what is the password to connect with it so this will not work so what we want is we want a extra layer remember in the iPad when I was talking about it we have mentioned about the extra layer this is the layer which we want to create so how do we create that layer it's very simple you can just right click on your project here and create a new class I name this class as product DB okay later and in this class basically you have to create a save method so I want the object of product DB so I will say product DB and I will say this is DB equal to new product DB once I get this object I can simply say db. save but unfortunately we don't have a database here so what I can do is or we don't have a save method so I will click here and say create method save and done we we got this method here and this method is responsible to store data in the database the entire service class should not have any idea about database it will simply say save it I don't care how you do it it is your job prod DB to connect to database and save it now to connect with database you have to learn how do you use ddbc so you will find the desp in the description you will find some videos one is for spring framework one is for longbo which we're going to use and also for jdbc so in this we have to connect to database right and I will not be teaching you database connections jdbc connections here I'm just going to write it so basically for databas connections you need something called a connection object so you have to say connection which is coming from java.sql now this is coming because you have added that jar file of pogress if you don't add this jar file you will not get this so we'll say connection con equal to okay we'll just say equal to null by default and and let's create this object in the Constructor so I will say public product DB Constructor and in the Constructor of this I will simply create the object and the way you do that is by using a driver manager. get connection and in this you have to mention the URL okay now it will take three parameters from you not one it will take three parameters the first parameter which you have to mention is about U the URL of the database the second one is a username and I know the username so this is po that's the username the password for my machine POG is 0 you when you install make sure that you change it and then we have a URL here now this is something you have to remember or byart that's your choice but when you work from a long time you remember that so you say jdbc colon uh postgress SQL colon SL slash not this slash sl/ Local Host because it is installed on your machine colon 5432 which is the port number for postgress okay for different database we have different port number uh I think for MySQL oh I forgot the port of MySQL that's weird so this is the port of a postgress so you you got this detail right we got the URL we got the username and password uh so you might get an exception what you can do is you can click on more actions and you can say surround with try catch because it might throw an exception so one once you got this database connection created let's save the product now to save it what you need to do is you have to write SQL query right so what we need SQL query here so I can say query now what will be my query my query is very simple I want to say insert into product uh with the fields because I don't want to mention the id id should be autogenerated so I want to insert name I want to insert uh type of the product I want to insert the place I want to insert the warranty cool uh and then values we have to insert the values as well now question arise from where you will get the values tell me if I want to insert all this data from where you will get the values so basically the values you will get is from the product this is where you have all the values right because one product will have details about your product it will have a name type place and warranty so what you can do is you can add the values here okay but since we are going to use prepare statement in jdbc now if you want to know more about jdbc you will find the link in description and you can watch the later video later about jdbc but here I will say question mark question mark question mark four question marks for four different values and we can replace that later but how do you execute the query so we have to create something called a prepared statement St equal to con do prepare statement and we just have to pass the query here so whatever query you have created just pass it here now this might also through an exception so what I can do is I can say surround with try catch we are just creating a query now let's execute it so how do we execute it so we can simply say st. execute now what it will do is it will execute the query so let me just go go with the flow again in the main what we are doing is we are saying add product now this product is actually going to product service inside product service we have this add product method which is taking a product it is asking database to save it so this same object goes to product DB and then you are accepting it here and we are using SQL query to send it to dat to the database right uh but the problem is we can't simply say execute because we have not mentioned the values yet it is still question mark we have to we have to change these values now how do we change it quite simple just before the execution you can say St dot each question mark is one parameter right so you can say Set uh set string because the first one is string so the first question mark since we have four question marks we have to replace it the first question mark will be replaced by the name so p. get name the second question mark will be replaced by uh p. get type no yeah type the third question mark set string again the third question mark will be replaced by P do uh get place and last question mark which is set integer because last one is a integer here so it's a set int question number four I mean the question mark 4 this is speed. get warranty so we are able to set all the values and we are saying execute our job is done the question is will this work I'm not sure let's try so what we can do is we can simply comment all other things only focus on adding a product what do you think will it work or did I completed all the steps I'm not sure sometime you get to know things when it gives you errors right uh so let's go go back here say run oh we got an error it says po uh phrase unable to pass the phrase what's wrong what's wrong what's wrong what's wrong just check oh someone also mentioned that you can wrap it where do you have the option of wrapping it in the oh yeah okay this looks good so we can wrap the entire output cool so uh we we forgot to do one more thing we forgot to mention the database name so the database name is telis here okay we have not started with the data the data jpa yet I want you to show you uh the part of data jpa right and that's why I'm doing I'm going step by step here let's try to run this once more time and oh something happened you can see there's no error it says process finished with exit okay let's try let's see how do you verify this let's open our PG admin and let's run run this particular query oh can you see that we got ID which is autogenerated and we got all this data here in the database this looks cool okay now uh once we have entered the data let me commend the entire section because now we have data in the database so let's so let's let's not insert every time now this time I want to fetch all the products this is something I want to execute okay so get all the products uh so here when you say get all products let's not get it from the list question from where you will get it again simple DB dot you can say uh get all this is the method I want so in the database I want to say get all but unfortunately we don't have a method so what you can do is you can say create method get all so we got the method and this should return a list so let me create a list here so I will say list of products and we'll say products equals to new okay so people who are finding it difficult my suggestion would be First Watch the jdbc video and then then also watch the maven video so jdbc video and Maven video then watch the entire session you will love it you will see the power of Frameworks and how everything works okay so basically what we're doing is instead of getting data from a list last time we were getting data from the list right this time we want to fresh data from the database so here we are saying we creating a list and let's fill this list from the values which is coming from database okay how do we do that so just say return product so that you will get rid of errors this time it will return empty let's let me try that let me see if I get the empty here let's run this and you can see we are not getting any data because it's empty the list is empty so how do we fill this it's very simple again use the same thing let's copy this because we are going to query the database paste what are the changes the first change you're going to make is the query we're not going to insert this time we're going to select so we'll say select star uh okay we are not getting star we are going to mention the column name because we don't want ID there so we'll say name type place and warranty fromom table name is product and we don't have to apply well Clause is because we want all the data so basically we are fing everything from database right and and let's execute the query now since we don't have any question mark here you can skip the entire section from here and simply say execute the difference is execute will not return the data we want data right so execute query is something you can use to to get the data and what it returns is the result set so it will return you a result set and what you can do is you can get data from this result set and store it in a list but the problem is it will give you uh d data in the result set format you have to convert that okay cool so basically we want to F data from result set and set it in the products here because that's what we returning here right so how do you add so basically we can run a loop because if you say RS dot there's no method using which you can get all the data and save it in a list we have to do it manually one by one uh there's no option so what I will do is I will use a loop here and F data one by one so you can say fesh dat till the end and every time you fet data you can simply okay what we have to do is we have to create a product object because okay I know we don't have time to explain this now but just go with the flow everyone so what we trying to do is we want to create a list of products right so we will be having multiple products so every time you run this Loop create a product object and it is highly recommended make sure that you watch the video of jdbc and Maven trust me this will be very very easy okay cool so uh let's get the product object and save each value in the product values okay uh so how do we update the value so we have to say p do set name and this is coming from rs. getet string from the First Column so since we are getting multiple columns right this is the First Column second column third column fourth column then we can say p do Set uh type this is coming from second column so I will say get string column 2 p set place you know this is like a beautiful poem from a language which you don't understand right it sounds beautiful but you're not sure what is happening three then P do set warranty and rs. get string or get integer this time it is integer and we are saying four okay so we were able to set the value for the product now once you get all the values just add the product in the product so we say products do add P now let's say you have have 15 values right so in the database now this while loop will run 15 times and every time you will get the new product object you are setting the values which is coming from database and adding to a list the same list goes back to the product service the same list goes back to the main and in the main you are printing all the values let's see if this works run and voila can you see that this time you're getting data from the database is this making sense at least you are getting the gist of it right how things works out so yeah so basically we were able to fetch all the data right now same thing can be done for other things as well uh for the other methods which we have here so you can see we have it for uh get all we we can also do it for the get product name for a particular name we just have to put say we have to change the query here okay cool so let's get back here and what we have to do is so yeah you can also do it for the uh get get a product with a name but that is your assignment okay so write SQL query and convert this into uh uh database I mean convert this so that you will get data from database make sense now I want to show you a real magic okay and that magic is how do we change this project into a spring Boot and a data jpa we'll compare this code from the code which we'll write in the spring boot okay now I don't want to teach you entire spring boot now uh so yeah I don't want to teach you entire uh spring boot but I want to show you the power of spring Boot and what it can do for you okay uh so how do you work with a spring boot project the first thing is if you want to create a project of spring boot you can go to inj and you can say file new when you create a project you have to make sure that you select spring boot but there should be an option in your IDE I think my particular IDE is the ultimate version yeah so I have ultimate version here uh you will not get this options the spring initializer option I guess in the community version so if you want to do if you want to get this project even if you have a community version that's completely fine what you need to do is you need to go to the website and uh he where the other file went yeah so you have to go to the website called start. spring.io okay and here you have to select so you have to select the project type first which is Maven in this case the language it supports is Java cotlin groovy we'll select Java here version whichever is default you can go for 3.1 and then you have to mention your group ID so that represents your company ID basically a unique one so this is com. telescope and then the project name so project name is product demo we'll say product spring you can change this as well uh make the packaging as jar and the version so make sure that whatever version you have in your machine select that and then from here you have to add dependencies now what are the things we need so basically the first thing you need here is um what you need you need a post driver so post SQL driver added it next you need something called a data jpa because I wanted to show you the part of it yeah this is called a spring initialiser so add these two dependencies post G and spring jpa and click on generate that's it your job will be done you just have to unzip this okay so click here unzip it I will just hide this so that I don't share everything uh it's in downloads okay nothing is nothing is risky there okay cool so you can see I have this file here what I will do now is I will go back to my inj I guess I already have the in DB here let's use this window I will open this project which I've downloaded which is there in downloads it's there in product spring and yeah open this project this window Okay cool so you can see we got our spring project here and trust me once you see spring project you will say this is easy very very easy okay okay so if you go back to your pom.xml uh this is what you have you can see we got two dependencies here in fact we have three dependencies uh you got data jpa if you can see you got postgress and you got the testing part testing framework okay cool now in SRC if you go to main you have Java and this is your basically main application whatever code we have in main you will be writing that here expand this I will explain this in some time okay this part so what I'm going to do is here I'm going to uh copy the code which we had here so let's you go back to main and let's not add everything because we already have that in database let's execute this part only this part we are fetching all the products how do I fetch all the products just paste it here the question is will this work uh of course not because we don't have a service class yet so how do we get the service class it's very simple you can just copy this service class and paste it here so you can see we got a service class which is here we also need a product class paste here okay so we got the product we got the service uh service says it is searching for product product DB okay we'll we'll get that and you know what I will do I will remove product DB and in get products let's say okay let's solve this problem later let's solve this errors later and just to keep it more simple I'll remove this part just to make it easy and yeah cool okay uh so the problem is we don't have a product service class the object we don't have the object of product service so let's go back here and say product service let's create the object for it as service equal to new product service okay and then in this we have the object of product we don't need a list as at this point so we can remove the list as well and okay now one thing to remember about spring framework now spring framework provides you one of the most important feature in programming which is the springing the dependency injection now what is dependency injection if you go back to the main method you can see we are creating an object of product service when you say new keyword and when you mention the Constructor basically you as a programmer you are creating an object by yourself is it a bad thing yes is because when you create the object it is your responsibility to destroy it basically you have to manage the life cycle of an object spring framework says you don't have to create the object I will I will do it for you trust me Spring framework says you don't have to create the object I will do it for you let me show you what I'm saying what I will do is I will just comment all the methods here comment and let's create a very simple method so I will say public void show and in this show basically I'm saying in show okay and we all all know that if you want to create if you want to call this show you need to First create the object let me come in this section as well yeah so first you need to create object of product service then only you can call it right example I can just go back here and I can say service do show this will work let me try run and you can see oh it is giving you errors because we have added this dependency of database okay okay okay let me comment this post and data jpa for some time only data jpa commenting should work when I comment it and if I go back to my application and okay what did I just comment it oh yeah if I comment that then we have to get this spring boot dependency okay you know what I will do just trust trust me just trust me because I have to make a lot of changes if you if I want to achieve that because I have to get this spring boot dependency then if I commented uh let just reload the MAV dependencies okay so the thing is you have to say service do so that it will work right but then spring boot says you don't have to create the object you can just remove this and it will still work how step one you have to tell spring framework to create the object for you spring framework will give you the object and for that just on top of your class say add component so what you're doing is you're telling your spring framework to create the object so spring will handle it for you you don't have to handle it uh ignore the XML file at this point if you're finding it confusing and make sure that you do it by yourself then it will make much more sense okay cool uh and then go back to your see the problem is we are doing so many things at the same time in less amount of time it it does create some confusion okay so what I will do here is I will just try to okay so how do I get this object from spring so what you do is when you talk about the first line spring provides you something called Spring container so every time you run this line I mean the moment you run this line it will create a container for you imagine a container as a box where the objects are already created so you don't have to create the object objects are created and if you want to learn everything in detail uh you will find the link in description about this spring video just watch the video you will get the complete idea now this basically gives you a application context object so let's use it so this run returns you the object of application context let's just use it and using that context now since the object is created right so the object of product service is already created by Spring framework and it is available for to use how do you get the object it's very simple you simply say context Dot and you say spring framework give me the object okay so spring framework get bean and mention which class object you want I want the object of product service service do class so you mention which class object you want and spring will give you the object yeah this is called dependency injection and now this should work but the problem is I can't run it because you know there is there are certain properties you have to mention so I will not run it now so get bin will give you the object but this will work trust me okay so what we say what we're saying is we want to get the data right so from the service uh let me just uncommon this part so from the service we are saying get all products which is here okay we are not going to go do add product right which is already there data is already there let's use get all products uh okay so you can see we are saying get all products and this will work only when your application is connected to a database and that to with the help of queries okay and you can see we don't have any file here which is which has a datab connection but let me create a file here because we want it DB right so what I will do is I will just right click here and say create a class and we'll name this as product maybe you can say uh DB or whatever name you you use doesn't matter and in this class you have to mention all the queries but then if you really want to use queries here then why you are using data JP and all these features let me show you something let's not even create a class let's create the interface okay okay and the only thing you will do here is you will extend something called jpa repository now jpa repository is a interface which will help you with the concept of omm now on this channel there's a there's a video on om please watch it hush is not here so you can't add the add that in description but you can search on what is or and watch the video you will get much more idea but ultimately when you see the output for this you will get the the gist of it now repository says tell me which class you're working with so I'm working with product and what is the type of your primary key the primary key is integer so we simply create this particular uh interface here and no configuration okay we we have not done any code here but see the magic and okay now this is the exciting Point people who are still watching just see the magic if I go back to product service and let's create the reference of product DB so DB DB just a reference and DB dot can you see that we got methods just say find all no need for any SQL query no need to do everything which we have done here if I show you the code what we have done in the earlyer project is this if you want to get all you have to line you have to write this number of lines with this no line this is what data jpa is doing for you it's not like behind the scenes there's no SQL query because the only way you can interact with your database is with the help of SQL but this is happening automatically is it making sense okay but the thing is we got the method but unfortunately your your your application has no idea about the configuration nothing because nowhere we are mentioned which dbms you're working with is it postgress is it Mas or is it Oracle we have not even mentioned database name we have not even mentioned username password we have to configure those somewhere now where is that somewhere if you can see there's a resource folder here in the application. properties this is where you mention all the properties now what's the property I'm talking about I'm talking about the URL the URL is for the spring data source. Ur and the UR is same which we have written before which is DBC colon postgress let me just copy it because this is the URL which we want copy and paste it here next thing you have to mention is the username so spring data source username which is pis single s and we also need a password so spring data source password which is 0000 and that's it once you mention this let's see if this works run oh we got errors I thought it will work okay it says there are certain things which are missing what is that missing thing it says error creating a beIN with product DB oh okay the thing is in the product service we are creating a reference for product DB right where is the object where is the object that's the problem so thing is as we mentioned for product service remember we have mentioned component here in the same way in the product DB as well on top of it we have to say this is a repository because we are working with repository here and in product service if if I want see the the thing is just by mentioning this spring will not know that you want this object you have to say hey Spring I want an object of product DB and for that you have to say Auto wire search it and give me the object okay if you want to know more about aut Wire Watch the video which is there in the description about spring framework okay if you want to know how I got this full thing when I just mention user just type user it will give you prompt and it happens with some of the IDS okay and it should work now let's rerun the code okay now we have different error and I love errors see the thing is it says and sometime you learn byas right it says everything is good now I got the object but the problem is this product class is not managed it is not it's still not a be and also there's one more thing which you have to remember when you talk about the product it is not just a simple class right this is trying to map so basically your table and your class is getting mapped and to achieve that you have to say add entity here next you also need to specify the primary key which is the primary key here which is private in ID that's your primary key and the the do to mention that it's a primary key you have to say at ID is it ID yeah so at ID and then we have to also mention that this value is actually autogenerated okay so how do you get this autogenerated uh so ignore this part because this is just something which you can see later you have to say autogenerated uh generated value equal to uh you have to set a strategy if I'm not wrong you have you have to set the strategy and the strategy is generated type uh identity sequin should also work I guess but let's say identity and also we need to get the data Setters for this for ID right click generate get a set for ID you know we are doing so much and so much of thing in one video and you need to rewatch this things are looking good now let's see if this works run and what VOA can you see that you got all the data from the database without writing any SQL query yeah we can also add ID if you want to get the ID but this this should work now what are the things you can do you can also add you can also add the product because if you see if you go back to your product service and let's try to find what are the objects what are the methods we have so DB DB dot you can see we have find all we also have save we also got uh count how many column how many rows you have you can also delete if you want you can also say find one we have so many options there is it making sense and just imagine just by using this your productivity will go on the next level and that's why in the industry spring framework is so famous so let me explain the entire flow once again if you want to create this project the first thing you do is you go to Spring uh start. spring.io mention all these properties here which is uh mavin Java whatever springboard version you want to use make sure that you don't use Snapshot because this creates some issues uh and then mention the group ID mention the project name and add these two dependencies the post quiz SQL driver and spring data jpa next you need to uh copy the files which you have already created Service uh let's start with the XML file first in the XML file make sure that you have these two dependencies or three dependencies at least these two is these two are very important for us oh I got tired in not only next in the uh product service you have to make sure that this is component because I want spring to uh give the project yeah sorry uh I want spring framework to give me the object of product service and uh I can use something called Product DB which is a interface not a class and it is extending with jpa repository and with this you can perform the operations the cut operations with the uh with this particular interface you can fresh data you can save data you can you can update data you can delete data and we are able to do that but then you have to also set some properties which is application. properties which we have done here so go with the video once again uh I will try to share this code today itself hey yeah you can also use service here for different layers basically we use different annotations doesn't matter it works so all the uh entity class or normal classes you can have component entity class will have entity and uh service class can have service so got the idea what is spring data jpa so all these operations where you don't have to add SQL query is happening with the help of spring data jpa so before spring we used to do that with the help of hibernate and if you want to learn about hibernate there's a video on hibernate as well and know if you check out the channel there are so many videos in last 5 to six years sometime even I forgot what videos I have your code will be available so after after some time check out the description for this particular live session uh you will find the code repo as a title and then there will be a GitHub link and now today we are going to build the same project which we have done before so that we can it it will make much more sense okay what we are doing but this time we are going to use uh MVC okay so basically we are converting our project into a web project okay that will be cool right so let's get started and let me show you the code what we have done till now and let's talk about the code and then we'll also see what we're going to do today in this particular uh in the image okay so if you if I want to go through one by one uh basically Bally we have this number of classes here let me just move it here yeah uh and also move it this side so basically we we have a product class it's all about this class which is product class and in this we have used four variables basically and for those four variables of course we need to get gets and Setters in fact um before this session around 600 p.m. or 5:00 p.m. IST I have uploaded a video a shot on YouTube which talks about uh the longbo how do you reduce this code okay so if you have not checked it out do check it out later so we'll we'll also convert this into a longb but we'll see later how do we do that uh so this is the class which you want to work with so every product in my office basically it's a that's a product right even if you talk about this remote that is a product now what I want to achieve with this is with this product basically I want to store I want to have a place where I can check where a product is actually is example there are some products on this St C in fact lot of products because I'm going live I have a stream deck for the switching between uh the screens I have a Mac Studio for the streaming I have my personal laptop here uh to check everything is going well I have iPad where I write something and I don't know why this light is sitting here I also have a light here that's huge okay so it gives you a good light on the face uh so basically uh we have so many items here right so sometime we keep this item somewhere and we we lose it right so we have to search where that particular item is and for that we have this uh database or this application which stores where it is and that's why you can see we have a name typ place and warranty because I also want to know if the current product is still in warranty or not now to manage it uh what if you want to add a product what if you want to remove a product what if you are not remove we have not done that what if you want to add a product what if you want to search something what the we the product particular product is and also I want to know uh in a particular place how many products I have what are the products we have and if you want to put that logic we can do that in here so we have this product service class and the reason I have created different classes here of course you can write everything in one class because that's what we do uh when we start learning coding right uh in fact I remember when I used to start when I started uh my programming journey I used to write everything in uh in one particular file it's good it works the problem is if you want to change something and you're not sure what you're changing right and that's why creating different layers makes much more sense so what layers we have so basically this is my main do Java this is where I execute something so if you want to call something and if you want to do all the execution stuff which calling methods and stuff you will do that in the main class of course you can name this class anything you want but since this is a console based application which we already built we have this main method here and in this basically we are getting all the products now how we are getting the products that's not the concern of meain it's just it just says I want the product give me the products uh same goes for other methods as well if you want to search a particular product uh if you want to search for a place if you want to add the products so basically we have the we have the calling here the actual logic goes here so you can see we have a product service here this is where the actual l logic goes okay but now since we want to store this data in the database right see not every application with have a database and if you're using a database of course you can write your database connectivity code so doesn't matter which language you work with which database you work with we have to connect to a database right we have to follow certain certain steps in the same way here if you want to write your database connections don't do it in service class service class simply says this is where your logic will be if you want to process something uh example let's say if you want to add two numbers right so from a main method you will say hey add method take this two values which is two and five just add it the actual calculation will be happening in the service so all the processing will be done in the service but what if you want to get something from database and that's why we have one more layer here now this particular layer actually communicates with your database right uh in fact in the earlier session we were able to implement uh okay I think I have done different thing yeah this was the project right because we have also implemented spring let's talk about this one uh but things remain same right if you if you go back to your uh previous Cod this is day two which I'm talking about U so you can see we are writing all the SQL queries now what we have done in the previous session so basically we are doing a quick revision of what we have done uh but if you if you go back to our previous session we tried to reduce the code right we tried to say let's use certain tools and some Frameworks so that we we don't have to do everything by ourself now my question to you is is it a good idea to use Frameworks to reduce the code what do you think you know some of the people I know they say Hey you know uh we as a programmer we should write everything by ourself right what do you think that's a good idea whatever the application does don't Tak help don't take help from anything else just build it by yourself okay let me talk about the first problem which is if you try to do everything by yourself there are a lot of things you have to do by yourself building your own OS because ultimately your application runs on the OS uh building your own runtime that means your jvm and J uh building uh I mean not using any Library example here for database connection as well we have imported some packages don't do that do it by everything by yourself right and we can't do that right we we want to complete the project in a month or two or maybe in a year if it just complex project if you try to do everything by yourself it will take months even to write a simple database connection the second reason is uh before I talk about the second reason I have a question for you and just complete the sentence I have used this multiple times so even if you know it you can answer that or if you don't know about it think about it it's a funny thing not a uh serious issue or serious thing so my question is if debugging is removing bugs okay that's what we say right debugging is removing bugs then what is coding if debugging is removing bugs then coding is adding bugs so what I'm saying is every time time you write one line of code that is a potential bug let me show you this line can be a potential bug this line can be a potential bug it will not it may not come up when you run the application but may based on the different uh input from the user it might behave differently and that's why we do testing right so every time you write your application it's important for you to test your application that's very important so one way to reduce the testing and uh one way to reduce the potential works is don't write the code write minimum lines of code what if the spr the framework which you working with says hey you know you don't have to write all these lines I will give it to you behind the scene the good thing is all this particular uh Frameworks they are well tested and they are getting used in the industry from a long time so at least you can trust them that they are not creating issues okay I'm not saying it is 100% safe that there have been some ISS some uh cases where you have used certain libraries and they found some issues with the library and because of which the Hat attack happened uh you might have heard about log 4J in fact in the spring framework also there was some I forgot the library but if anyone knows let me know in the chat uh it was having some issues and again it's not like the issue stays it gets rectified but uh it happens I'm not saying it is 100% safe but try to write less amount of code okay cool uh so this is what we have done in in the database but then in the yesterday session we have seen something called a spring framework Now using spring framework spring provides you something called a dependency injection which means you don't have to create the object you simply have to use the object uh example if I want the object of product service in the previous example uh on day two basically we were able to create the object with the help of new keyword but the moment you use a new keyword that means you are handling the object object you creating object you are doing everything with it so in in uh spring what we doing is we are not creating object by yourself we are simply saying there's a spring container so spring Frameworks actually create a container for you imagine a container as a box which has lot of different objects and you simply Say Hey I want that object example let's say if you want a phone of course you will not manufacture the phone by yourself you will simply go to a shop and buy the phone in the same way spring conna says I have all the objects which you need just ask for it it okay and that's what we doing we saying hey Spring get me a bean get me the object basically the objects in Spring containers are called Bean of course it has more logic to it but let's not talk about the logic at this point imagine being as an object okay and then we are getting the object of this particular class which is uh product service and then you got the object and you can use the object for your purpose right and we able to call the methods now the beauty is there's nothing Chang change in the service right you can see we have all the same methods of we have commented it but it should it should work and uh product service needs the object of product DB right of course if you have a class which is calling another class methods you need object for it and you don't have to say new keyword what you can simply do is all the classes or all the interfaces which is repository basically uh what you can do is you can write a annotation now let's not discuss the difference between repository service component again it will take another two hours just to understand it but you know just a gist basically spring will say okay the moment you mention repository the moment you mention uh service the moment you mentioned component where we have used it uh entity and you can also use component what spring will do is every class which has this annotation on top of it it will create object for you and you you don't have to get object by yourself spring will give it to you and you simply say give me the object and one way to do that is by using autowired now how exactly it is searches let's not talk about those things okay but then it will it will give it to you now what we are going to do today is we will be building we will be converting this project into a web project now today's session might be B shorter because we don't have much to do uh we just have to convert this Pro into web so let's do that okay so let's get back to our drawing board so basically what we're trying to do is first of all why do we build a web project is because when it comes to standard on Project what we have done it only works on my machine if I want to use the same thing on someone else's machine basically I have to build I have to take the entire project give it to them and they will run it example in our office we have four working machines and if only one machine has the software they will not be able to access it and also where will be database database is is in this machine then we have to connect and all the stuff so what we can do is instead of building a standard on Project we can build uh a web project where anyone can use it not just us we can we can use it the entire world can use it you can also use it uh maybe we can create a account repository you can have your own account and you can manage your own products uh we can do that and uh okay that's one so if you talk about client you know we are poor people we have a client we have a laptop here so basically from this laptop I want to access this database see ultimately it's all about database right as a user you're not concerned about uh I mean you you are concerned about the data you want data right so how a user will be connected to a database so basically a user uh I mean of course if a user wants to send a request database only works with one language which is SQL if you talk about the langu uh database like dbms so when you talk about D dbms we have uh our dbms specifically we use SQL language example we are using poiz here right now when we use poiz so that is rdbms you need to use SQL now from a user of course as a imagine a normal user a user will not be searching something online uh not be searching something by using SQL query I mean ask your parents or ask your friends who are non from non it and say hey if you want to check your profile on Instagram just say select uh star from Instagram database where the username is this of course as a normal user we don't do that right U so what you get is a UI so basically a client will get a UI here so this is where the UI will be and this UI will send the request to the server of course a server can be on the same database can be on the same machine or database can be on some other machine doesn't matter but let's say at this point we have on the same machine now in this machine we have to first create something here so a user will send a request right a user will send a request and what a user will get is response now specifically if we talk about a protocol which we use to send and accept the request we use something called HTTP right so we have to use HTTP protocol to send and receive the request now this particular box here this is responsible to accept the request from the client so whatever client's request it will say okay I got this request let me see if I can fulfill that or not okay and then based on that it will send the response as well yeah so based on that based on that it will send a response as well now this type of layer in between we call them as controllers now what is a job of a controller just to accept the request from the client and send the response let me repeat the only job of a controller here is to accept the request from a client and send the response now whatever extra you want to do example let's say if you want want to process something client says Hey controller I want to add two numbers now question arise where you will add those two numbers will you add that in a controller or somewhere else so never remember never do any calculations in the controller controller job is to just to accept the request and send the response now after controller you will be having one more layer here which is your service layer now this layer is responsible to perform the operations example let's say client says hey uh server I want to add two numbers okay server says give me the request uh I mean of course I will work only when you send the request the controller this particular controller here will accept the request and then it will say oh you are basically trying to add two numbers let me take help from the service now it is service responsibility to add those two numbers and then of course service can send the response to the controller and controller will send the response to the client if that makes sense but what if client says Hey I want to know some data which which is there in the database example let's say uh you want to fetch a product which is in the database now question arise who will do it is it your service who will do it or someone else so basically we have one more layer here which is your data layer in fact uh yeah we can say data layer it has multiple names to it but just to keep it simple we have a database layer or we can say database layer uh data layer doesn't matter and then this request goes to service will request to a datab database layer and database will send the response now this is how you basically have multiple layers in the web application a client sends a request request go to the controller controller sends a request to the service service send a request to the data data will have the SQL queries which will send request to the database fresh the data send it to service send it to service service will send it to controller controller will send it to the client okay uh now the beauty is uh a client or or a controller can send the data to the client right but how the data should look like of course the data should be fancy right it should not be a simple uh data you want to see data on a proper layout and all the stuff so what you can do is you can send a HTML page so basically what a clients receive HTML but nowadays when you talk about clients it's not just a web page it's not just a web browser right in the earlier days we used to only use web browsers but now we have multiple devices example you can have a phone uh you can have uh what else we can have any anyway let's say we have phone as well now a client here wants to access data from a phone now if you open any application a application we already have a layout right because see in a browser when you see a page you don't have a layout here you simply have a blank page whatever it has to show should be coming from the server but on the mobile phone when you talk about any application doesn't matter is it Android iOS or any type of mobile phone you have a layout specified what changes is data example let's say if you go to Instagram and when you scroll basically you have those boxes fixed the data changes right so we already have a layout here now from the server side do you need a HTML page or do you just want a data of course right you just want a data now question arise what should be the format of this data normally a data can come into different formats in the earlier days we used to be we used to use XML but now the famous one is you can also write in the chat the famous one is Json which is Javascript object notation so whatever data you get from the controller will be coming in the Json format now the same Json format will be uh will be showed on the Android of course the layout is fixed data is coming it will show the data and even if you're using a a browser what you can do is instead of sending an HTML file what you can do is you can send a react page a single page application and then the react will already have a layout every time you do something it will send a request to the server and get the data and that's why if you see on the front end part react is very famous right and mobile you can also use Android or iOS applications I hope this makes sense everyone okay uh so now what we want to do is we basically want to create this controller now how do we do it and since we are creating a web project we also need a server so we need a server where you can run this now different languages have different servers example if you want to work with PHP uh I think PHP uses a Zam server right uh we also have vam server Zam server there are entire shop of servers for PHP uh then if when it comes to okay what other the language JavaScript so if you if you are using a JavaScript in the backend uh basically you need a node there nodejs and that's why if you want to build a back end application you use node there and uh if you are using Java basically if you are using uh if you want to use a web server one of the most famous one is your uh Tomcat okay so basically if you want to use the entire thing you need to install Tom Cat in your machine okay so that's what we we are trying to build here this is this this is the entire thing we're going to do now in this particular session we will not be talking about the front end okay so this section here this section here is not something we are concerned about we just want to focus on the backend part does it make sense we are focusing only on this but now question arise even if you build this how will you use it if you don't have a front end how will you use it so what we can do is see see the thing is every time you have a server who says you can send a request and you can get the response even if a server provides 10 features so let's say a server says I will provide you 10 features or a server has a 10 feature but server only wants to provide five features to the public how do we do it how do we provide the service to the outside world let me repeat my question if a server has so many features or so many services how it will expose those services to the outside world and that's why we have this concept called API so basically from the server side you have to expose some apis does it make sense everyone so we basically have to uh build this API and uh expose it now question if you don't have a front end how will you expose this so that's why what we can do is instead of using this front end here we can use a API tool a tool where you can send request to the API and one of the most most famous API tool is Postman there are multiple options but then Postman is very famous so let's use it cool let's now it's time to get started with the project okay let's re-watch the session then if this complex yeah you have to watch it if you if this is the first time you're seeing this it's very complex uh so do rewatch it okay let's get back to the code and what we're going to do here is we will build a web application right and of course we can reuse the code here and what I want to do is I just want to close the earlier project so that we'll have less windows open we already have so many things going on and uh stuff I hope you're also practicing what we are doing in the session because just watching a session will not make any sense uh you will forgot this concept in a day or two so do practice this yeah behind the scene behind the scene even if you're using a spring framework behind the scene this are your suets controllers are your cets uh if you want to go detail of spring mvy I will I will I will make sure that there's a link in description you will get entire idea how this controllers are created uh because see what we are going to see today is spring boot okay and spring boot actually makes your work very very easy but the thing is if you really want to understand what is happening behind the scene that's why you have to understand how spring framework works so spring boot is actually a layer on top of spring framework to make your work easy so what I will do is I will create a spring web project now how do we do that just go to the browser and the same website which we have went yesterday which is spring initialiser select Maven select Java all the same settings which we have done uh project name here product where spring web because we are building a web project now and jar file v file okay now the difference between jar and war is in V basically you uh you deploy the project on a tomcat server so basically the place where you are deploying it should have a tomcat server jar what it does is it embeds the Tomcat server inside the project that means if you have a project ready you can just run it because Tom CAD is there already uh okay now what about dependencies we need Post gisp in this particular uh thing as well we also need data jpa because that's how we going to connect with database apart from this we need one more thing which is web okay so we need a spring web what else we need let me just check am I missing something here I think spring web is what we need uh we can also add lombok here because I want to reduce this code let me see if I have lombok I've never tried lombok in this yeah we have Lok here cool now once you have all these things click on generate your project will be ready to use let's unzip it done and let's open this project so go back to intell idea file uh open uh downloads spring product spring web click on open uh new window because we want to use the earlier window as well and voila you got your spring web project ready okay now as I mentioned the Tomcat is already present in this particular project if I go back to if I go to external external libraries and if you search here Tom Cat will be already present where are you Tom cat oh don't tell me they are giving you glassfish now oh yeah it's here so you can see we got Tom Kat I don't know why I was not able to see that properly oh he it here uh okay says can I also win this MacBook I watch all your live stream for sure hes uh but I think you already got one from Apple yeah H H says it so you already have a Mac Mac Studio so you can see Tom is already already here so what I will do is I will just minimize this and let's create a project now or we already have a project let's expand it now if you check out the pom.xml now this is where you have all the dependencies right uh so we got dat jpa we got spring web we got postgress we got the lombok as well to reduce the code and apart from this if you go back to SRC and if you see the files we already have one file here which is the main file and if you expand this no fancy code and you can see we got okay we have one more file here which is application properties which we'll see later but we got the project right now if you are coming from the old world of java let's say if you are talking about suret or spring framework normal spring MVC we need to do lot of configuration even before you run the project let me run this project what I will do is even before running the project let me go back to my browser and here let me search for Local Host colon 880 enter and you can see it says this site cannot be reached because the server is not running so if you want to access your Tomcat with a default port number now this is a default port number for Tomcat and you can see this is not running what I will do is I will run this application without installing any tomcat and stuff because Tomcat is there embedded if IUN this code okay oh the same problem I don't know why I add the data jpa because the the the thing is springboard is very smart now since we have added data jpa it is searching for the database connections you know I just to keep it happy what I will do is I will just get those connections which we have done yesterday all these connections copy and paste in the application. properties which is empty in this case at least let's keep our uh data jpa happy right we have done that and now let's run this once again let's see what happens run and you can see it is running the Tom Cat started on port 8080 so just by downloading the project you are able to run it no external dependencies that's fun right uh let's go back here refresh can you see that yeah we got the error because that's our life but at least you got the error from the Tomcat it says the homepage which you're searching for is not there because we are accepting the request but then the request which which is coming from the client we not able to process it because there's no mapping there right okay so what we'll do is uh let's do the mapping now question arise where to do the do the mapping so I will just just go back here and create a controller remember in this we have talked about a controller so we need to get this controller here so I will say new and let's create a class and let's name this as a product oh I'm not sharing my screen let me do that again what I've done so right click on the package say new and say Java class and click on product controller so we got a product controller here now how do you make this a controller this is a normal class right how do you make this as a controller we have to give give it some extra power how do you give that to extra power how do you make this a controller just by having a controller in your name will not make you a controller so how do you make this a controller it's very simple you simply add something called a rest controller now question arise controller makes sense why it is called rest see there's a concept of rest API now since we are uh yeah now since we are using uh HTTP request if you want to do anything on the server side if you want to uh get the data if you want to save data I mean send data if you want to update data if you want to uh delete data what you can do is you can use something called rest which stands for repres representational State transfer whatever communication happening happening between client and a server it's all about transferring the states right sending the values and that's why we say a rest controller to know more about rest controller I will put a a video in description this will make much more sense then so you can see we got a r controller and now this is a controller it is ready to accept the request but the thing is if you talk about a web application you have multiple pages right uh so you have multiple pages and every page will have a different address if you see if You observe the address bar your uh address bar changes for different request in the same way for this particular application as well you have to specify what you want to do uh do do you want to uh get the products do you want to add a product do you want to update a product do you want all the products do you want a product based on a particular uh name for that you have to provide multiple mappings example if I go back to my browser and if I say okay from this page not the courses I want all the products I can simply say Local Host colon 880 slash products the moment I enter it's not working because there's no mapping happening so what I will do is for this particular request for this particular request what I can do is I can create a method so if someone is calling products I have to call this method now this method returns a list of products right because you're asking for all the products so list of products uh we have to okay we don't have that class yet so I will say get products or I can say get all products because we are fetching all the products that's it the thing is we don't have this class product here can you see that we don't have this class so what to do okay so basically uh we have a class here which is a product and we don't have a class so what we'll do is we'll go back to earlier project and copy this class product okay not here here let's space this class so we got product now there's no issue we have to import the package for list okay and then here we can simply return okay what do we return oh I don't know how to F this value it's because controller will not do all those things right controller needs helps from another layer with what is that layer yeah that's right we want these two classes service and dat database copy this and paste because we need these two layers as well the service layer and the DB layer so what you can do is you can create an object of a product service so you can say product service I will say service and should I say new keyword no right I can simply inject it so the way you can inject that is with the help of Auto wired and we got it we got the service class and now with this service you will say service dot get all products job done you simply converted your normal project to a web project voila this is your web project now but will this work that's the real question let's try let's restart the server and there is no error you can see go back to the browser refresh oh it's not working it is not working what's wrong what's wrong there my friend what's wrong the thing is we created this method but nowh there's a mapping right of course a controller can have multiple methods different methods should execute for different requests so we have to mention in English hey this is for products request simple right no but that's a comment right how jav will understand that uh so what we can do is we can say add it to do that we have to say get mapping and in this bracket you have to mention what is your mapping so this will be for products SL products so whenever you get request for products this is what you want to execute save go back okay we have to restart the server restart no issue refresh wo we got it is this making sense okay how to copy files is very simple just go back to the earlier project and just copy the files right click copy and paste in your recent project now whatever you're building in fact I'm not following a proper standards here you know the controller should go to controllers package uh the DB goes to the repo package uh service goes to service package product goes to model package and that's why we say MVC right which is model view controller so the data which is passing between all these layers are called models right product is a model here yeah springboard is actually interesting cool cool cool you know why I'm not creating those packages because the project is already so complex that's a good practice you should always do that but yeah I'm teaching you the concept so it works for me okay now what about uh I want to send the data okay I want to create new entry in the database how do we do that how do we send the new recorder that's a question now let's say if you have a form on your mobile application or a web page you can enter data and you can say send but I don't have a UI here right how will I send data from here and what should be the request now the request can be I want to send a product not products because when you are fetching it you are fetching the products right but what if you want to send a product one how do we do that in fact you know before that I just want to show you one more thing what if you want to fetch one particular product not multiple uh so let's say if I want to fetch okay all let's say I want to fetch Black based okay so what I can do is in the request I can say hey I don't want all the products I want one product that's why product slash I want Black based enter this is not working how to make it work it's very simple you can add one more uh mapping copy and paste and here instead of saying get all products I can say get product and it is not returning a list it should return a product simple product and the mapping will be product slash Black based right and then you cannot return all the products you have to return one product so you can say service. get okay but the problem is I think we have commented it in the service class so if I go back to Service uh you can see we have commented this and the thing is this should be coming from the database not from uh list because we have not coded this in the last session let's me let me code it fast so I can simply say return DB dot uh find by okay if you have to find by name uh name right because you can use find but then it will work with uh the primary key you can see we got find by ID uh if you want to do find by we do have find by but will this work oh we have to pass the query function as well so what I can do is uh spring J data jpa in fact data jpa provides you one more thing you can go to database and if you want to fetch from particular column not from the primary key uh what you can do is you can say return a product and find by name so whatever column name you have so say find by this is fixed what will change is the column name so find my name and whatever name you get here string name and the beauty is you don't even have to Define this okay you don't even have to Define this okay where do you find the the last session uh go to the channel and there's a question where do you find the last question let me just do that so go to the channel and uh search for a playlist tab so in the playlist there's a 10day chall challenge you can follow the videos there okay just add this method go back to your service and say DB do find by name and whatever your name is no need to Define no need to write any uh SQL query or something now go back to the controller and say get product by name which is uh okay where's the name let me just try with uh black beast I'm hardcoding it at this point let's see if this works blackbeast restart the server of course you can also install Dev tools which will help you to directly restart the server every time you make a changes okay let's refresh this oh it works you can see we got Black based but the thing is it will only work for Black based if you if you try to use some other name it will not work you know why is because we have hardcoded the values we are saying black beas black Bas at both the places so what you can do is instead of saying black black beast you can put that in curly brackets and say name so whatever data you passing from there is saying this is so much easier than day three content okay see this is easy because we have done this in day three if could have skipped the day three this will this is like next level complex because we have not even talked about this layers here so we have done the difficult part in the last session okay so you can see so whatever data you're getting you will save that in this particular name in The Curly brackets and then you can accept it in here so you can say name so whatever name is coming here you can accept it here okay let's try and the same variable can be used here now instead of saying black Bas because see it doesn't matter what you search it will always give you Black based we don't want it we don't we want to so whatever user says here whatever user says here that's what you want to accept so restart and refresh oh it's not working you can see it's not working what went wrong I was expecting error but there's no error give me something okay so let me tell you what the what a problem is the thing is you have to use one annotation here which is path variable if you want to use this so by path variable what you're saying is in the path whatever variable you have just try to map it so you can see name name try to map it that was missing there and now refresh it works you can use it for any other name uh in fact I remember we have Asus Rog right that's one machine but the thing is since we have a space between two words uh on the internet if you want to use web address web URL you have to use percentage 20 which is a code for space just say enter and you can see we got Asus Rog is it making sense and the format which you can see here is dis format okay Patrick sounds more like uh giloy from Silicon Valley okay so let me go back to my code again and now let's do it for the adding a product because we were able to fetch products what if you want to add a product okay so if you want to do that what you can do is uh you can create public and since we are adding it we can use void because we don't want anything in return normally you know a good practice is to to return the status code uh HTTP has different status code example 4004 is for error related to resources 500 is the other related to the servers uh 200 is okay everything is going well so every time you perform some operation you have to return the status code as well at in this particular session we are not doing it because again it would be very complex okay so public void uh add product and now question arise what what should be a part of this parenthesis or sorry the round brackets if you are passing a single value you will accept that in a string now you are passing an object from the client side how will you do it so since we are sending data from a client we have to send the entire object of a product because that's what we are adding so we have to accept that here so we have to say product P so we are accepting it the entire product we are accepting it now once you accept it uh we can simply say service dot add okay we don't have ADD method okay we have to implement so many things now uh in this service this is the add method okay product and t. save okay this should work let's go back to the controller say add product pass p and your job is done but how do you map it now question arise what type of request you are sending so basically whenever we talk about HTTP we have different protocols there or different methods there in HTTP so if you want to fetch data we use get and when you want to save data or when you want to send data we use post okay so we have to use post mapping here not get mapping so let me repeat when you want to get data we use when you want to fresh data we use get when you want to send data we use post and what should be the URL I want to be consistent with a product we'll say/ product now you will be saying don't you think the this one and this one matches this product and this product they are matching but then look at the mapping as well we got get mapping we got post mapping here okay of course this is also different but even if they are same we got two different types of mapping there one is get and second is post mapping now people who are coming from this s background they know what I'm talking about right do get do Post okay so we got this right but the question is will this work let's go back to our browser and let's send the data oh but how do I send data from here okay because see browser supports fetching of course you can use some tools internally to do that but if you want to send data let's use a real tool which test which will use to test the apis and that's why we have Postman now Postman is specialized in uh request sending and response what I will do is I will start with the products so let's try this first this address in the postman this is how Postman looks like if you want to download Postman you can go to post man.com and you can download depending upon your OS so go back to postman and send set a get request get request for this particular URL which is Local Host colon 880 products and says send and you can see you will get all your data here and this is a status code you can you got there but if you you can customize it from the server side uh let's try it for product/ blackbeast send and you can see we only got black beast but now I want to send request I want to send a post request so you will go to post the URL is this which is Local Host colon 8/ product but now this time we have to send data as well how do you send data so what you can do is you can just copy this now if you want to send data you will do that in a body so in the HTTP body you do that and replace this with the and let's change values so we already have till 15 let's add 16 one of the item on my table is uh a pen pen is of type pen it's on white table expiry but never expires let's say 9999 Okay so Yeah so basically you have to use yeah we can also use headers actually yeah but then let's let's use body uh you can set the raw and you can set the data like this you can set the format what you're sending you're sending Json and say okay the pro the problem is uh yeah it should work we can't we don't have to set the value right for ID let's try let's try says send okay it says product not found that's weird here we forgot to rest the server is it yeah ID is actually autogenerated so we don't have to pass the ID but let's say even if you're passing it no harm run something happened you can see there's no error you got 200 let's send the get request again for the products send you can see we got all the products if you go down oh oh it's not working it's not working you can see the data is not assigned it's I mean we were able to send the request data is also saved in the database but not the data which we have sent okay it was not able to accept data that's the problem so we have to use one more anation here which is request body so you have to mention hey I'm sending something from the body accept it okay uh so we have to explain everything to Spring framework okay let's try now let's send this data again because this time it will use another ID run oh sorry we have to send a post request in fact where is my history here I'm using Postman for a long time yeah here so let me use a post request with the same body send nothing went wrong here let me use the get request send go back yeah can you see that we got it so we got pen a white table and everything is this making sense oh that's great okay now uh I want to show you one more thing about lombok right how do we how do we integrate Lok here okay and now it's time for lombok let's go to lombok Lok how do we integrate that we have already added the longbo part in the uh in the XML and where do we want to use it I want to use it for the product if you look at the product class there are so many things here right what you can do is for this particular variables I don't want to create gets sets you can skip the entire part from here even two string you can say I don't want to write it it it makes your code so lengthy right uh so what you can do is you can just go on top of your class and say data and that's it it will autogenerate the geta status for you so basically lombok is used to reduce the number of lines of code you write in a class welcome back alens my name is ready and finally the Java project is here that's right I know you waiting for this from a long time uh so basically after watching Code Java after watching spring framework there was one question right what next how do we build a project and that's where I have a small project for you so basically we'll do this project together using spring Boot and we are going to use a database as well for data storage right and for this project we are going to use mongod Deb now why spring boot why mongodb will discuss that bit later and also we need a front end right and for front end we are going to use react now basically basically in this project we are not going to build reite application let's say we already have the UI ready now we have to work on the back end part so the backend part will be done with the help of spring Boot and the database will be mongodb even before looking at the project there is one particular question in your mind and that is is it a good time to watch this video maybe you don't know spring boot maybe you don't know mongod DB how will you understand this project so I am making this video considering two situation the first one is what if you know spring Boot and Mong DB the basics you are at the right place is because you understand how will you use your knowledge of spring boat and build an application and also if you know mongodb Basics you will know how to use the mongodb cloud which is atlas now what if you don't know this Technologies and that's the beauty now I'm making sure that when I'm explaining you the concepts at least you will understand the flow of a project you will understand why I'm doing certain things and why exactly that line of code but yeah if you are if you want to do it by yourself after watching the videos that's where you will get stuck but of course there's different way of learning stuff sometime you have to learn from the basics and sometime you have to come from top to bottom okay this is what you can do in the project and now let me learn the basics so doesn't matter at what stage you are you can enjoy these sessions now let me talk about the project which we're going to build so the project is actually very simple this is a project basically we are building a platform for someone to get a job and what if someone want to hire someone so basically this portal or this website is good for both the employer and for the job Seeker let's say you are a jobseeker and you are looking for a job I know this is a very basic UI but again as I mentioned right the idea of this video is not to understand the UI part the main thing is the back end so I I wanted some UI so we have it here and you will find this entire project the UI the back end in the description so you will get a GitHub link and you you can access the project and you can run on your machine okay so you don't have to type everything but by yourself don't worry okay so let me click on this get job now so basically I'm looking for a job and these are all the job description which I have so these people are looking for candidates who knows so let's say if I talk about the first one they're looking for developer and basically they're looking for software engineer okay I've just typed the description the way I want it doesn't matter you know the grammar is not may not be perfect the technology might not match we're just here to learn right so let's go with that so we got some description looking for software engineer who can work with spring Boot and mongodb and react okay you are watching the right project then uh the experience required is one year it should be year but then that's fine uh these are the skill set required in terms of Technology uh then we have the same profile let's say if you go scroll down we also have Junior data scientist uh spelling mistake here but you can ignore uh so data scientist who has end to endend project experience that's great 12 years I'm not even sure if data science is that old but anyway it's my data I can do whatever I want with that okay so skill set required Python numai and those stuff okay so this looks cool right and also we have an requirement for editor who knows Final Cut Pro and who can use my Mac Studio uh the actual editor is not using it so I'm looking for someone else okay doesn't matter so let's say these are the profiles we have here and now let's say if I have lot of job requirements let's say 1,000 or 10,000 how do I search for my specific requirement of course you can use a search box here so in the search box of course you can search for a particular profile let's say I want to search for developer so I can type developer here okay and it should give me the results so it is giving me all the results of developers you can see developer developer developer developer here but what if I'm not sure what profile I need but I'm sure that I know something example let's say I know I know threads so I can simply type I know threads and you can see it will find a profile where the skill set is threads okay so basically we are also able to search and that's exciting right because you're implementing a search feature and trust me if you're do it by yourself using a Java code or any programming language it is very time consuming and we have to write a lot of lines of code so we'll understand how do we do this using the mongodb address so here let's say if I want to search for maybe I'm looking for a job only in Mumbai so I can type Mumbai you can say it will give you the jobs which in the description where Mumbai is mentioned so basically for sure when you search for something we not just searching in a profile so let's say this is a profile here this is a description this is the number of years of have experience wir and these are the Technologies you should know so basically we are not just searching in the profile we are also searching for the description and the technology okay maybe it's like a more of a Google search which we are trying to build it will be quite awesome right and also what if you want to hire someone maybe if we want to post a new job so let's say in my office I need a requirement of a blockchain developer so I can click on hire talent and let's say job profile I will say blockchain developer and number of experience required is two let's say say a description person should be familiar with ethereum developer who knows how to make dabs that's it and then the the requirement is let's say the person should know JavaScript anyway we don't have a option of solidity here but again I wanted to keep it light so we got JavaScript there you can change the UI the way you want it's all about the submission which you do for the server so when I click on submit and if I search for let's say blockchain developer now is it there it should be okay let me refresh so you can see if I go back to this page if I go back to get job now we have a requirement for blockchain developer cool right and if I search for dab okay you can see we got the developer here okay so this is what we want to build now for this of course the react app is ready so I will show you the code a bit but we not be writing the react code by ourself but we will be requiring a spring boot so we are going to build this with spring Boot and we are going to use use mongodb in the back end so let's get started so we'll minimize this screen I I don't want this and in fact I wanted to show you the vs code so this is basically the the uh the react project which we have so you can see we have multiple files here this is for the form this is for the feeds what you see all the developers all the job posting and you can see the request is getting sent to the server again if you don't want to use react in the front end you can you can use normal uh UI as well or even if you don't want to touch the UI part I'm not a big fan of UI by the way we can use the apis calling maybe you can use Postman to test these apis or we can use Swagger okay so we'll use all those things in this project I know it's quite exciting so let's get started okay so what we'll do is first of all let's create a project from scratch now what are the Technologies we going to use here so we are going to use inj for uh Java we are going to use the database which is mongodb now why I'm choosing mongodb here why not other database see the thing is there's a huge debate debate between SQL and nosql datab databases which are good I have made a video as well before this so you can find the link in description which talks about what is nosql so basically just to give you an idea there are different types of databases available we can categorize them as SQL and non-sql in SQL we create tables right so we have a database in database we have tables so in in nosql basically we don't maintain the structure now why it is important is because when SQL got famous the at that point it was around 1970s at that point the memory was very costly so we have to make sure that we use minimum storage and we can store maximum data and that's why the SQL type of table makes sense now if you talk about the current world we have so much of data which is unstructured you can't actually create the columns based on that so what if your table is created and now you want to add a new column trust me it's a big headache in SQL that's one so basically we don't want to use SQL here the second one is you want to scale your application you want you want to have replicas of your of your database it is very difficult to do it in SQL of course you can have replicas but then it's very difficult to sync it so not no SQL database excel in that and we can Implement that with the help of mongodb so mongodb is a nosql database where you don't have tables what you have is documents so if you want to relate it's very easy database database is there on both the side in SQL we use tables right which is which has data here we have collections so we can have tables related to collections in SQL you store your data in the row format right so if you want to represent a particular entity let's say I want to store my information my name my age my phone number my email ID that becomes one row now that in nosql becomes a document now the way we can have multiple rows in SQL we can have multiple documents which is a part of a collection that makes sense right so basically whatever data you're storing in fact the data which you have here this is a profile this is a description this is the experience and this is a technology you know so this becomes one document and if you have multiple data that multiple documents okay and we don't have to install any software for mongodb because we are going to use the cloud service and trust me this skill sets which we are going to do in today's session is very important okay so we I already have a intj on my machine which is int Community version we are going to use that but then we want to create a spring project right and since we have a community version I'm not paying for it so we have to make sure that we have that second we want to create a service we want to create a mongodb service right so basically what we need to do is first we need to get the account on mongodb Atlas okay it's very simple you can just click on it and the important thing is it will not ask you for credit card information hopefully let's see if that uh is asking for you uh so I will sign up with my Google account and this should be a quick process so click on submit you can choose your email ID and you can choose you can click on submit I normally prefer to have Google login everywhere so that I don't don't have to remember the passwords so it says welcome you uh use your account to deploy depl Loy a cloud database okay that's what we're going to do and it's still building it okay so basically it is asking you some questions you can just enter okay we are here to learn uh mongodb whatever application we are building we are building content management and then preferred language so basically we are going to do that with Java now that's a beauty about mongodb okay which is you can use multiple languages here now since we are going to use Java so I can click on Java here and click on finish now if you're getting confused how exactly the document looks like so if you have ever worked with Java JavaScript the JavaScript object you know how you create that's how the document is getting stored so basically that's a Jon format for us but in the storage it is getting stored as Bon which is binary sun right which is Javascript or we can say binary Json to make it easy okay they still loading the page maybe the internet is low on my side and that's what it is we got our story the only thing is it will ask you what type of service you want okay so if you can go for serverless you can go for dedicated but you have to pay for it now since we are into learning stage we can select the free one so you can you can click on share and you can click on create it will create a cluster for you now of course this is not a point where we talk about cluster imagine you're getting these services and the beauty is you can choose any of these cloud service so mongod D behind the scene will allow you to choose AWS or Google cloud or Azure so we are going to use AWS here okay so region is Mumbai by default you're selecting the good things uh don't need to change anything here the only thing I want to change here is the cluster name I will name this cluster as the go and click on create cluster the next thing is it will ask you for the username and password to protect your database right so if you want to connect you have to enter the username password so you can see we have the root uh by default I've enter entered this before as well so it is taking the default values so you can enter whatever data you want to have I'm just typing root and root and I will click on create user so my user is created quite simple right uh the next thing I want to do is I will be using this from the the local environment so I'm connecting my application which is running on my machine with the mongodb atlas so we have to enter the IP address I want to protect only I want to protect this by allowing that only my machine can use that database so I can click on add my current IP address click on that and click on finish and close so by doing this you got your database deployment so as you can see our database is created now what's the next step in fact we got our cluster not a database if you can see we don't have a database yet so it says database deployment but then we need to create a database so you can see we have an option of database here when I click on database we already have one now what I will do is I will click on browse collection so basically we don't have a database we'll create that in this so this is the dashboard and we can see we have an option of monitoring but we will not do all those things let's click on browse collection and as I mentioned the tables in SQL you you can map it with the you can map it with the collection here so let's click on the browse collection and it's trying to retrieve the database and you can see we don't have it yet so databases number of database is zero so we have a cluster ready which is teliscope and we need to create a database now so click on add my own data so basically you can also load some sample data set which they provide you uh so if you don't want to have your own database or stuff I mean if you don't want to have your own data so you can click on load sample but since we have our own data we can click on add my own data here and it will ask you for database name so I will say this is for job listing and then the collection is job post in fact you know what I will do I will also keep my database name as Tesco just just to make it easy and the collection name is job post so click on create okay so as you can see we got our database name as telescope and we got our a collection name as job post and as you can see we have an multiple options here we'll see this agre ation search index later so we'll have a separate video on it uh and I also try to explain some of the more features but as of now just to keep it simple let's say this is our data here and as you can see we don't have any data it's empty so we can click on insert document because I want to see some of the listing some of the jobs so I will click on insert document and this is how you can insert and the beauty is this data looks like a Json format maybe a key value P just to simplify that now I don't want to enter the data by myself I already have some data set ready with me so as you can see this is the data I already have here so I will just copy paste this part into this so as you can see I have inserted data now how this data looks like so you can see we have an ID here we got description the same thing which you were able to see on the page we got experience profile and the Technologies which you know and you can see this is an array basically in Java now how can you relate this with Java pojo so in the Poo we create a class right and we have different fields example if you talk about a person a person will have a name a age age now that thing comes as a key value pair here so you can say this is age uh this is name and stuff so I will click on insert it will insert some 11 10 11 records and you can see how many records we have so in total we have 10 records that's great so even this is enough in fact sometime in real application you'll be having lot of data to play around but let's say we have 10 and I want to fetch this data from my Java application okay so remember this this is the thing we are building so if I remove the stuff this is what I want to fetch so what I will do now is so this thing is working because I already have this project ready in my inj so if you can see I was just doing some testing here so the project is ready but we are not going to use this project so I will stop this the moment I stop this project from the spring board so you can imagine we have two different application running the front end and back end so what I will do now is if I go back to my browser if I refresh you can see data is not coming you know why because the server is not running of course you can do you can print some message here the the server is down or something of course that comes at a later part but as of now I'm not printing any error you can check in the console log it will show you the error that server is not running or something okay so this is what I want to make it work and as I mentioned before you can also use a Swagger API so let me show you how what Swagger API looks like of course we are going to build this the entire stuff one by one so let's say if I run this project and if I go back to my browser and here I will run 8880 port and you can see the Swagger API is running with that so if you don't want to work on a UI part you can also use Swagger and you can see we have a post controller here and these are the options you have so even if you don't want to use front end you can use Swagger you can get all the post so you can see if I uh click on try now execute it will show me all the documents now this is coming because the server is running okay now if I stop the server even this will not work so if I click on execute now so you can see we got an error in fact this Swagger itself will not work so you can see that this is not working so if I open this Swagger homepage is not running so we have to make it work okay now for this what we going to do we have we have to build the project from scratch so basically we have to go to one of the page which is start. spring.io now basically you you can use intellig Enterprise version you can use Eclipse you can use SS your choice but I'm going to use in community version where you don't have an option of creating a spring project by default so I'm going to use start. spring.io which is spring initializer now in this we are creting a MAV project so basically imagine you're creating a project and you need certain modules you need certain project structure so you can click on Maven project here and we are going to use Java language the next thing is you have to specify the version which is 2.7.4 the only thing is if you want to use Swagger this version does not work properly so I will change the version once the project is created but if you don't want to use Swagger this perfectly works the next thing you have to mention is the group so I will say this is com. Tesco and the artifact is job listing okay that's the project and we can keep it as it is this is the package name that works for me and then we got the jar file so we have to create a jar file here so once you deploy the application when you export when you create a package the packaging will be happening as a jar not W and you can choose a Java version 11 works so we can go for 11 now that also depends upon what setup you have in your machine so you need to have a CO Java setup basically jdk setup uh 11 17 works you can use that 19 just launched so you can use that as well but I'm a I'm a person who prefers stability so I will go for 11 the next thing is you have to add dependencies now what are the dependencies dependencies we need here so basically we are building a web project so I will use a web spring web okay uh the next thing I will need here is mongodb so I will search for mongodb so basically we want to go for spring data mongodb and the beauty is you know all these things which will happen happen with less amount of lines and trust me it's beautiful so if I click on this uh you don't have to write a lot of lines and some sometime It's Tricky when you have a lot of lines so I will click on generate and you can you can see we got our project here so what I will do is I will just unzip this project so you can see this is the project which is unzipped okay so I'm making sure that that project belongs to idea project so in my uh user account I have this for which is called idea projects I'm I'm keeping that project there so that I can open that int from intell so I will open intell of course when you open this you will not see the project so this is what you will see and this is my demo project which I've already done and now I will click on open project from here so basically when you open intellig idea of course this is the community version free one so I click on open and navigate to the idea projects and in that I have a job listing click on open okay we have to tust we don't have a choice and we got the empty project as you can see we don't have a code here but I just want to verify if my pom file has it so pom file you can see the version is this this will may not work with you so I'm going back to 2.5.7 there's not much change between these two versions and it perfectly work so this version normally works with swagger and so you also do it when you are trying this also try with the older version and once it is working then try to change the version to see if that if that is working okay now these are the dependencies which I have right so if I it you can see it is downloading dependencies whatever jar files you need in this project will be downloaded but till that point we also need two more dependencies which is for Swagger now how will you get this dependency it's very simple uh you can go to one of the website called mvn repository and you have to get this two the Swagger UI and Swagger 2 okay so you can click on that search and you can click on the first one the version which normally works is you can go for 2.9.2 you can see we have lot of use here so I will use this particular version copy this go back to your code and paste the same thing you can do for the second one with the Swagger 2 so you can see I'm searching for Swagger 2 here the first one and the same version make sure that you have the same version so that you will not get any conf conflicts and paste so you can see we got these two libraries and if I open my external libraries you will find all this uh files here so you can see we got mongodb we got spring framework files and this should be for Swagger as well it will be there don't worry you it will be there so let's say if I minimize this now so we got Swagger in in case it's always good to refresh your Maven dependencies you can also click on this Maven and click refresh it will make sure that you have all the SP is required anyway otherwise it will give you error here if it is not working you can see we have all the jav files and now it's time to write our code the first thing you will do is if you go to your main see ultimately we have to connect our application with mongodb right so we have to specify where you do you have your mongodb do you have your mongodb in your system or you're trying to connect the cloud that's first second what is your database name what is username password of course right for connection you have to pass the cenes as well so what I will do is I will do all the configuration if you can see we have a resource folder in which you have application. properties this is where you do the configuration now what's the configuration I need here so basically I need spring. dat. mongodb do URI so this is the uh First Property you have to assign spring. data. mongod d.u and we have to assign a URL now from where you will get the URL or URI in that case we will go back to our mongodb the cloud and you can see we have should be having an option of if you go to overview you can see this's option of connect okay so if you click on this connect here it will ask you for what type of from where you where do you want to use this URI so I want to say I want to connect to an application so click on this now it will ask you which language you're using so we are doing it with Java and the version is this and this my friend is your Ur So I will copy this and paste done okay the things we need to change you can see it's a long string the things you need to change here is you can see this is your cluster name and this is the URI but password is not mentioned here so you can enter the password manually so the password for my machine is root so username root password is root and that should work the next thing I want to do is this is where I want to specify the database name as well so basically this is the property so string. dat do I mean spring. dat. mongod db. database and this is where you have to mention your database name and I guess database name was delisco itself so we'll skip it delisco I can just verify once again I can go back to the collections part here and you can see we have a database which is teliscope so use the same name so database name is telesco even my cluster name is telesco you can have a different so make sure that you enter your database name here okay that's it this is the property have to mention and now I can simply close this I don't even need the form file close this okay now what next now we have to write a code to make it work so what I can do is I can go back to my Java and you can see we already have an application here now the thing is I want to enable the Swagger API and as of now we don't have a Swagger API enabled if you don't want to use Swagger you can skip the entire step and since we are not here to learn about Swagger and stuff so just to give you an idea it helps you to request to your web service okay now if you don't want to use Swagger you can skip this step so if you can see I have just copy pasted and this is the only thing we are copy pasting okay because we are not here to learn about Swagger so the only thing is you have to do is for the application you have to enable this Swagger to and we have to import the package for this uh we have to we have to make a bean because this is how this Swagger will load it as I mentioned before this is a optional step for the project so I'm just I'm not explaining this stuff because I just wanted to show you how do you build an application uh let me import the packages okay and the good thing is I will make sure that this project is available in your uh in the link in the description as a GitHub link okay so this is done we have done with our main okay now this is only for Swagger and we are not going to use this file anymore so we are just enabled this Swagger okay but then now I want to send a request to the server right now how do how do we do that so in Spring framework basically we create we use something called Spring MVC which is model view controller and here we have to first create a controller and you can see we don't have a controller here so what I will do is I will create a new class and I will this is a class basically the name would be let's say post controller because now we are going to work with the post right job post so instead of saying job post every time I will say post controller and that's our controller here now how do you make this a controller it's very simple you you make you make this controller with the help of rest controller okay and in this you will write your method so for every request you want to send let's say I want to fetch all the post so I can use the URL which is Local Host colon 8080 that's the that's the port number where the server will be running and then I can say slash all post it will will give me all the post what if I want to submit a new request so I can say/ poost with the post method uh so the first first one was get method the second will be post method and what if I want to search based on a term and for that we can use again a get to fetch but in this particular video we are going to focus only on how do you get all the records okay that's it or maybe I can also do it for the submit part but the first thing I want to do is I want to enable this Swagger right so in that case what I can do is I can create a method and this is only for swaggers for you can say redirect so every time someone send a request for the homepage it should redirect to the Swagger page and to do that we have to import HTTP Ser response response okay so basically what I want to do is every time someone request for the homepage okay we have to do the do the mapping for that as well which is request mapping so when someone request for the homepage we have to mention that hey call this wager API okay and to do that what I will do is I will say response. redirect and in braet I can mention Swagger do Swagger ui. HTML and it is giving me some Er okay it's asking me to add the exception so I will say throws IO exception and that's it the Swagger UI will be ready let me just prove you that now to prove that I will run this application first I will right click the main uh application and say run okay it will take some time I hope it will work without any error okay you can see it is working and how do I verify that so I can just go back to the browser and okay Swagger API is not working refresh enter and you can see we got our Swagger API it's simple and this is our controller name if I click on this controller you get all these options by default but I don't want this options so in that case I can I can what I can do is I can just write API ignore API ignore okay that's it so when I say API ignore and the server go back here and now when you refresh so you can see it has removed all the pr predefined requests we want to Define our request by ourself so I can say no operation defined in the specs okay that's good so what I can do is now I can Define the request now what request I want to send so basically here I want to have a option of fetching all the post now for that I can create a mapping so basically I want to what what what mapping we want to do we want to map it for all the post so I can say public which will return the least of post now what is this post hold on so for a document I also want a pojo right which will map the document and pojo okay we don't have a pojo here and the first and one more thing the post controller should be a part of a controller right so let me just move that so I will say new package and I will make this as a controller and let me just move this I want to create one more class here in fact one more package first package and this is where I will keep my models and in this model I want to create a pojo now what pojo a mapping class class which will map my Java class with the document so I want to name this as post itself the job posting okay not the post method oftp the job posting and here I want to have some variables now what are the variables I need so if you compare with the database if I go back here so you can see we have a description here let me just increase the font size a bit it's not visible so you can see we have a description we have experience we have profile and we have technology which is an array the same thing you'll be doing here okay so what are the things we need in the post so first of all we need a profile right so if you remember database we had profile description Technologies and experience so let's put that so we got first thing we got we are going to get is the profile then we are going to get the description then we'll need uh the experience and the technology so we can say text that makes sense okay but then this text will be an array and that's done of course you can keep this bracket here as well that works and for this I need gets and Setters so so I will right click I will say generate uh I want a get set up for all the fields and click on okay and with this I also need a Constructor generate a Constructor with in fact I I want a blank Constructor without any field and we also need a two string in case if I want to print that's it so I will say right click generate two string okay so we got our post right it's so simple okay so going back to the controller we got our post now so we can import the package that's imported and we have to also import the list that's done so if I want to fetch what method should I have so I can say get all post even that works and name doesn't matter right so we can say get all posts and now I want to do a mapping for it so when do you call this particular method see see of course the homepage is done now homepage Returns the swager page but let's say in this Swagger if I go back and I want to have let's say uh a URL which request for all the post it should fetch the post so in that case I can just go back here and for get request I can say get mapping and the URL which I want to have is let's say all post so if we are requesting for all the post or maybe I can simply say posts that works right so we can say posts and it should return the post but then how do I return the list here by will return empty that works now if I want to fetch this data what I will do now is I want to return the L how will I get the data so of course I want to F this data from database right now if you know code Java jdbc and database we have to write lot of steps right but not in spring so we can use spring data remember we have added one of the library for spring data this one spring starter data so basically we can use that to fresh data and for that what I can do is I can create a new uh interface basically not the class interface and we'll name this as let's say post rppo all we can say repository okay now this is this is how you will fretch data okay you don't have to write any methods any implementation I know that sounds new for some people they're like hey without writing any queries without writing anything how can I FES data let me show you the magic now so what I can simply do is I can implement this interface or not implements I can extend this interface with the repository and in this you have to mention two things the first thing is uh what type of data you want to you want to fetch which is of post which is of post type so basically this is the mapping uh so whatever data whatever document you fetch will be a type post and the next thing is the primary key basically but then we don't have a key so I can keep it string I'm assuming that it will be string okay so let's get it that way um and then that's it we don't have to write any methods in this you don't have to create any class which implements this the repository will take care of all the Cod operations fetching creating updating and deleting okay that sounds fun right let me show you so what I can simply do is once I got this repository remember I don't I'm not creating any class or stuff I'm just going top here and I'm saying create the object of post repository not the object but not of not the object of the interface but behind the scene the spring framework will create a class for it it will create an object for it you don't have to do that and I will simply say this is a repo and I will just map this as Auto wire So when you say autowire it simply means spring will create an object for you and it will map it that's it the that that auto wiring part and you don't have to do anything else just come back here and say repo do find all can you see that we have so many methods we have not written those methods it's in build in repository that's it I know you will not trust me at this point the only way I can prove my point here is by restarting the application and I know you might be saying there's a difference between the time here it's just that my camera was not working properly so I have to take a pause and do it again and if I refresh oh can you see that we got all post and if I click on this hopefully this will work it's already been a lengthy video I will click on try it out execute oh okay we got data but it's not printing data here oh something went wrong here so we're requesting for posts and it is requesting for the Post find all okay now what is missing remember when we go for the Post repository we have mentioned that there's a post right there should be mapping but post is not aware of that so what we have to do is we have to say document here and we have to mention which collections this will map to so this will map to okay what's my collection name if I go back to my thing it is drop post so I will just come back here and say job post now this should work so if I restart this server uh there's no error and if I go back to my Swagger API hopefully this time it will work execute oh can you see that we got the data this is what we wanted right oh it's working and if you look at the code it's so simple we just got a controller here where we are doing the mapping we got the post repository and nothing much here we're not declaring any methods or stuff and if I look at the post this is just a pojo so fetching is working that's awesome how about posting the data what if I want to submit data Maybe I want so apart from this all post I also want to have post which will submit data a post method let's try this should be simple so I can create I can simply create another method here for posting data what if I want to get my own records so I can create a method which will return the post itself so whatever post you are submitting it will return that and I will say add post of course method name can be anything and okay the mapping what type of mapping I want to do any guess we are submitting data so for submitting we have to use post mapping and here we have to specify the URL I can I say URL post see when I say posts for get it is it will fetch okay so in the method we are specifying it's get method so it will fetch in post we are submitting data okay this post and post might be confusing but this this post is basically the job post this post is HTTP method but then when you submit data we have to also accept it right here so I can use a request here to accept now see if you don't understand all this concept see flow is something very easy right easy right but what if you want to do do it by yourself don't worry I have a spring boot tutorial you'll find the link in description which explains each and everything properly this is a project video right so I'm just focusing more on the project part okay so request body and here I can mention the object where it will receive the data so I want it to receive data in a post format so whatever data you will submit from this from the client side will be accepted here in the post object I know that's something which spring is amazing at okay once you got the data I can simply say repo dot uh okay how do I which method I have to use for saving I I think there's a save method yeah there's a save method which will accept the post object that's it that's it no need to mention any query no need to mention any code for it we done with this but I guess save returns something okay what I will do is save Returns the object of post so we can simply say save it will return the post object and we're returning it that's it and here I think it should work if I go back or we have to rest the server so you can see we are fetching data we are also submitting data and if I go back refresh okay post can so that we got post here that's that's awesome and you can see it will give you some demo data as well so that it is already mapped right so I want to submit data I can say description okay description first of all I will set the profile profile I'm looking for is blockchain developer and here description can be ethereum developer with daps experience in Mumbai or maybe I can say this time I can say Pune okay experience I need only two years BL developer technology what you should know let's say you should know Java I don't know why you should know Java but let's say Java then we can say solidity and you need to know XML I don't know why but let's say XML and let's click on execute it should work oh it worked can you see that we got the data but then we are getting the data on the client side right let's verify from database so if I go to database now and let's see if that is there let Let me refresh the database hopefully it should be there it should be there it should be there if I scroll down down down can you see that we got the blockchain developer ethereum developer whatever you have written there and the array of your skills awesome it was so simple right so basically we were able to request data we were able to save data but of course at this point we have only worked with the swaga part we have not connected with the UI uh we'll do that and also there is no searching option okay we have not created something for searching we'll do that in the next video so in the next video we'll also try to understand how will you use mongodb what is the feature it provides and then we'll explore one more application is called mongod DB Compass okay which is a UI basically I'm just everything which I want to do I'm just going to the browser right and I'm accessing the cloud service what if I want to access everything on a desktop application sometime it is very easy to access a desktop application than going to the website so we'll try to explore that as well in this video we'll focus on two important features of mongod DB the first one is the mongodb compass and the second one is mongodb address search that's right search because remember in the first video if you have seen it so basically we were able to search the data from the document and irrespective where that data is is it a key or in the values okay somewhere in the description there was something mentioned it was able to search search for every text not just the title okay so let's try to implement that so how will you get the compass so there should be an option here otherwise what you can do is I can just go to Google and search for search for mongodb Compass and this is the website in fact you can directly click on download but let me just go to the website first so this is how the mongod compass website looks like so this is a GUI for mongodb that's right so basically you can access the mongodb address from the browser in fact you know we have multiple options you can use mongodb on your local machine when you do that basically you have to use the console and you have to type a lot of commands and if it of course as a programmer it's a good thing but if you want to use a GUI tool you can use compass for that as well which connects with the local system or you can use Compass to connect with the cloud as well you have a choice so this act as a GUI for both and for this example we're going to use compass for the cloud so you can click on download now it will download and it's a straightforward setup you can just click cck next next next uh nothing difficult it's just that you have to specify which version of compass you want to download uh which platform you're using and what format you want to download so click on download it will give you the setup now once you click on the setup once you click on next and install it's very easy uh nothing fancy there the only thing is when you open mongodb compass you need to do some configuration that's it when you open it and I'm just I have just clicked on it it it might take some time so this is how your mongodb compass will look like and you can see it will ask you for the connection now mongodb Compass is not sure do you want to connect with the local machine or from the cloud now based on that if it is a local address this is support number you can click on connect but I think in my machine I don't have a local setup you can see connection refuse I don't have the local setup but what I have is I have a cloud and to connect the cloud you have to enter the URI oh from where you will get the URI remember in the project we have done that but if you have not following the project that's completely fine when you open your uh let me just start from the cloud itself so when you go to the mongodb atlas and when you have your database here you can click on connect and when you click on that you can see we have multiple option but this time we are going for the mongodb compass and the beauty is they both have the same URL so I click on this and I will say yes I have mongodb compass and the version is this and later this is the URI so just copy this and for my database I have a user name which is root and the password is also root so I will replace that with with this so here I just have to replace this part with root that's it and click on connect I hope it will be connected oh it is you have to has a men mention the database right why just giving test database I want my database it should be theis let's try so click on connect and it says connecting oh it connected Can you see that you got your host and that's the beauty of mongodb so every time you create a database it will create multiple replicas of it of course we have to be fail safe right what if you have only one server and that goes down you need a backup servers so mongodb for every database it creates two extra copies for you we call them as replica set so in total you have three working databases at the same time and you can see that is what you can see here on the left hand side if I click on databases here it will show all the database which we have created and we have created only one telesco it was by default and if I click on telesco database you can see it is giving you option of the collection which is drop post if I click on this we got all our records so it's better to use the GUI than using Cloud uh the only drawback which I can see is if your machine is slow Compass will give you issue that time you can use cloud but it's not that heavy normal machine can take it up so again it's depend upon the developers what they want to use I normally prefer to use the cloud service Some people prefer to use the GUI tool to do everything or the desktop application but again that's that is preferred depend upon the people okay now this is the first thing I wanted to show the next thing is I want to implement the search feature and this is the beauty about Atlas search so in the mongodb the atlas we use something called a search option remember when whenever you work with data and we have created database right and normally we only had around 12 13 records or 11 records in normal databases which is production grade we have thousands of Records oh that's less we have millions of Records now if you want to search for a single word from all the records trust me it's very timec consuming so basically as a user of that application when you type you have to wait for minutes to get the output and we don't have that much of time right so how will you speed it up and that's where in the database concept we have something called indexing so every field every text will be indexed so that it will be easier for search okay but then how will you do that do you have as a developer do you have to do everything uh not exactly if you can see the they the same option is there in the on the cloud as as well okay so you can click on this index and you can create index from here but since as I mentioned before I'm a big fan of cloud so I will be doing that from the cloud itself so let me go back to the cloud service and if I go back here okay so I will click on my collections there is an option of creating a search index so if I click on this index okay this is where you can create your index so it says make your data more discardable with Atlas search and I can create the index here the only thing is when you're creating the index it will give you two options V Visual and Json I am a newb to this so I will click on visual that's what we do right when you learn something new you go with the basic steps keep it simple learn the first layer and I always believe in the uh learning where we have onion uh peel like learning right we have different layers so you learn the basics and then you learn the advanced part then you learn the more advanced part so we click on next it will ask me for the index Name by default the name will be default keep it that as it is you can change it but let's keep it default and you can mention what database and collection you want to index I want to I want to index the job post if you have multiple databases and collection it will show you the options you can choose one and click on next and that's done oh we have to click on this create search index now before I do that in fact let me just do that and I will show you something there's interesting thing called index Fields there's option of dynamic so what happens is by default it will IND it will try to index everything from your database or everything from the from the collections it's good but then if you have a huge database and if you have huge number of documents it is way timec consuming because it will it will take a lot of time second it will consume lot of memory and sometime you don't want to do that why you will not do that because what if you have multiple Fields let's say we have eight or 10 fields in the document and not everything need to be indexed maybe you can specify these are the three three fields which I want to index example in our case maybe I want to index description I want to index the profile that's it I don't want to index other things I can specify that with the static mapping by default is dynamic it will map everything I can also make it static now even for 11 records it is taking so much of time maybe it is done I'm not sure let let me just refresh this so you can see we got only 11 records and it is working now so the mapping is dynamic mapping here and if I yeah that's done so indexing is done now let me just go back to my colle ctions so that's the first thing if you want to apply the search you have to first create the index and now using that index we can actually test it uh so okay how do I test let me just go back to search there should be an option of testing as well click on visual editor okay so if I click on visual you can see we have an option of search tester it is indexed right it should be able to search now example let's say I'm searching based on Java so if I say Java and if I click on search it will show all the documents where Java is mentioned in the profile in the description or in the technology example in the blockchain developer we don't have Java mentioned in the profile and in the experience in the description but it is mentioned in the Technologies if you can expand this you can see we have a Java here so basically everything is indexed it is easier to search and you can use the search from the from the client side but you will say hey that works right I mean this is working from the cloud what if I want to do that from the Java code uh that's tricky right how will you do that let me show you how to do that so if I go back to the collections and if I click on aggregation can you see that we have an option of aggregation now this is where you can create a pipeline what you want to search and you it's not like you you can search only one thing maybe I want to search based on Java and even for Java let's say we have 50 records so let's say in total we have 200 records and if I specify Java it is giving me only it will it is going giving me let's say uh 50 records now out of 50 records I only want five and that two in this sorted order of experience can we do that so basically you can see I applied three filters the keyword Java second I want to sort it and I also want to limit the number of post which I can see so we can do that from the aggregation uh so if I click on aggregation here taking some time for the agregation Builder so if you don't apply any filter it will show all the post so in total how many post we have uh so we can see we have 11 documents here but then if I want to apply a filter I can do that but from here so you can see we have an option of stage by default it will give it will show you one stage I will click on select now you can you can basically apply multiple operators here the first operator which I want to use is search so if I scroll down you can see we have an option of search and this is what it will give you so I will just remove this extra things from here now index what index we have remember when we created the index we have to also mention the index okay so if index has not specified or not created this will not work so you can specify the default but even if you don't mention that's fine since it is we have a default index we don't have a different name so you can skip this part next you can specify the query what you're searching so basically I'm searching for Java in description or maybe I can specify in text now you can see it will only it is only giving you eight documents so out of 11 documents it is giving you eight documents here which has Java in it okay that that makes sense now you will say what if I don't want to just search Java in text let's say I want to do I want to search for Java in description profile everywhere so what you can do is you can make this as an list so we can say list of text comma maybe I want to search in description maybe I want to search in profile so basically we are searching for Java in everywhere and also we can we only got eight documents which is there so you can see we got Java in profile uh we got Java in profile okay we got Java in Technologies this time right and we got Java in um we have Java here so basically this is how you get it this is how you apply search what if you want to apply a sorting on this how I want to sort this I want to sort this based on the experience you can see we got experience 10 we got experience five I want to sort in ascending order so what you can do is you can apply a stage here here that's what aggregation is pipeline right the first search then sort it then limit it so we can expand this and I want to sort so we can just scroll down here and you can see we have an option of sort now it will ask based on what field you want to sort I want to sort based on experience and sorting order so if you say one it is ascending minus one is descending if I'm not wrong yeah so one is ascending so you can see the experience we got experience here the first profile has Experience One second profile experience 2 it is working you can see that we got experience 5 experience 10 and then least goes on it it keeps increasing right so the Sorting is also done and that's eight documents but I don't want eight documents I only want five documents again I can apply a stage and this time I want to do it for I want to limit right so limit should be there yeah limit is here and it says how many documents you need mention the number I will say five and that's it you can see here it is loading sample five documents and we only got five documents it's that beautiful right and yeah you got your pipeline now you will say okay that the pipeline is done here itself what if I do that from the code we still AR we have we have still not done that right okay that's one thing we'll do that okay in the next video we'll try to implement this feature in the Java code but before that I want to mention one more feature of mongodb now mongodb Atlas has one more feature which is client side field encryption what it means example let's say you have a client application where user is submitting data now it can be your name it can be your age it can be your email ID that that works if if anyone can see that even I don't think email ID should be shown email ID SSN maybe there are some sens sensitive information so you have a bunch of data with you and some of the data is public is okay for you but I want to encrypt some of the fields maybe email ID SSN number or whatever fields we have how will I do that I what I want want is from the client side the moment your data goes on the public database of course right in mongod DP behind the scene it is using AWS Azure gcp which is a public Cloud I don't want to compromise my data based on their security how about encrypting the data in sending it there so basically it provides you something called field encryption where you can specify which field you want to encrypt from the server side okay let's say this is your server that's your database the moment you send your data from server to database it will get encrypted okay so the what you save in the database is encrypted but what if you want to get data back when you fretch data from database it will be coming in encrypted format itself but the moment it it is received on the server where you have your driver it will get decrypted okay so basically you don't have to do that encryption decryption uh Atlas will take care of it of course we're not going to do that in this course because that's a way out of the curriculum but yeah we can do that again try that there's some amazing documentation available I will try to put that link in description as well otherwise you can search for client side field encryption using mongodb and you will get the documentation now basically till this point we were able to create a spring boot project which has basically some controllers using which you can request to the database which is in mongodb and we have some uh documents available there we have a collection using which we are fetching data and then we were able to fetch data we were able to submit a new post right we are basically working with a job post application here and then in the previous video we have also seen how do we create a search index how do we create a pipeline and then we were able to filter data right so basically we were able to search we were able to sort data we were able to limit if you have not seen those videos first I would recommend you to watch the first two videos to get proper understanding what we are doing if you have already seen that let's continue so basically at this point we know how to do it from the mongodb aggregation pipeline console and we were able to get the data right but now I want to achieve that with the help of java so how do I fetch data from Java application whatever we have done here and I think that will be tricky right it will we have to write multiple lines of code multiple files to filter out and all those stuff let's see how complex that would be apart from that we'll also connect our application with the react project remember we have the react project we which was not working because we we stopped the earlier project which was working and then we are building the Java project from scratch so we'll also connect those two applications let's see how so basically uh the pipeline is very here right so this is what you got but now I what I want is I want to do the same thing from java so maybe as a Java developer when you maybe we can create an API where you can search for a particular term and it should do those processing here now to do that it's not very complex okay so in the Java code which we have here we can add one more controller let's say let me just add one more controller here which is public uh it will return the list of post of course the same thing but this time it will be filtered right so the difference between in fact let me just write it next to or just below the get mapping this one uh so this should be similar right maybe I can say search to make it simple so we are searching for something right so it should also return some data so I can say return repo doind all as of now let's let me just go with find all and the request for this would be get mapping and I want the request type should be uh it should be for posts the only difference is in the post I will also specify the text which with which I want to search example let's say I'm searching for Java so it should be Java but then I want to make it Dynamic so I can say text whatever is user is entering so if a user says Java this will be replaced by Java if a user says blockchain or maybe Mumbai so whatever text a user want to search this will get replaced but how would I accept that in a variable so in that case we have one more orientation which is called path variable and I can say string text so whatever data is coming here example the URL is let's say posts SL Java now this text will be replaced by Java and that's what you will get here okay as of now we are just doing find all just to see if this works and whatever text you are passing here and how do I verify that let me just run this code I me restart the server and okay everything is working there's no error let me just go back to my browser and Swagger UI let's refresh controller uh you can see we have three now so initially we had only two this time we also have a text search if I click on that it will ask you for a text oh we have not um okay try it out it will ask for the text let's say if I say Java and if I let me very specific which is Mumbai because we only have one or two with Mumbai post but if I execute you can see we have received multiple not just two if I'm not wrong you can see we haveed multiples filtering is not happening okay we have to do something for that right now why it is not working is because it's very simple we are saying find all we don't we should not be saying find all we have to be very specific can I say search by text I mean whatever text I'm getting here it should search by text or maybe I can say find by text so I can say find by text uh but you can see it is giving you error because there's no method which says find by text uh that's tricky so what I can do is instead of touching our post repository which is very specific so post repository is extending repository here which provides you basic operation fetching data posting and all those basic features right c c operations create read update and delete if you want to do something else you want to search based on your own condition that's where we have to implement it that's where we have to do some customization so what I can do is I can just use I can just create one more uh okay this this should be a part of something else this should be a part of a repository package so I can say this is repository and I want to move this post repository here and in the same repository I want to create one more class now or interface actually which is let's say for search search post or maybe I can say search repository I'm just finding a good word for it so I can say search posit that's it now in this what I want is I don't want to use any mongodb already present methods I want to create my own so you can I create a method in fact it's an interface I can declare it here so I can say find by text which will accept a string I can say string text right and it should return the list list of posts so basically I'm creating a new interface but then the only thing is this thing is not present in this spring data or mongodb features we have to implement it by ourself okay for that what I will do is I will create a class which basically implements this I can I can say search uh repository implementation and this particular class should Implement our search repository as I mentioned we have to implement this code by ourself right uh okay it says we have to implement a method which is this find by text so basically we have to return return a list right so what I will do is as of now let's keep it empty so I can say list of post and I will also make this as a final variable because it's good to have a final variable when you when you know that it will not be changed okay so I will name this as post itself equal to it's an array list so I can say array list okay that's it so we have created this array list here and we have to import the package is it done am I missing something here oh I'm missing a new keyword my bad okay so you can see we got the list here and the same list I will return of course at this point it will return a empty list right and I just want to verify if it is doing that so I will rest the server at least it should return the empty list it should not be giving all the data okay it's not working oh I forgot to one more thing this is a new class which we have created right and I want my thing to read this so I can say it should be component yeah one more thing in fact in the post controller we still have an error we don't want to say repo right repos don't have find by text what what where do we have find by text it is there in the search repository so apart from the post repository I need to create one more which is search repository and I can call this as sreo which is for search I'm very bad with names so let's go for S repo and here we'll say this is auto wir so basically now we have two different repositories one which is getting the features from Spring data and second is from the what we have created and instead of creating this as repo do fext we have search repo by fine text it is asking for the text as well let's enter that so at this point things should work and we should get a empty string let's verify if that works let me just run this application uh no error let me just go back to my Swagger and if I say execute now it should return the empty at least that's the idea yeah so we are we getting to empty right at least we are not getting everything now we have to make sure that we fill this up how let's go back to our code and we just need to change this particular method we have to fill this list how will we do that it's quite simple actually uh if I go back in fact I don't know the code right so what I can do is I can just go back to my cloud and see the beauty here uh if I you can see in the aggregation when we do all this pipeline here when we created this pipeline here there's an option of export to language let's see what happens if I click on this uh this is for this is the pipeline which we have okay for mongodb but if you want a language specific code this is for python what if you want to do that for Java this is a code that's it just copy this code and it will work oh not exactly uh we have to do something extra as well so we need to import you can see we have two checkbox here let's click on this click on this you will get the entire stuff so the first thing we need is this import libraries and copy and paste and if I go back here paste I I I need to import all this the only thing is uh I think mongodb client is not a part of in fact you know what I will do I will not just use import because there's some version issues and uh I was able to resolve that by not copying it it okay uh the only thing you will need here is uh do we need mongodb client okay how will I get mongodb client here okay it says we have to create an object okay I will not do that what I will do is I will just go back here and since we are using spring framework we have to we can do something extra here I can say client and I can say this is client I will just declare it I will not create the object I want spring framework to do it for me so that will'll reduce that line so so we don't have to use this line now the next thing is it will ask you for database connection now we already have a database connection uh but just to keep it simple I will use the same line I can use the entire code from here and paste it inside my method here that simple okay it's just that now uh it's not more no more mongodb client we just saying client and we have to import all the packages for mongodb we have to import for control space anyway we have already done that before right so this should be coming from the B is it B uh yeah and then we have to import for mongodb collection as well from a client and then we have to in fact we'll change this we'll not be using find itable uh because when I was trying it it was giving some issues I found a solution inste of going for find iterable we are going for aggregate itable you know why I'm doing that because this aggregate method returns a aggregate R trable so let's go with that let's import it for arrays as well and done this is your code I know this doesn't look good because it's not formatted properly so I can just do the formatting here so that you can see everything in one line so basically let me explain uh in brief what we have done so basically what it does is whatever we have done in the pipeline here right so this pipeline is converted into Java code so basically it is using a document search you can see we are doing a search here search is happening based on a text so we can also replace that with a wild card uh and then is ask for the query what is your query we have specified the query is Java of course this should be changed this should not be uh Java here we'll replace that later and then it also ask you for where exactly you want to search you have to specify the path so I want to search this in uh I want to search this in text uh Technologies description profile I also want to specify I want to sort but based on what I want to sort based on the experience and one is ascending order and then we have to limit by five I only want to see the five post but then I don't want Java here what I want is I want text so whatever text I'm receiving from here that will be the search term okay this works but then if you say we only got data in result I want this data not in result I want data in a post how would you do that so maybe we have to go we have to iterate we have to search one by one post and put that into post how do you do that okay so how do you get data from result oh there's one more thing missing here the database name is theis you can also get this from the uh application. properties you can just see uh we already have a database name here so you can fetch data from there you can fetch this value but just to keep it simple since we got code from the website itself we'll use that and here we need to make sure that we have okay we are fetching one by one so what I can do is I can use a result here and result is iterable right so it says iterable so we can iterate between the elements and if I say dot there is a method which is for each so basically we will iterate between each document so I got a document here and I will be using a Lambda expression here which is much better so whatever document I receive for each iteration I will add that in a post as simple as that so I can simply say post dot or posts do add now the only thing is I can simply add doc here but that will not work because Doc is a different format post is a different format this Doc is basically a document format the post here is basically basically a Java format uh that will not work so what we can do is we can basically use a converter here so there's something called a converter so if I go back here and if I say converter and I can say converter we just need to use this and since I want the object of this I can say Auto wir Now using this converter so I can say add but I have to convert this before so I have to say converter dot there's a method called read okay and and then in this you have to pass two parameters first is the Class Type you want to convert so I want to convert this doc into post format so post. class and the second is a bent Source bent is basically document the document itself is Doc so whatever doc you're receiving we are passing that here and this converter will convert that doc into post and it will add into the collection or the list and we we are returning it I think we are done with this but the main question is will this work let's try so I will save this and I will go back to to my Swagger it's here oh we have to restart the application as well restart okay so is running go back here and hopefully this will work click oh there's an error we missed something so basically we are passing Mumbai and Mumbai is not working in the post so basically there's something wrong with the mapping which we have done it's sending text here that text goes here so what is the exact error let me check this file so we got to client and then we have specified database name as well oh this is empty we have to also specify the collection The Collection is drob post I missed that okay no errors let's go back here and say execute oh can you see that we got it so we got only one entry which is for Mumbai because we don't have more entries for Mumbai okay let's try something else let's try with threads there should be only one post or one job with which has threads involved and you can see we got threads so we are basically searching for text now and if I search for let's say Java execute and you can see you will get multiple records now it's searching for Java in the uh Technologies it is searching for Java in the text now what is important is if you can search the exper you can see the list it starts with the one Experience One experience two because we have sorted and in total you will get only five you know why five because we have mentioned the limit here if you remove the limit it will be coming with uh all the records and if you don't mention sort it will just print as it is added to the collection is that simple okay at this point the SOA is working SOA is very happy but remember when we have a react application refresh if I go home and if I say get job now it's not working the data is not coming here okay what is wrong what why is not coming if I see the console let's see the console here let's see what it says console says there is issue with uh okay there's a block there's a problem with the access control allow origin basically we have to allow the origins because they're coming from different server right the request is coming from the Local Host 3,000 so what you can do is for that is very simple you can just come here and on top of your rest controller you can mention cross origin and you can also mention from where you're getting the request so I can say Origins there's only one as of at this point which is HTP colon sl/ Local Host colon 3,000 that's the react application we have and that's it we are done with this let's I hope this will work go back here refresh oh it's still not working there's something missing or it still says blocked or do I have to apply okay let me know the solution the proper solution for this what I will do time bin is oh first of all it is post it should be all post that's what we have mentioned in the react application I can change that maybe you can change that in re re application or you can change it here itself just to make it easy I'm changing it here itself all post and then I will use cross origin everywhere in every method just to be safe and now if I go back and refresh oh can you see that we got our data yay things are working out I don't need you anymore close and you can see we got our entries and let's see if that search happens and if I search for Java or let's search for Threads it should print only one yeah it's working can you see that so what what we have done in the react is every time you type something if the length is more than two characters it will hit the request so for every character it is hting the request I guess it is uh no it has to match with the words so it is sending the request and the moment it matches with the words you can also Implement wild card and let me know how you do that in in the comment section if you are implemented wild card and if it is working record a video upload that on YouTube and paste that link in the comments and I will approve it okay I want I want to I want to see your implementations how you are doing it so this is the threads and uh yeah this working we can also try it for High the Talent let me just enter the job profile here which is for let's say editor editor is already there right let's say maybe I want to search for I want to hire a mongod developer and experience should be let's say 3 years I'll type something here and the developer should also be familiar with Java submit let's refresh and you can see that we got mongodb developer working out right so this is how you build a project so don't worry I will just push this project in the GitHub you will find that in the description for all three videos and yeah we have talked about everything everything is working fine and let me know your experience in the comment section and let me know if you want me to create more of such videos on projects so our code is working here so we have seen how to you work with spring MVC we have seen how do you work with spring rest now it doesn't matter what type of application you're building it may be a standalone software it can be a web application or a web service there's one thing common for all the application which is maintaining the log file in fact we have one more maintaining Security in fact we have one more handling the transaction so basically it doesn't matter whatever application You're Building you have certain tasks which you'll be doing everywhere now look at the methods here in the controller itself so this is our alien controller and you can see we have get mapping for get aliens we have get mapping for get alien for one particular alien and then we have it for add alien what happens is whenever you call a particular method we always maintain a log in fact in this code just to make it simple I have not written any log statement but that's what we do right so here we have to write those beautiful logging statements maybe there will be one line here but by saying hey we are the execution started in fact just to keep it simple uh let's use sis out here we can use log as well loger class or something but let's use system.out.print and we can say get aliens method called so this is what we can do here and maybe before returning the statement or before return statement we can say uh get aliens method executed so we can do the log there right so before calling and after calling not just here we may have to write it everywhere in all the methods now that's one second we have to maybe sometime you have to also maintain security not all methods needed but let's say if you have certain methods and if you want to maintain security there so that means uh first two or three lines they will be used by security statements so we have to maintain log I mean we have to write log statement we have to write security statements and then maybe you have to also handle transactions now when we talk about a method in this method you can see if you talk about this particular line this is the actual business logic this is what we want to do we want to find all the aliens but apart from this we have certain other lines as well which is only for extra things okay there is not something a user expecting us expecting all the aliens this is something which we do to maintain the application uh because we want log file to maintain the application uh we want security to handle the application now don't you think we should be doing all this extra things not inside the actual code but somewhere outside so what is recommended that don't write any extra code inside the business logic keep it outside so what we can do here is we can create a separate class let's say this is only for logging purpose so I will say logging and then we'll name this as aspect again we'll talk about why aspect we'll talk about that later but we got an aspect here so we got a separate class aspect and in this class I want to create create a method let's say public void and I will say get log or maybe a log method that's it instead of H yeah so let's have a log method now in this method I will print get aliens method called okay so what I'm doing is I want to maintain log but not in the business logic so you can see we have our business logic here I don't want to do that here whenever I call get aliens I want this to be printed but how how we can connect them there's one way you can create object of this class here and then you can call the particular method from here but again we are disturbing or we are you know making our business logic dirty we don't want to do that we want to keep it clean business logic will be different from the other logic now this thing here is actually called as cross cutting concerns because these are concerns which are actual logic but then these are applicable for all different methods so when you can take it common you know if you instead of doing that in this method in this method in this method you can take it common so we can call them as cross cutting concerns okay but still the question is how this will get called and that's where we have to use a special concept which is a o which is aspect oriented programming but how can we do that that we'll see in next video so let's Implement aop here now first of all to understand aop let's search for aop in fact we'll search for spring aop documentation so it will take you to the official website of it and let's go here spring aop and you can see we have beautiful Concepts here so it stands for aspect oriented programming with spring now there are certain terms okay now first of all spring aop is not that difficult is just that the terms are bit weird now first of all we have a concept of aspect Now what is aspect so you can imagine one class which will have all the methods all the cross cutting concerns example the class which we are working with here so this logging aspect will be aspect for us then we have a join point now see our code our business logic let's say we have certain methods and then out of those methods I want to maintain log for let's say two or three methods so we have to connect them so we have to connect a particular method with the crossing concern so I want to call this log Whenever I Call get aliens so get aliens becomes my join point this log becomes advise so you can see we have one more which is advise here the expression the what you say the Wild Card expression which we are going to use example I want to execute this log Whenever I Call get alien so if we have to specify some wild card pattern right that will be a point cut and then the most important concept here is weing now aop is not new okay so in Spring also we are doing it but in general when you talk about aop we have to connect the actual method with the advice now this connection can be done at compile time or it can be done at run time now when it comes to Spring aop we do it at run time so you can see that's what it says it says spring aop like other pure Java aop Frameworks perform viewing at one time okay we have some more uh just be with me so we have types of advice here you can see we have five types of advice here in fact three are after itself the first one is before advice this simply means when you want to call a particular method for example I want to call this log Whenever I Call get aliens so this will be called before calling get aliens that is before advice after advice means I want to call log after the execution of get alien and then we have around which is during the execution of get alien again we'll understand each and everything step by step in these subsequent videos so that's about the this aop words right and then we have certain types of advice so just to recap aspect will be a class where you will have all the methods join point will be the point of execution where or the point of the execution where you want to call a particular advice advice is the actual method you want to call which is log point cut is the expression which we use and then we have waving which specifies when you want to connect it in fact in advice as well so if we talk about this log advice we have three types there in fact we have five but in general we can talk about three so we have before so before execution of the method after execution of the method and then we have around which is during the execution of a method so let's Implement now so let me minimize this documentation which we have here so let's Implement so let's go back to our M dependency now since we want to use aop so of course we have to add some dependencies for aop as well now that's what we do when you work with normal spring application but in Spring boot if you can see we already have spring aop here and that's the benefits of using spring boot right you get all the dependencies required so we got spring aop dependency otherwise if you are working with normal spring MVC you can simply go to m repository and search for that particular Library there now the first step would be we need to make this class as aspect because this is aspect right and the way you do that is by using an annotation here which is at aspect so say control space and done we have made this as an aspect the second thing is I want spring to know this I want spring to know this there's a class exist like this and the way you can do that is by making this as a component so that's done we have specified that this is an aspect and this is also a component I want to call this log method whenever I execute get aliens that's what we are trying to do so that's your join point right so this is your join point and to achieve that we have to write a point cut oh but we have not seen the Syntax for it so what could be the syntax now to mention the syntax we have to also mention the type of advice you are working with so the type of advice is we have five options right out of which three are very famous before after and around or you can say in general I want to execute before so I will say before advice simply say add before your job will be done and in bracket you have to mention the expression so we have to go with the expression which is very simple you have to say execution because we are executing some methods but which methods so in the execution bracket you have to mention the exact method name syntax would be you have to mention p in fact you have to mention public then you have to mention the return type now in this case the get aliens return type is what uh in fact I will just simply copy the method name as it is instead of getting confused so I will say copy go back here and paste so that's my method name public least aliens get aliens but you have to mention the fully qualified name for it so okay when I say fully qualified name it means you have to also mention class name so the class name is alien controller and then you have to also mention the package name now I'm not sure about which package it is it is com. delisco do spring MVC that is the big name so I have to mention the entire part so you have to mention the package name and then the class name and then your method name so once you do that your job will done so you have mentioned two things here this is an aspect and second we have to use add before annotation so I guess this is done let's restart the application just to verify so as you can see we got an error okay it says there's no matching type as alien okay that's bad maybe you have to mention the package name here as well uh let's do that quickly oh maybe in fact you can also give Star there if you don't want to mention the entire this can be anything it can be any written type with with name get aliens let's try now okay so there's no problem now let's go back to our Postman and here let's send a get request in fact we already have here let's send that okay so this is executed but we have not oh there was an error maybe because I have mentioned the okay send can you see that we got the output but look at this code here it says get methods called and that is happening with the help of this log okay just to confirm once more I will say from aspect just to verify if this is the meth which is getting executed uh let's say send once again and you can see we got from aspect so we are right it is coming from aspect now the amazing thing is your actual code don't even know that there is a log or there is a logging happening right that's the importance of separating the concerns from the actual concerns now not just for logging we can use it for other purpose as well for security purpose for transactions we can use it in fact spring framework internally uses this concept to achieve lot of stuff okay so we have seen before we have seen aspect but how can we use after and what if you want to maintain log really log file here so we'll see that in in these subsequent videos so let's use logger here now we are doing the log on the system.out.print and right that's not the right way now to achieve log you must have done this before as well with normal Java but the same thing we'll be using here so let's create a object for logger so we'll say private startic final so the class name is loger itself let's import the package for that uh so let's take it from or. slf4j just to keep it simple and then we'll say logger is equal to now the way you can get the object of it is with the help of in class we'll say log out Factory dot get logger and we have to mention the class name here so the class which I'm working for is logging aspect so we'll say loging aspect. class now once you got loger object we can simply go back here and inste of using system.out.print we can say logger dot now we have to specify the logging level so it can be info it can be debug it can be error so in this case I will go for info because this is just an information and that's our message that's it okay but we have to also mention the file name if you want to go for and then what level of uh info you in fact let's run this application let's see if that if this works go back to postman and say send again and you can see you we have okay where is that log so you can see we got a log here which is get aliens method called from aspect now this is info level but what if you want to customize it so yes you can go to application. properties and you can mention what type of log level you need and the way you you can do that is by specifying loging dot level dot so login. lev. root and if you say equal to it should give you the popup so you can see we have all these options here so now if you say you only want to see warning then it will not show you info so I want info so let's you can specify info anyway it is coming there so you can specify in that way in fact you can also save the in external file with the help of some with the property so let we have a property we can do that so we can say logging do file and you can mention the external file uh maybe I can use app. log that works let's save this application and let's relaunch it now you can see if I say send we got the log that's great but it should also create one file which is app. log which I can't see here refresh the project it should be yeah so you can see that we got app. log and this is where you will get your log file so that's how you can use log in aop the way you use log in the last video we have seen how to use before advice now in this video we'll focus on after now when you say after it means it will execute after the execution of a particular method so in this case we are working with this method which is get aliens right so what we will do is let me create one more advice so we can copy this and we'll say copy and paste now this method name will be let's say log after okay and let's name this one as log before now the only thing you will change here is instead of having before we will making it is after and then we'll say aliens method executed so we have to import the package for this let's do that okay that's done uh let me just rearrange the annotations here once so let's relaunch the application after doing that so we got before and after now let's go back to postman and here so that's my send request and you can see we got data and look at the console it says get aliens method called from aspect that's what we are doing printing here and then after the execution we got get method executed in fact you know we are not actually confirming I mean we are not able to actually get is it really happening after the method so what I will do is before rning the statement here I will print a statement or maybe yeah we can let's print a statement and let's say we'll say fetching aliens that's it just to understand if it is happening after the execution so let's relaunch the application and go back to postman let's say send again okay server got not started okay you can see uh we are saying get meod call that means this is before aspect then we are printing this which is fetching aliens and then we are saying executed so that's how we can use after but if you remember when we have seen documentation we were having two more Afters right so one is this after so if you see three other parts so we have after returning advice after throwing advice and after finally by default it is after finally but we have this two as well okay now looking at finally you can guess if you are coming from java background where we have worked with exception handling finally simply means irrespective if you are getting the error or not this method will be executed so that means this method here will be executed irrespective of the exception to understand that what I will do is I will go back to my alien and here let me just generate an exception so before saying fetching aliens let's say if I have int I equal toide by 0 so I'm trying to create an error here just to see what happens if you get the exception let's relaunch the application go back to postman and when you click on send look at the console we got the exception that is something I was expecting but are we getting the the message so you can see log it says get aliens method executed that means even after the error this is getting executed because this after is after finally so irrespective of the error if you got the exception still it will execute the advice even if you don't get the exception it will print the advice but what if you want to print this advice only when your method is successfully executing and that we'll see in the next video in the last video we have seen how can we use after which is actually after finally where doesn't matter if you got the exception or not this will get executed what we want is we want to execute this only when you get a successful execution in that case instead of writing after we will be writing after wring that's it that's the only thing you have do nothing much I know in your mind you'll be having a lot of thoughts you know we will be doing something this or something that but it's damn simple you just have to say after returning your job will be done okay I know you don't trust me let me just restart the application go back to postman and anyway we are generating the error so let's click on send you got the same error on this console but let's go back to our real console and here of course you got the exception if you see the message here nowhere it says method executed because it was not successful right and now if I make it successful of course this will work but before making it successful I want to do one more thing let me copy this method and paste it here I want to say get aliens or maybe I will say issue now I want to print issue whenever you got an exception which is error so I will say log exception So when you say log exception now this method will be called only when you get an exception but to do that we have to change this so instead of saying after returning we'll be saying after throwing so as I mentioned we have three types of after we have after returning we have after throwing and after final fin so after returning will work only when you get successful execution after throwing you will get when you get the exception and after finally you will get irrespective of the exception and let's run this code go back to postman and here if I click on send you can see we got okay let's go back here yeah can you see that we got an issue here so issue is because we are getting the exception and that is after thrwing but now I will go back to my controller and here let me remove that particular exception now when you don't have the exception of course it will not be calling after throwing let's launch it and you can see we got normal output and here in the code you can see it says called and executed okay so executed is getting called because of log after so that's how we can use after returning and after throwing this video we talk about Spring Security Now Spring Security is a de facto standard for securing your spring based application now think about this that doesn't matter what type of framework or the language you are working with whenever you make a Enterprise level project or maybe a web based project one thing which concerns everyone and that is how do you provide security for it okay I'm not talking about the jvm level security I'm talking about the web level security example you know whatever application you have on the web now it can be a simple application it can be a very complex one where the data about your users are very important in fact overall this system is very critical so you want to secure those type of applications in a good way when I was working on my first web project the problem is I was able to complete the entire project the only problem which I facing was about the login and log out and trust me as a developer my main focus was how can I complete all the features which a user needs I forgot about login and log out and then at the end of the project what I did I added some new features that's what we do right we complete the project and then we add some extra modules now when I was working with login and log out everything was working initially but after some time you know I was getting access denied for some pages even after logging I was getting that I I have done that so we have two different type of pages right we have public Pages we have private pages and we have to make sure that public pages should be allowed to everyone even if without login but what about private Pages that's where you need login right so that one concern which I was facing the second one was log out uh when I was logging out when you go back again you were logging in so that was some issues which I was facing the second issue we have when you work with the application is passwords how will you save your passwords normally when I was working on my first application the mistake which I did was on the client side when you see a page where you can have your username and password you can simply type a password there and then you can see the password as well right and that's why we say okay make this password hidden so you will say hasht there but what about the server side I was actually able to see all the passwords okay so in fact not the actual users but when we were testing the the application when we were creating our own fake accounts we were seeing our own password because I was storing all the password as it is and then I realized I did some research I realized you should be encrypting your passwords before saving it but do you think encryption is safe people can hack it right so if you can hack the database if anyone can hack your database they can actually decrypt the passwords in fact they can do it with the help of Bruce Force attack or something they can decrypt your passwords then we can go with the stronger one right instead of going for a basic encryption maybe we can go for hashing we can go for sha 256 because md5 was something which was obsolete at that point but I was using sha 256 and then I heard a news which is they were able to create two hashes of the same message and anyone can hang it now right so even sha was compromised then we had a new Option and that is bcrypt now bcrypt is a algorithm which is specially made for saving passwords okay so that means when you create the application you have to provide login log out you have to do all the code in fact for every method which you call for every s which you create okay you can translate with other applications as well other languages as well so for the subet for every subet we have to check for the username and password we have to wait for the sessions and everything has to be done by you what if you want to use l app what if you want to use the new Google one which is au 2 for everything you have to include libraries you have to do lot of configur and that's where spring says Hey developer you don't have to worry about all these things you want log in I will help you you want log out I will help you you want to use bcrypt I will help you in fact in Spring boot security we already have a bcrypt library what if you want to use l app spring says I will help you there okay when I say spring will help you there that doesn't mean spring will give you everything by default you have to configure it okay spring will give you those features but then we have to do some configuration there okay when I say some trust me it is some configuration because if you do that by yourself you need to create multiple files and you have to die doing that that was my experience when I was when I was working on my first few applications in fact I did well when I was working on a big project but everything has to be done by V developers okay we were five to six people and then everything was so difficult to manage I'm not talking about the features I was talking about the security okay uh so spring will help you there but the moment I say spring will help you that doesn't means spring will give you everything there are certain things you have to do by yourself you have to configure it so when you do this for the first time it will be difficult because we'll be getting a lot of files we'll be getting a lot of functions but that's the thing right you will be doing this only once you will not be doing this every time because you will build the project only once and while you're building the project you will do all this setup you know day by day you will keep adding new features you will not be doing the same thing again and again so that's why Spring Security is amazing it provides you login log out it provides you oh we have forgot about one thing it's not just provide you authentication it also provides you authorization which simply means authentication is who are you your username and password but what about let's say if I talk about web application where a user can read the articles a content creator can create the article and they can write the article the admin is responsible to create new users admin is responsible to create new content writers so here everyone have a different role right so as a user you can only read the you cannot modify it as a content creator you can edit your own content you cannot edit someone else's content as an admin admin can add content admin can remove content everything how about those rules and that can be done with the help of authorization and Spring Security will help you in both authentication and authorization but how exactly to do that that we'll see in the Practical video this video will try to implement Spring Security as we have seen in the theory video that we can use Spring Security to secure our spring based application now it can be the interace application it can be a desktop application or it can be a web application in this example we'll try to implement a web application and we'll try to secure that now first of all if you say you want to secure it of course you need a web application here okay so I had a I had a choice of making the application from scratch and then implementing security or we can use the existing application and securing them but I thought you'll be watching this for the first time so let's go with step by step so let's create a project in so I will do I will be doing this project live here so let me create a simple project so I will say new and then we will be using here spring boot okay will not be working on normal spring because it takes a lot of configuration and nowadays most of the projects are built in Spring boot so let's go for spring startup project and here uh you can mention your name here so I will say this is my secure app of course you can name anything doesn't matter and then let's give a package name as com Tesco you can have any package name doesn't matter and here as well okay so this package name should be com dot thco dot demo no not demo but secure app okay now once we got that let's click on next now here what are the things we need now first of all we have to specify this spring boot version now this version is working perfectly so let's not change this and then here we can search for certain things example we want to make a web project right so we'll select web that's one thing we require next thing we require is jpa because we also need to work with database as of now we don't need that but later on we'll be needing that okay let just not do that now the next thing we need here is uh security but then we'll do that later as well so we can select that here you can say security or you can add that later manually let's click on next and click on finish now once you got your web project you can see on the left hand side we got a project which is secure demo or secure app that's our project and you can see we have all dependencies added here now once we got them let's create a simple application now first of all whenever you say you want an application you need a controller right because the query which I'll be finding now is okay this is the application which we got but when we want to call this methods example if I go to my browser here if I say the port number by default will Local Host colon 8080 if I say local colon 8080 you you can see it is showing nothing right we got refused but even if you do that and if I say get aliens I want to get details about all the aliens because I feel programmers are aliens right because we live in different world if I click on enter you can see it will not return anything we want this we want it to return get aliens okay number of aliens let's say three to four aliens so then their ID their name and their let's say current technology they're working on so if you want to implement that let's go to our thing let's create a home control controller of course why you need a controller to work with this so let's create a home controller because we'll be having something to handle the home request so that's our controller and I'm not I'm expecting that you should be knowing spring MVC a bit so we'll have to say controller here that's one thing and then here you have to mention certain methods the first method you need is public string which will return the homepage right I want to return the name of the homepage so I will say home and here we'll simply return we'll say request mapping and this will be for so whenever you call a homepage it should return the homepage oh but unfortunately we don't have a homepage we need to create one so we'll simply return we can also return index.html or index.jsp or home.jsp the page name doesn't matter you can have any page name just make sure that you mention that page name here so we are saying home.jsp but you can see we don't have Pages here so let's go to our main and inside main so you can see in our SRC we have main inside this main we need a folder so let's get oh not this one let's create a folder name as web app so we'll say folder web app this is where you have to put all your JC Pages we can also use theme Leaf here that's your choice again and here in this I want to create a JSP page which is named as home home.jsp now in this page home.jsp I want to print welcome aliens that's it I just want to print welcome aliens so whenever I call home.js Page should print welcome aliens let's try this first right click here say run as I hope it will work let's try so I will say run as spring boot app you can see it says started let's go back to our browser and here if I say enter oh we got an error you can see it says download that's weird right we don't want to download a page the thing is spring boot by default doesn't work with JSP right you have to add one more dependency to make it work with JSP don't worry you will get the source code in the description area where you will you can directly download the code but if you want to work with JSP with spring boot you need to add one dependency so for that we need to go to m repository and this is where you will search for Tomcat Jasper because this is responsible to convert your JS pain to seret and we can pick up any one let me just go with this table one so let's go with 8.5 point this one this should work I guess let's paste it here and you can see in fact there's one way you can do that you can actually check the Tomcat version you're working with so you can see the Tomcat version which we are working here is 9.0.4 in fact that should be the best option right let's go with the suitable one so that it will not create much issue later let's format it okay now once you got this Tomcat Jasper it takes some time to download okay so once we got our Jasper let's restart the application once again and now if I go back to the browser let's go back here and say okay I want to call the homepage and you can see we got welcome aliens at least this is one working now we want to have one more page which will show all the aliens okay and I know we are going for a lengthy code but then this will make much more sense later in fact let's not do that now let's say I want to secure this homepage okay let's do that let's Implement Spring Security here what I want is whenever a user goes to the homepage like the moment I say enter I want to make sure that the user is logged in I know when you call homepage anyone can call the homepage right there's no usern and password for the homepage but at least for this application I want to secure it I want to ask for the username and password for this homepage if you want to do that the first thing you have to do is in your controller instead of calling home.jsp you should be always calling login.jsp where a user will enter username password that's one thing you have to do the second thing you have to do is once a user is entering username password you have to verify it okay we'll do that the next thing you have to do it you have to also maintain the session there a lot of steps you have to do right instead of doing that what if I say you can use Spring Security here which which will give you all those features what I'm talking about is let's go back to our pom XL file and this is where you just need to add one dependency trust me that's how it works uh let me just prove my point what I will do is if you want to get that login form okay let's go back here and search for Spring Security Boot and you can see this is the one we have to use again the version you can go for is 2.1.2 let's copy and paste in fact you don't even have to maintain the version because because that's something you have defined in the global scope here let's format it once again and you can see we have added Spring Security as well now once you have added it here let's relaunch the application let's see the magic just by adding that Spring Security let's see the magic let's relaunch it let's go back and now here if I say refresh can you see that the moment I refreshed we got a login page we have not created any login page in our appli if ation and that's the beauty of spring boot or spring boot security Now one thing is amazing here we are securing the application everything will be maintained by Spring Security we don't have to maintain the session or something like that okay but then what is a username and password I don't know let's try so by default the username is user so when you talk about Spring Security the username name is user oh but what is the bful password is it ABCD or 1 2 3 4 so if you go back to your console let's scroll can you see that we have a security password which is generated here so of course this is not a recommended way of doing that this is just a static password which you got uh but just for the time bin we can try it so you can see it says user and username and password let's click on sign in it worked can you see that we got welcome aliens so if you have 10 pages you know if you try to access any page now we don't have any other page but let's try with get aliens you can see it says oh we don't have that page right that's the issue but then if you want to access the application you have to provide login and password now I know there's one question in your mind of course as a user you will not remember this password what if you want to create your own account of course we can do that what if you want to fetch data from database of course you'll be having a table there which will have username and password so let's say if your application has 10 users every user need to have a different username and different password how can we do that even we can do that so we'll see that everything in the next video we'll see how do you change the usern password because we don't want to work with this user and password which is specified by the Spring Security what if you want to have username as naven and password as 1 2 3 4 can we do that so yes in the next video we'll see how do you change the username password which is in memory maybe after that video we'll also see how do you fetch data from database okay so we'll do that everything but in the upcoming videos that's it from this video which where we have seen how do you create a simple spring application and how do you add a spring security module there welcome back aliens my name is D ready and in the last video we have talked about how can we Implement Spring Security but then we have just added the jar file right and when we ran the application we were getting this login page the username for this was by default user and the password was generated by the application itself so if you can see we got the password and when you copy this password let's go back to our thing and say enter you can see now we can access the application you can see we got welcome aliens but then we don't want to have the system defined username and password what if you want to have your own username and password and that's where you need to do some configuration now what configuration talking about now when you talk about spring application if you want to configure something we can use anotations or we can create a class where you can provide the configuration so let's create a simple class class here and we'll name this class as application security config again you can have any name doesn't matter uh so I'm going for app security config let's click on enter and here if you want to do the configuration we need to extend a class here which is named as web security configure adapter you just need to extend this class so that you will get all the configuration features now since this is a configuration we have to write at configuration which is the which we normally use for the configurations that's one thing we need to add one more annotation by specifying unable web security which will enable the web security for the application so we'll simply say enable web security that's it we have to add this to annotation now once we have done with this we need to override two methods in fact let's go with the one method itself and that method is so let's say Source override and the method name is is user details service so this is the method you have to override click on okay and this will override that method now we also need this object of user details service in our application and that's why you will simply write here at bean I want this to be a spring Bean okay but then we don't want this return stuff let's create our own because we want to have a username and password which I will Define of course you can fetch this username and password from database but time mean let's go with in memory username and password to achieve that I will simply create a list of user details now user details is a inbu class in Spring okay first of all I have to import list and then I can import user details now you can see user details is a inbuilt class in inbuilt type in Spring Security and now once we got let's say users equal to but then okay let's create a add list for this for all this us users but then question arise how can I create users now if you want to create users it's very simple right we can simply since it's a list we can simply say users. add but you can see when you say add it is asking you for the object of user details I don't have any user details here so what we can do is we can use a inbu class name as user so we have a inbu class named as user so it's say user Dot and you can see we have a method which is with default called password encoder of course right when you work with the applications where you want to store the password your password should be encoded right it can be a plain text or it can be a encrypted text so time in will go for the default password encoder which is actually not recommended to go for but for time being it will work with then we'll say with a username so we have to use a builder pattern here so we have to say username and the username I'm going for now is naven and let's say I want to go for the password as well again this is not a good way of doing it just for the experiment we are doing it just for to understand how it works behind the scene uh so we have user dot with default password do username do password dot I can simply say build now what it will do is it will create the object for you but I can see an issue here yeah it says this method which is with default password encoder is replicated but that's fine again we we should not be using this but we can we will see that later how do we remove that how can we use some other uh encoding type but as of now it will work this is how you define your username and your password but we still have issues now see spring is not just about authentication right it's also about authorization how can you specify your role in the application now you might be a admin you might be a user so here we can simply say dot I can also specify the roles you can see we have an option of roles where you can specify the list of rules I can go for time being let me say this is just a simple user so I'm saying username naven password 234 and the rule is user and then we can simply say build and then once you got this once you got the object of users we can add multiple user okay we can say users. add with the same syntax we can say some other name some other password and some other rule we can add multiple users of course right every application has multiple login and now I will simply return the new but then what should I return now since we wanted to return the object of user details service which is not using any database here so we are using in memory database or not even database we'll say in memory there's a class which says in memory user details manager this is the class we have to use in memory user details manager and where you have to return the users that's it so this will take care of user details service now I hope this will work hopefully let me just relaunch the application so this time we'll not be using user and the password generated by the system to login and you can see we we are not even getting the default password now let's go back to our Chrome and here okay first of all we have to relaunch it so let me say Local Host I mean Local Host colon 880 and now it is asking for the user and I will say password or not user because let's say if you try with user and some weird password you can say it says bad credentials we want to say naine and password is 1 2 3 4 let's click on sign in and it worked can you see that that's how you can have your own username and password but hold on we don't want to use this we want to use database right I want to fetch my username and password from database how will I do that that will see in the next video so I hope you are enjoying this series let me know in the comment section and do subscribe for for the videos welcome back everyone my name is D ready and in this video we'll try to implement Spring Security where the username and password will be verified from database in the earlier videos what we have done is we created a spring application in fact spring boot web application where we tried to implement Security in fact initially we were getting the user and the password from the spring itself but then later on we thought why can't we have our own username password and we have done that here but now I want to fetch this details which is naven and 2 34 whatever username is there database I want to verify that from database in order to achieve that first of all I don't want to give my specific username and password I want to Fest this from database so what we'll do is we'll first of all we'll comment the entire part we don't want to use this then how can we achieve that security so the steps are first of all you need to create one method which will return the object of authentication provider this is what it will return and the method name can anything we can say Au provider okay that's the method we have to create now this method has to return the object of authentication provider so let me just import the package for that that's done and this will be a bean because we want this to be a spring bean and now once we I mean we want this to be spring B which it returns now what I want is I want to create the object of it right now when you say you want to connect with database we have a special thing which we normally use for database which is Dow da basically stands for data access object so let's say Dow so there's a implementation for authentication provider which is Dow authentication provider let's use that uh we'll say provider we simply have to return this object right so we'll say provider equal to new Dow authentication provider that's it we just have to get this object and here let's return that object so let's say return provider now will this work of course not right we have not return any code which will connect with database of course this is responsible to you know talk to database and verify it but we have not done anything as of now now first of all we need certain things for example if you want to work with this type of application in your P we need to have database properties which we have not done till now okay how do we achieve database properties here so it's quite simple we need to add boot right I mean we have to add jpa so what I will do is you can just download this Li is from MAV repository just go to Google search for MAV repository and you can search for the repositories which I have otherwise let me just copy paste which I have with me so I have added two dependencies here the first one is for data jpa because we want to use jpa here and then of course you have to also mention the database connector which is in this case I'm using MySQL but hold on have we configured the database in MySQL so before recording the video I have created this database here which is the Disco and you can see we have a database which name as telesco of course you can use any dbms which you want not just MySQL you can use post you can use msais whatever database you prefer I prefer Mas so you can see we have a a database name theis and the table name is user now if you see this table user we have four records just ignore the first and second we will be using this later concentrate on this third and fourth so we have a username as spyman and the password is web the username is Superman and the password is fly okay just random username and password here and okay so we are using MySQL the second thing which is important here the username to access MySQL is root and the password for this machine is 1 2 3 4 So based on the machine which you are working on of course you you will be doing some set up for MySQL remember the password for this my the password is 1 2 3 4 let's get back to this and so you have to specify this two two things and in order to connect your database we have to do some configuration for application. properties these are the properties which you have to add so first you have to mention URL which is uh the Risco in this case you have to mention the root username and password and then you have to also mention driver class name for MySQL as your dbms will change it will change the url of course whatever username you have whatever password you have and it will change the driver class name as well okay once we have done with that we need to have a model class which will be connecting with a database I mean of course a class which represent a table in this case since we're using jpf to do that so in this case let's create a class named as user and in this class user I want to have three properties the same properties which we have for database so if you go to myol workbench you have ID username and password let's create these three variables quickly okay so we got three variables and we need to have Getters and Setters for this right click here and we say source generate get and sets I will say select all click on generate so you can see we got the fields and we got gets and sets here I want to make sure that this will connect to database I have to say this is entity let's import the package done and then this ID has to be a primary key so I will say at ID we don't have a choice right we have to mention that it's compulsory now once you got this class which will connect with database I mean it will represent the database table now what next let's go back to our app application security config as I mentioned this is not complete yet so what are things you have to specify here so one more thing we have to specify here is every provider normally what happens you know whenever you work with the applications uh which will connect your database so your controller or your configuration will be interacting with a service layer and then this service layer will connect with the database layer that's how we normally work with so controller talking to service service talking to Da uh in the same way we have configuration talking to service service talking to Da okay so that means we also need a service class here and to achieve that in fact we'll say provider dot let's mention our service provider so we say set user details service that's my service name okay but then we don't have the object of user details service okay that means we need to create the object here so let's go back here we'll say private user details service let's import the package for this and then let's have object name as user details service itself and this has to be Auto wir I I will not be uh creating the object for this it has to be autowired that's one thing we have to also mention one more thing which is the password encoder if you remember we have done that here as well uh we have to do the same thing for this so we have to say provider dot we have to set the password encoder so we'll say password encoder now but which one uh time in I don't want to use any encryption technique or any hashing concept I will simply use use no op password encoder do get instance uh because I don't want to encode now yes I know this is not something which you should be using never save your password in plain text but as of now we are just experimenting stuff so it should work now once you got this object everything should work but hold on we still have one issue we have got the database we have got user class but then this user details service how it will know to connect with a dow layer we don't don't have this class right let's create a class which will represent this so what I'm talking about is let's create a separate class and I will call this class as my user details service a very simple class and this will Implement an interface the interface which we want here which is user Detail Service so that once you click on finish you can see you will get the ready stuff everything you don't have to override this method by your s this is just the feature of eclipse okay now once you got this load user by username you can see it something to do with database uh we have to it also throws the exception let's see how to use that but how will you get this username and of course you have to get this username from a repository if you have worked on jpa before this is what you do right you create a separate repository class or interface for every table you have okay that means we have to also I know we are creating a lot of files but it will all make sense once we complete the entire code let's create one more interface which will represent the user repository so I will say user repository which will extends jpa repository and click on finish you can see we got user repository extends DP repository and in here instead of having T we should be saying Hey I want to work with user where the type of your ID is long that's it you have to mention user and long of course if the classes which you created in separate package need to be imported for sure okay but which method we have to use here see by default you will get all the methods for find all find one but we want a separate method which is if you go back to our application security config you can see I want this class so you can see we want to find a user by the username so that means we need to have a special method here which is which would return the user and it should be find by username it will accept string username that looks good we got this interface and it has a method which is find by username let's get back here so here I want to call that method but then to have that method with us we need to create the object of user repository right so we'll say user repository and we'll say this is repo of course you can have any name on top of this you have to say at autowire because I don't want to instantiate it right and here you can simply say user user is equal to because this repo when you say repo do find by username you have to just pass username so whatever username you got here will be searched in database and it will return that particular user if what if you don't find the user of course right uh when you log in it is possible that the user which you're searching for is not there in the database so in that case we'll check if user is equal to equal to null we can simply print a message user not found that's one way or the other way is as you can see this method throws the user not found exception can we simply use that let's say through new username not found exception we can do that right and we can print our own message we can say user 44 which is user not found of course we can do that right just for fun okay uh this is done what if there's a user what if there's a user in that case you need to create oh in that case what you will do because you can see we have to return the object of user details oh that's tricky unfortunately this is an interface okay how can you R the object of it that's bad right so that means first of all we need to create a class which will implement this method which is user details okay we still have one step to do so let's create that class okay I will repeat every step don't worry so let's create one more class here and that class we can have any name to it we can say user details implementation or we can simply say user principle okay why I'm using user principal as a method name or the class name is because principal simply means the current user okay so let's have that this will Implement a interface which is user details let's pick up that and click on finish and you can see we got a class which is user principle which implements user details and it has all this methods time being since we are not implementing you can see we have method like is account not expired is account not loged is cren shs not expired so we have all these things which we have not configuring now so let's keep it true by default in case if you want to have database Fields there you know because let's say after every 30 days you want to change the password so you can do that in this method but as of now we are not doing anything else okay but still we have to say get password and get username because that's that can not be true uh in this case you will say do we have the object access here we want the object of user right which we are working with so in that case what I will do is I will create a Constructor because I want that object anyway I want the user object here so I will create a Constructor with a field but which field I'm talking about so I will say private user user Which object I'm talking about so if you go to my user details you can see we have this object here right which is user I want to have this access to this object in this particular class and the way you can do that is by creating the Constructor I will say Source generate Constructor using fields for the user click on generate we got the Constructor it should work now so with that object user I can simply say user. getet password and or not here here we'll say user. getet username that's it we just have to write this methods but we have one important method here which is get authorities this is where you have to return the collection of authorities uh that's weird to achieve that we can simply say return I want to return a collection right so I will simply use a collections class I want to return all the unique collection so I will say Singleton collections. Singleton I may pass a list here but time when I just want to return the users so I will say new simple granted Authority and the role which I want to focus now is only user I want to focus only on users now uh let's do that we don't want this return statement and that's it our job is done I guess here oh I know we have return so many methods so many classes okay once we got this user principal class let's get back to our code which we are writing we don't want to return null we want to return the user principal class which accepts a user as the object you can see this user which which I've created here I'm passing it here so that it will go to user principal and it get assigned here and we can get the username and password everything looks cool I think our job is done let's get back to app config everything is done yes it is implemented this is auto so this will be getting connected with my user details service object hopefully this will work let's restart the application and of course even if you get the error that's where the learning starts right we should be getting errors okay and we got the errors okay I was just joking and we got the errors uh the error is I'm not sure what this error means it says uh found being not able to inject the object for user details service but why oh I forgot to mention one important thing here we have to write add component or add service my bad let's relaunch this okay now there's no error as you can see everything is working let's get back to our browser and here I will search for local host called 880 it will ask for the login and this time I will use weird password and something let's click on sign in you can see it says bad credentials and in the console I guess we have not got anything in the console that's fine let's get back here and let's enter the actual usern name so I will say Superman fly that's the password fly is the password let's click on sign in and it worked great you can see that it worked we got welcome aliens so this time it is accessing the data from the database and that's the beauty of spring boot security let me do the recap what we have done see whenever you talk about security it will first check this file this file is very important when it comes to Security in which you are creating a method which is or provider where you are fetching data from the database as you can see this is for database code this user Detail Service is responsible to fetch from database right let's go back there and you can see here we are creating the object of user repository this is the interface which we have since we are using jpa so we don't have to implement this class or this interface jpa will take care for that and then in this we have a method which is load user by username where you got username uh we are fetching the data if the user is not available then you will simply throw the exception which is 44 this is fine but then we are rain the object of user principle so we need that class as well so you can see we have user principle which is which where we have the user object it is accepting the object which you are passing from the the detail service and then we just want to return all the users and that's it nothing fancy we got a user class as well everything is simple uh that's how you are able to run this application I hope you are enjoying this series let me know in the comment section and do subscribe for further videos and also let me know what else you need in this score so that I can try to make a video on that that's it everyone bye-bye welcome back everyone till this point what we have seen is we implement mented Spring Security of course using spring Boot and then we were able to log in with the help of username and password which is fetched from database to achieve that we have used an authentication provider in which we have created the object of Dow authentication provider and then we have assigned the user details service object and then we have used one thing here which is the password encoder now the amazing thing is we have specified it is no op password encod of it simply means I don't want to encode my password it will be in plain text and if you go back to your database which we have already used you can see we have a username as Spider-Man and we have a password as web now is it a good idea of having your password in a plain text of course not right if anyone can hack your website or anyone can hack your database they will be seeing all your user passwords we don't want that at least your user details should be secured so instead of using no op password we should be using some encryption way now of course you can use some encryption technique here and you can convert your normal plane text I mean the plain text password into a secure text something like that let's say cryptography or Cipher text now in this case the problem is even if you go for Cipher text it is not a good idea right because anyone can decode or anyone can decrypt so they can use certain techniques and there are something haing ways so yes it is not safe to work with uh Cipher text what about hashes now one of the way you can do that is with the help of sha because in sha doesn't matter how big your password or how small your password is the output length will be constant that's not the case with cyber text right so we should be using hashing here but which one should we use message digit 5 of course not that's very old uh we can go for sha 256 or sha 512 but then you know few years back there was an attack on SJ as well so hackers were able to crack it or hacker were able to create some Collision codes so what to use now now specifically the problem with s we can have multiple computation or multiple calculation happening at the same time you can find Sha but what if you can make it slow what if you can have multiple rounds of hash because if you have multiple rounds it will be easier for you to protect it and to achieve that espcially for working with passwords we have a New Concept which is known as bcrypt so if you search for bcrypt online it says bcrypt is a password hashing function designed by these two great people and then it is based on bluish Cipher that's great the thing is it came because sh was not that secure if you read the article there are so many articles on this how to use it in fact we have some website where you can generate the hashing example if you go to this website which is browse Ling and if you type a password let's say if I want to Crypt 1 2 3 4 instead of saving 1 2 3 4 in the database we should be storing the hash of it so we can simply click on bcrypt here and you will get the output now you can see there's something interesting here which says 10 now this 10 simply means it is going for 10 Rounds by default just 10 rounds but you can also make it 12 rounds if you say 12 it will calculate the hash 12 times and if you say Big R now you can see the the code will also change so this dollar2 a means it is for BPT then we have do 12 is the number of rounds you have and then if you go forward this is your actual password okay of dollar so this is your actual password but in the database you'll be shorting everything uh if you try for ABCD here if you say bcrypt what will change is the next part right this thing will remain constant let's go for 10 Rounds here and click on BPT and that's your password for ABCD in fact if you go to database you can actually match can you see that at the end okay we got a different password this time yeah this is but that's fine you can use this one as well so what I will be doing here is you can see we already have two passwords here but I'm not sure is it 1 2 3 4 for which one I forgot the password is it 1 2 3 4 for nav let's try so what I will do is I will try to use bcrypt so this to is actually bcrypt passwords which I already have it in my machine so let me just go back to my workbench and here instead of mentioning no op password encoder we should be using bcrypt now you might be thinking we have to add some libraries right don't worry by default in Spring boot we have a library for bcrypt let's try so we can simply create the object of bcrypt here so we can save new bcrypt and you can see we have bcrypt password encoder let's click on that and you can see it has also imported the package here which is BCP password encoder that's great now we don't need this one so we can say control shift o which will remove all The Unwanted you know which will arrange all your inputs and that's it that's the only line you have to change it's that awesome right now let's relaunch the application now of course one thing will not work the password for Spider-Man and Superman will not work because they are in plain text so let's try for naven and uh K here so we'll say Local Host col 880 and of course it will give you a signin page this time I will say naen and the password I guess it is ABCD for naen let's do that and when you say sign in can that it is working so the password which I have stored in the database is this one but I'm just trying ABCD is working let me just do that once again so let's say Local Host call 880 login and we'll say naen if I use some different password you can see it says bad credentials and if I strive for Spider-Man and if I say web this will not work now because the password is stored in plain text so in database as well when you create the user account you have to make sure that you store the password in a bcrypt encryption format or bcrypt hash formats uh that's about bcrypt in the next video will also see how do you customize this form page because I don't want this login form of course this is good but this given by Spring right what if you want to have your own the login form can we use that so that that will see in the next video I hope you are enjoying this series let me know in the comment section and if you and also click on the like button thank you so much welcome back everyone till this point we have tried to secure our application with the help of Spring Security and then to achieve that we have created multiple configuration in fact we have done with this class where we are saying hey you have to verify from the database with the help of this class here and then we have also changed the password encoder from no op password encoder to bcrypt so that we want to store our password in a secure way that's done right but if you see the output which you used to get so if you go to that Local Host part so if you type Local Host colon 880 you can see we got this login page the amazing thing is we have not created this page this page is given by Spring framework now of course right if spring is giving you something it will give you something very simple you can see this page is damn simple but can we customize it unfortunately I don't know the way to customize this there might another way but what if you get the option of creating your own form of course right when you have your own form you can have extra features you can add anything you want right you can change the color scheme as well so what if we can create our own login and log out page page so what I will do is I will just go back to my application and before the video itself I have added two pages here one is login and second log out now you might be thinking it will be a very complex page don't worry I just kept it as simple as possible you can see this is a very common page very simple page here which has a form tag which is calling login and then we got username and password two Fields just it both are input field and then there's a button that's it nothing complex here there's one line which I've given here again we'll talk about this later as of now there's some Spring Security message there that's simple right the log out page is a page let's say after logging out you want to show some page that you have successfully logged out and then you will also give an option of calling the homepage okay that's very simple page which I have created but then will this work of course not right when you run this application by default spring will have its own login form so we have to inform spring framework hey I don't want you to give me a login page I want want to have my own login page how do we customize that in order to achieve it let's go to app security config this is where you have to create a method where you can specify all these configuration let's do that quickly now instead of creating that method by yourself we can actually override it so there's already a method which is there inside web security config adapter so let's override that method so I will go to source and I will say override a method and the method name here is configure which takes http security as the parameter let's click on okay and you can see we got this method as of now this is only calling super we don't want to call Super config I want to have my own configuration in order to achieve that we can simply use this object which is HTTP and then we can set multiple properties here so the first thing I want to do is I want to disable csrf which is CL site reference and we can simply say cf. disabled that's one thing now we have to mention that we want to go for a login page so we can you can simply say dot here and we'll say form login now anybody want to have your own form you simply have to say form login in which you have to specify the properties now of course right you have not mentioned what is your login page name that's very important so we can simply say login page and here you have to mention your login page in fact you don't have to mention the login page we have to mention the request type or request name in fact we are working with spring right so spring will have its own MVC so you have to mention whenever I want to log in I can simply call SL login which will call a controller which will handle the login page okay so we have to say login. permit all because I want to call this page we have to also mention what happens when you say log out we have to also work for log out so we can simply say here log out because we want to achieve log out as well so I can say log out dot there's a method which is invalidate HTTP session I know if you're coming from background of you know working with s JSP everything has to be done manually right so here we don't have to worry about that we simply have to say invited session everything will be taken care by Spring framework or spring security and then we have to also clear all the authentication yes we'll simply say true once you're logged out of course you have to clear everything and then what is a request type so let's say when do you say a person is want to log out maybe it is slash log out it is slash out or slash get out anything can be done right so we can simply say log out request matcher so on which request you want to do that so we can simply say and PA matcher and in this you can specify the URL so I want to say I want to log out when I say log out that's it you can give any URL here it actually doesn't matter and we have to set one more important thing what after you say log out you have to call some pages after log out so I can say whenever I call a log out so we'll say log out successful okay something went wrong here oh okay so we have request matcher my bad let's call this class and here I will say log out success URL okay now which URL I want to call so let's say time and I will say log out success that's it so it will call log out success or you can also call homepage that's your choice and I will say permit all that's it this is the thing you have to enter here so we got a login page we got log out page and we are doing it here but will this work of course not right when you say you're calling a login and log out or you have to also change something in the home controller right because we want to handle the login logout so here we have to add those two methods so I will say public string and I will say uh login page okay so we'll simply say login page and it will return in double codes we'll simply return login that's a login page we have so we already have a login page here and then we just have to add a request matcher for this method this particular method here and here I will say login simple and then we also have to work with log out success because we have to call the log out page I will simply copy this since I'm lazy I will say this is for log out of course I will say log out hyphen success and here I will say uh log out page and then I I want to call the log out page here so I will say log out that's it simple page I think it will work now everything looks cool let's relaunch the application the moment I say I can't see any eror we got eror it says permit only works with HTP authorized request oh we have to do that as well so let's go back here and I guess we have to add one more so I have to say do authorize request. ant matcher I will say whenever I call login it should allow that let's try with this permit all and now since form log is different so we'll simply say and there because I want to specify more properties there okay it's working there's no error let's go back to our page I will say refresh and we got an error it says circular path we are not able to find login oh we have to mention the page right so I have to say login.jsp and here as well have to say log out. JSP so you have to mention the extension as well let's relaunch it and now let's go back here I will say HTTP colon 880 enter and you can see it says welcome aliens that's bad I guess the maybe we have to also say any request has to be authenticated because we have not mentioned that so let me do that once again I will say Local Host col8 and now you can see that we got a login page this is our customized login page which we have and now I will say naven and ab CD when I click on submit you can see we got welcome aliens is that simple but we don't have the log out there so we wanted log out as well so when you say log out I just want to change one page in fact uh where the page we have it says home.jsp let's also have the log out button here so I will simply say button here we say AHF or a link we can say/ log out so whenever I say log out it should log out that's it normally we should be using some CSS of some format but as of now it should work everything done let's go back here refresh and now if you can see if I say naven and ab b c d if I click on submit you can see it calls the page and when I click C on log out everything is logged out you can see we got log out home in fact even if you go back you will not see the same page in fact you will get login page that's awesome now if fory enters some wrong username password when you click on submit you can see we got bad credentials you got this text here because in the login page this is what you have written right you you say Spring Security last exception this is responsible to print that message bad Curren shs that's the only importance of this line so that's about this video where we have Tri to work with login our own login page and log out page the only thing you have to do is you have to add these two pages plus you have to go to app security you have to override this method which has all these properties nothing much everything will work in fact yeah we have one more thing we have added two more methods here so that's it from this video I hope you enjoy let me know in the comment section and do subscribe for f the video Welcome Back everyone till this point we have seen how can we Implement Spring Security the we have tried form login we have tried with database but now I want to use oo to so let's say if you are building a website where you want to provide security you want user to be logged in of course you can have your own database you can have your own way of verifying the user but we have one issue when you have your data I mean when you have user details in your database in your server you have to prict it right of course it becomes difficult to manage that database and if someone hacks it you are responsible that's one issue the second issue is even if you have your database even if you have your own server most of the users they are using some type of social networking login example if you have let's say your consumer has a Google account they have a Facebook account so can we have Google login in your website I mean you don't have to manage the data right it will be managed by Google so how can we do that to achieve that we have this amazing feature known as O2 so what I will do so let me search for O2 so what exactly O2 is so you can see it says it's a authorization framework that enables application to obtain limited access to the user account okay so it's not just Google we can also use it Facebook GitHub dig solution so we have so many options here you can see you can get more details about what is or2 how exactly it works behind the scene but now we want to use it so we will be using Google login to access the website okay in fact we'll not be doing the entire code here the main focus would be how can we use Google login and let's access the token so once you log to Google Google will send you a token I mean for the website and then based on that token your website will give you the access it's that simple okay so to achieve that what we'll do is first of all we need to say hey we want to use o or2 and for that we have to add a library so what we'll do we'll expand this and if you go to pom.xml we need to add one more dependency here for the O2 so let's search for MAV repository and here we have to search for Spring Security O2 autoconfigure now before spring 4 or spring five you could have actually go for spring boot security or two but now since after spring 1.5 or spring 5 we have to search for autoconfigure and here you can pick up any version let me pick up 2.1.2 that should work and here let me just copy this go back to our code and paste that dependency now once you say save it will get downloaded from the internet and it will be available in your M dependency so that one step is done now once you got this you have to make some more changes so let's go back to app security now don't you think we are using o or2 so we have to specify it somewhere so here we'll say hey I'm using O2 so please enable it so you will say adate enable go2 SSO so that's you have to add so this is SSO here is a single sign on so you can see we got the package as well so once you have done that now here we don't need user dat because we are not using database so let me remove all this extra thing which we don't need as of now the only method we need here is configure now in that to we don't need to go for form login of course in the previous video we have worked on form login but this time I just want to use Google page right so here what you can do is you don't need any of this stuff so we don't need form login let's remove that the only thing we need here is Dot HTTP basic so that's the only thing you have here so we got the and matcher and then we got HTTP basic so you have to say whatever request is getting that should be done with the help of O to so you don't need database anymore in fact just to clear some stuff we don't even need this method anymore so let me remove that just have a clean code okay in fact I think we don't even need this HTTP basic if required We'll add that later now think about this we are saying hey use o to s so and then it will search for the or2 token but we have not configured anything here right so don't you think we have to specify it somewhere we have to specify the configuration are we use using Google are we using Facebook even if you're using Google then how do we because see every developer needs a developer account on Google right if you want to have that access you need to create a client ID and a client secured key so what I'm talking about is you have to configure it in a file so this is our file which is application properties this is where you have to configure your or2 now what are the configuration we need so I already have a text with me let me just past it here so these are things we need so this is only for O2 so we are saying hey O2 client access URI here is Google so we are saying we want to use Google for my login this is your token name which is or token if you want to use in your application now this is important the scope when youbody who use O2 of course it will also give you authentication but don't you think you have to also specify what are the features is accessible example whenever user access using Google they will be having the access to the profile of the user and the email ID in fact if you don't want profile just remove that you can now you're only accessing email ID let me just have it there okay this is something we have to specify which is for Google and then we are using o token but this is important the client ID and client secur key oh we have not specified that yet because depend upon the application for different users we I mean for different developers we'll be having different client IDs here so how will we get this client ID and client secret so what you have to do is you have to create Google Cloud account where you'll be creating the application for this client ID and client secet so let's do that let's search for Google Cloud so this is their official website and here you have to click on go to console because I'm already logged in and I have a Google Cloud account so if you don't have you can create one the only thing is I'm using this for a personal use and that's why you can see I'm out of trial but that's fine if you have a Google premium account you can use that or otherwise you can go with the free one now here you have to click on this navigation bar and you have an option of API and services this is where you have to click on credentials now when you click on that you can see we have the second option second tab here as o o consent screen this is where you have to specify the name of the application we already have one because I've used it for one of my batch but now let me just change it to teliscope nothing to specify here you can see we have specified the scope here email profile open ID you can add scope I guess you can also delete scope if you need uh no we don't have delete option but you can add scope nothing to specify much here click on Save now once you click on save you can see we have an option of create credentials click on that here we have to create a O2 client ID when you click on that it will give you an option you can use it for web application for Android for Chrome T and iOS let's web app here and let's give a name of course you can go with the same name I will say teliscope app and this is where you have to specify the URL of your application now since it is on Local Host I will simply say HTTP colon Local Host colon 80 and the redirect link which I want is on the login page I will say HTTP colon SL slash Local Host colon adad SL login and then click on okay so enter click on create so that should work now once you click on create you can see it will give you two things it will give you uh the ID and it will give the secur key okay let me just copy that so this two this two is important so click on that copied let's go back to application I have copied the client ID it's time for secret key okay so once I have uploaded this video I'll be removing this client ID and security from my Google app so that no one can use it in fact no one will use it for wrong use okay I guess that's done but hold on we still have one issue we have specified that we are using or2 we have specified the configuration but nowhere we have specified how we use the token so here we have to specify public in your home controller you can use any controller for that matter I will say public it will return the object of principal from java security and let's have the name as user so whenever you use a token will will be using a token as user and then it will return the principal object I mean the current user object so it should return principle okay but then when it will be called so the request mapping you have to specify that you have to say request mapping in here whenever your user request I mean whenever you request for user it should send that and I want it to be in dis format or XML format so I will say response body I don't want a page I want XML or Json once you have specified that let's relaunch the application I hope hope this will work relaunching the application let's go back to our incognitive mode so let's say Local Host col8 and you can see the moment you do that the moment you try to use an application I'm not searching for uh Google pag you can see I have searched for Local Host col the the moment you say enter it will take you to the homepage or the Google page where you have to log in in case if you have already logged in it will not ask you for this page so let me use Tesco's ID here which is training gmail.com click on next okay you can see once I have logged in I'm calling this page but not this page I want to call Local Host col 80 nothing is coming on this page because nothing is there on this page Oh by okay I've made some changes before goinging the video let me call the homepage and you can see it says welcome alien and be called it right that's how you can use this but what if you want to know who is the user so you can say SL user and this is the current user so that's how you can use it you can use this token you can have you can save the data in database or in a file in a session it's your choice you can see the token it says who was the name of the user and then the picture you can use that as well what else we have here there should be also email ID here which I can't see but it should be there email ID should be somewhere yeah it's here and that's your email ID so that's how you can verify the user using Google or2 in this video we'll talk about microservices now now to understand microservices let's go back to 1970s that's right when the computers were new so in 1970s we were trying to solve simple problems with the help of computers maybe scientific problems but then they were simple in today's term now after the successful use of computers for the scientific and research purpose we thought can we use it for commercial use as well and that's where in early '90s or late '90s lot of people lot of companies started using softwares and computers for their internal use also we also got uh social media websites in early 2000 but now if you try to understand the entire ecosystem or if you try to understand the world of softwares everywhere in fact everything in the world is happening with the help of softwares if you want to book a cab you will use software if you want to transfer money you will use software if you want to buy something online or if you want to buy something you will use a software right so if everything is done with the help of a software which we normally call as applications what normally happens is we build a particular website now that's what we call them we build a website and in that particular website you'll be having all the services now when you talk about Services what exactly it means now imagine a shopping website example if you go to Amazon you get lot of features there right on Amazon you can basically search for a product you can buy a product you can basically add a product to a cart and then you can buy a product you can pay for that online and also as a seller I can sell my own devices or I can sell my own things on Amazon because it's also a Marketplace so basically Amazon itself is a huge software which has lot of different Services right now if you want to build this website by yourself what you will do is you have a traditional mindset of course one of the best thing about building a software nowadays is we think about creating different modules so what you do is you break down the entire project into small parts in fact that's how we think right uh so if I want to build a website I will just break down the services into small modules I will work on one particular module and also that's what we also suggest right so if you have a big team you can divide your big team into small groups so let's say if you have 50 people working on a one big project you can have uh you can have five different teams of 10 or maybe 10 different teams of five members and then these people will work on one particular feature right and that's how you can build this amazing website like Amazon or amazon.com to buy something now the thing is the approach is actually coming from the software development field now when you build a normal software what is a tendency so we have a software which works on a desktop in fact when I started my career I started in that zone you can just write an application which works on a particular machine right so what you do is you structure your entire application for one device now that's what we do in the web world as well even if you have multiple teams they are working on different Services they are still one package right so in maybe in Java or in other languages what you do is you create a package right you create let's say in Java we create a w file or a jar file you will put all the services in this particular wi file and then you will deploy this on a cloud or on on any server the problem is you have lot of services here on one one system so let's talk about some advantages of doing this the advantage of doing this type of architecture which we normally call them as monolithic application or monolithic architecture where you have everything in one place all the services right of course they are designed by other members you can simply group them you can form a particular package and you can deploy it so the good thing is you know what is happening with the application what are the drawbacks there are multiple drawbacks with this type of architecture the first one is team dependencies so let's say if all your team your 10 teams are working together to make all these features they have to actually dependent on each other they should know when you are going to go for for the next release right so of course with every release you go for a version so you have to specify okay I got a new feature but let me know when I can push this feature in the product so that we can deploy the entire project the second one is the scalability let's say if you talk about Amazon itself in fact we can take this example or we can take this concept for multiple examples multiple domains but if you stick to Amazon here let's say if there's a sale going on now if you know that tomorrow there will be a huge sale maybe for some Festival some some other event what do you think which of the services will be needed scalability here in the monic application what happens is if you know that there's a sale going on or there will be sale happening next week what you will do is you will scale your application in multiple systems right maybe you will try to do vertical scaling by adding more servers or more RAM power or you can do the horizontal scaling so basically you will have the same application and having multiple instances of them see in terms of sale it's not like you will get new users or you will get lot of new users you will get lot of new sellers what you will get is more searches you will get more payments so instead of scaling the entire application don't you think we should be scaling only the small modules there so that's what we want right we want to use this particular service so instead of scaling the entire monic application we can we should be able to scale that Individual Services next one what if you have a big application and if you think we will build this application using uh as a one the application the thing is you have to stick to one technology maybe if you're using Java for the entire application you have to stick to Java even if you have a new team who says Okay I want to work on nodejs they can't because you are building one big application which will create one particular package which will go to one particular server what you can do is if you have a different Services here you can actually create different services using different Technologies right so that's one of the advantage we want and that can be done with the help of microservices so each service which I'm talking about here they are called microservices the advantage is if you have 10 teams they can divide the entire project into small Services one thing to remember here each service here should be self-contained they should be able to deploy it separately and also they should be scaled separately they should not be dependent on some other services so each service here will be called microservices now since they are microservices we can create them in any technology we want so one service can be done with Java second service can be done with node.js and now since there are multiple Services you can scale them the way you want maybe if you have 10 Services I want to take this fifth and sixth service and I want let's say 10 instance of them I want the second service to have five instance so I can Scale based on the service requirement not for the entire application and also since they are separate Services even if some service goes down it will not affect the entire application now in terms of oneic application if you make one change it is not working it will crash the entire application that's not the case with microservices so basically what is microservice they are Individual Services which are self-contained they can run as a individual service and maybe let's say if I want to go go to Amazon if I say I want to search for the products that's one service I want to create a new account that's one service I want to sell a particular product that's one service so these are your microservices now everything looks good on paper right we are saying that microservices are great do we have have any challenges there the thing is yes now since we have multiple Services how this services will communicate it's not like they can't but then it's difficult to configure them okay and you will be doing that only once you have to use service discoveries you have to use API Gateway which will stand in front of all the services and you will be also having some resilience system in between what if some service failed do we have a field support there and creating a micros service is not just about writing code okay it's also about creting the architecture before you write a single line of code how do you design your system how do you create those Services that's also important and if you don't design your microservices properly ultimately you are creating a worst system of monic application now one of the question might be how these Services communicate so they basically uses uh HTTP request or response to communicate between each other of course they will be having endpoints to communicate and also we have to also think about security because now since you have M defense Services if a customer sends a request which Services you have to go to and does that particular user has uh access to to do that so that's the challenges of microservices but it's an awesome thing big companies they are using microservices on big level in this video we'll talk about cloud computing and we know about Cloud right oh not that one but the cloud basically means the group of computers on a network and that sounds simple now right now basically why we have this Con concept of cloud computing now think about this if you go back in the earlier days of software development and web application what we do is example let's say if you want to make a website what you do is you use certain Technologies like HTML CSS JavaScript using which you can make a static website of course you can use JavaScript to build backend as well but just to keep it simple let's say if you want to make a static website you can build that with the help of HTML CSS and JavaScript and then you can run that on your machine and that's why when you run it you will see a URL on the top right now basically if you want to make a dynamic website let's say if you want to add a back end to it it can be done with the help of JavaScript PHP Java C there are multiple options there but let's say when you build a back end as well you need to run a server which we call a web server now this web server is running on your machine of course it also needs an IP address and by default if you're running on your own machine you use something called a local host that's your own machine right and then it works on your machine but then when you're building an application or a website you want the world to use it you're not doing it for yourself right now in this case if you want the entire world to use your machine that's not possible because the website is hosted on your machine and you don't have a public IP address what you get from your router is a private address and if you want the world to access your website you need to get a public address and let's say you bought a public address and now you can basically Al use a public IP address for your machine so let's say I have this machine here and in this machine I'll be having the web server installed and then I also have a public address now you can give this public address to the world and they can use it oh you'll be saying okay they cannot remember your public IP address that's fine you can get a domain name let's say you get a you got a domain name as tesco.com so instead of using a public address now you also have a domain name but the thing is the application or the web application is hosted on your laptop and of course you want this website to be accessed 24x7 into 365 days you don't want this website to go down but unfortunately this is a laptop and let's say if I forgot to put a charger which normally people do the laptop will shut down and then people will not be able to access your website maybe I'm using a charger and then Windows says I want to update myself and then you know right you can't stop it so basically your website is down maybe your laptop is not working anymore Maybe you spilled a water on your keyboard and now it's is damaged now basically laptop is not a good option right and also there's one more problem with laptop is the computing power let's say you got millions of users and they are hitting your website and now this laptop is not capable to service all the customers and that's why you need to get a good Hardware now in the earlier days we used to go to the market the hardware shop or the hardware Market where you can get all the devices to build a good server basically you need a good CPU you need a good Ram okay I'm not talking about the CPUs which you use normal laptop I'm talking about a server CPU a a good amount of ram your 16 GB or 8 GB Ram will not be enough you also need a good storage maybe you want users to create account right and they want to update their details on the website so of course it will consume a lot of memory and then you have to build a big server and let's say you invested around 1 million that's a big amount so let's say invested around $100,000 to build a big server which can cater to the millions of of users and now everything is working now since this is this that's not a laptop so it will have a good computing power and you have to make sure that you install a good OS which will not go on update frequently and also you have to provide security now in the earlier days you have to do everything by yourself get a room set up the machines and make sure that it is up and running you need a good internet speed you need a electricity power continuously you need a cooling system to cool the entire environment because I'm talking about a server not a laptop even if you play a small game on your laptop you know what I'm talking about right it will create a lot of heat so you have to manage everything and also you need a admin to manage that machine that's right a human is also involved to maintain the entire server and that particular human need to be properly skilled in terms of configuration in terms of security as well because your user data is there in that machine so that's one thing that this is a huge investment to start with imagine your startup now and then to start a company you need to invest lot of money just to set up the system and you don't want to do that right now what's a solution here in fact before going for solution we have few more problems let's say you have this system ready and you have your e-commerce website and now everything is working well because you're getting around 1,000 2,000 or 10,000 users every day but let's say tomorrow there's a sale uh so a lot of new products will be going on sale maybe you are selling an iPhone for 50% discount okay I want to buy that but the thing is lot of users will be coming to that website IM imagine let's say you're selling a pen drive at 800 a.m. for one rupe imagine the number of users coming to their website at this point your server will go down so before going down what you will do is you will go to the market you'll buy a new server with the same cost and the thing is everything is working now and then the sale is over now you don't have that much of users and we have extra server lying there doing nothing so you wasted your money there right now to solve this problem what you can can do is you can instead of doing everything by yourself buying a computer maintaining it getting a person who can manage everything basically in admin and also person for the security you can say okay I don't I don't want to do this let me just hand hand this things to some experts now this is where there are companies who provide you this service they say okay you don't have to buy s by yourself you don't have to manage you give us the money we will buy the service we will buy everything and we will manage it still you have to do the initial investment you have to buy those servers which is not with you they are managing it so that was one solution but then again that was not optimized uh in fact these people they buy the server and they create mult multiple virtual machines on the same server and they C to different clients maybe you can say multi-tenant uh type of stuff where we have we don't have one customer we have multiple customer the same server but still you are investing and still they still have only one machine what if something goes wrong what if there is a network issue what if there's a power failure the Entre entire website goes down and this is where we thought okay let's not use one machine let's use multiple machine at multiple locations because your users are not from one region they are from India us Europe uh Middle East they're coming from everywhere and if you want to cater to all the customers you need servers in each location that will reduce the latency right now this is where we are instead of going for one server what if you can go for multiple servers they are connected and provide you the solution example let's say even if you want to add let's say five numbers so you can send that request to one server hey I want to add five numbers one server can do it or multiple servers come together to solve that problem and that's what happens when you search something on Google when you search it's not like only one server is involved there are multiple servers working together to give you that result in instantly so basically you need multiple servers and when you connect multiple servers that's where the new term arrives which is cloud so Cloud what is cloud basically cloud is a combination of multiple servers which are there which which you're not handling someone else is doing for you now who is doing it we have multiple service provider you might have heard about Amazon AWS we got Microsoft Azure we got Google Cloud uh we have red hat also in this market we also have IBM everyone is now in the cloud market and they say you don't have to buy the servers we have servers you just pay per use that's right you don't have to do any any investment let's say you're startup and you don't have that much of money you don't even have customers now and every day you get new customers what you can do is you can say hey Amazon I have let's say th000 users per day give me US computing power only for those users and the moment I get more users just scale it give me two instance give me a bigger instance that is elastic scaling which we call in fact they also they also have something called elastic cloud computing which is the resources expands on your requirement That's The Power of of cloud one of the best thing is you're not managing it and they give you up time which is almost 100% not exactly 100% uh but they give you almost 100% up time and you're not managing anything now the question arise have we used any cloud service yet if you are using internet you are basically using Cloud by default if you use Gmail that's a cloud service if you use Google Docs that's a cloud service if you play game online that's a cloud service everything nowadays is a cloud service service and by default we are using them right now in the cloud as well depend upon different users we have different types we have we have different service models example let's say initially you had everything with you that's on premise where you have the hardware so basically you have to get the hardware basically you have to get the uh networking you have to get the storage you have to also install some OS you have to also install the run times and containers let's say if you talk about software development we use something called containers now so you have to install the run time you have to install the containers and on top of that you have to install your application which will have certain methods certain classes you have to manage everything that's on premise now let's say you want to use some cloud service where you say I am okay to manage the software layers and Os but I don't want to manage the Hardwares now this is where I AAS comes into picture in which basically they give you the hardware networking and storage everything else is yours so which operating system you you have to install it depend upon you you can decide I want to go with this OS I want to go with this run time I want to go with this containers that's my application that's my function everything done by you except the data centers or the network or the hardware or the networking stuff but let's say you don't want to even manage the OS what you want to manage is the containers example let's say if you want to work with Docker or kubernetes now in this case you can say I have the application I have the runtime I also have the containers I want you to give me the operating systems which will support containers that is your Cass which is container as a service but let's say you want to go one level up you don't even want to manage the run times in this case they also offer you something called a pass which is platform is a service what you can do is you get everything basically you get the hardware you get the networking you get the OS you also get the runtime you just have to build the application and deploy it so it basically provides you a run time where you can build the application now we can go one step forward uh there's a concept of serverless computing now it sounds like serverless but there is servers of course we cannot achieve something without servers right so serverless they have servers it's just that you don't have to worry about any server you say I have this function or I have this particular module I just want to run it now if you can get into a subset of it there's something called function as a service where you get you you create a function and you say I just want to run this function I don't care where it is running I just want to run this also what if you want to scale your function let's say you have 10 fun functions out of which you have two functions which you think will need more computing power you can scale them that particular two functions that is function as a service and then we have the next layer which we use as a normal user which is SAS which is software as a service example let's say you want to store photos you don't have to build your own photo library on the cloud you get Google photos what if you want to type on a editor or maybe you want to type code you don't have to install software on your machine or on the cloud service you get online editors where you can build the application right so those are your SAS platform even Gmail Google dogs everything which you use on cloud is SAS which is software service now as a developer we normally focus more on the pass which is platform a service or Cass which is container service and it looks cool right the cloud services looks cool and you will be thinking okay this is good every company should be using Cloud they are using clouds and most of the companies are actually moving from on premise to to cloud services but the problem is if you talk about Cloud let's say AWS now it's a public Cloud which simply means if you host your application on a public Cloud Amazon is managing it right and they are basically sharing the hardware with the other tenants as well other people as well now in this case there are some issues in terms of security of course in terms of cost it is less because you're using a public cloud and it is shared by different people but what about security the data which you get from your customers are getting stored on this servers normal data that's fine but what if there's some financial data what if there's a personal data what if there are some data which no one should know but let's say as a company you're storing your customer data on a cloud service which is third party there is a kind of risk here now by default all the servers are public clouds right so these are accessible for anyone it's not like they can hack your data but it is hackable right how can you trust it also there are some regulations which you have to follow from the government and that's where we have some issues with the public clouds what's the solution you you can create something called a private Cloud inside your company's wall so basically you have a cloud which is a private Cloud for your own company so you'll be having multiple data centers someone will be managing it but you have a control on your private cloud and all your customer data will be stored on private Cloud Advantage safety drawbacks cost because now you have to manage everything so what if you can get a solution between this two and that's where we have something called a hybrid Cloud so what you do here is every company will have data from the customer some data is private you have to maintain the Privacy there and some data is okay to shift to public clouds so you will be using some Services of public cloud and some from private Cloud this is the hybrid cloud and most of the companies are using it in fact you might have seen this companies are moving from on premise to the cloud but there are few companies they are coming back they're coming from Cloud to on premise because they thought cloud is great but looking at the cost now sometime if you don't build your application properly or there are some cloud cloud services which are not that optimal you might get surprised by the billing they charge you for a part for that particular service so yeah that's about cloud computing so what simply means is instead of doing Computing on your own machine or your own server you can give this computing power to some third party some Cloud providers you simply build an application Deploy on the cloud and they will do the Computing for you you just have to pay on the go in this video we'll talk about blue green deployment now basically if you have ever observed there are a lot of applications or Services which we use it says that they are going for the maintenance at this time and most of the time the maintenance of that particular website or web application happens at the nighttime now think about those developers who are sitting there waiting for the night time to deploy the application or maybe they can schedule the updates but still they have to be there because what if something goes wrong the thing is why these updates are happening at the night time is because they have less users of course it depends upon the region as well example if I'm doing a update for an application which will be used in India I will be doing that update at the night time in India because you'll be having less users now what happens is every time you have a new feature so let's say you got an application which is let's say version 2.5 and now you want to release version 3.0 now at this point you're running server the server which is live need to be shut down and it have to be upgraded and sometime it takes time right to upgrade your entire application maybe let's say 2 seconds or 10 minutes or 1 hour now in that particular time the users will not be able to use your application and that's why it happens at the night time right but think about the people who are behind this updates they have to be there now you might be thinking okay what what's the big issue we will be doing updates once in a year or once in six months and if it the system goes down for let's say 1 hour that's fine we'll update our users see that's the old technique right nowadays what we we want is instead of updating the service after few months we do updates in a week now right so that's the uh agile way of doing stuff where you want to get the customer feedback instantly now when you want a customer feedback of course you want to push the new features and for that purpose you will be updating your software frequently and imagine going one hour off in a week you your user will not be satisfied with that so there are two issues first is a major one which is where your customers will face an outage basically they will not be able to use your application for that particular time and second issue is the developers of course or the Ops theme they have to sit there now even if you deployed your new version from 2.5 to 3.0 what if something goes wrong of course you tested it properly so you tested the entire project entire package internally privately and everything was working there suddenly the moment you make it public it is not working again you have to go back to the older version again you have to face the outages now what's the solution for this the solution is the blue green deployment now do we really have to paint your application with blue color or green color not exactly this is just a term they also use term like red black or having a secondary instance doesn't matter name doesn't matter what's important is this concept simply means you'll be having two running instance at the same time that's right two running instance now in this two instance one will be called a blue server or second will be called a green server or maybe a blue inst or a green instance now at one point only one instance will be live okay so even if you have two instance running and even if both are running but then only one will be live for the public so let's say in this case you have blue so all the users are actually using this blue instance right now in this blue instance you are doing all the operations in fact this is responsible to accept the user request respond to it or maybe this is connected to some other servers as well or maybe some to a database now parallely you'll be also Al having a green server now what's the use of this green server now this is there sitting idle doing nothing now as a deop team or as a develop team or off stream you want to push a new update from 2.5 to 3.0 of course you will not be doing that on a running server because that's running right so what you will do is you will do the update in the green server or the green instance and you will say okay I have the new package everything is tested let's deploy that on this green one now the moment you upgrade your green instance to 3.0 and you think it's ready to go live now you will gradually move your users from the blue instance to the green instance of course you will not be moving everyone because what if something goes wrong so maybe do it gradually at least if something goes wrong least users will be affected so you are gradually moving your users from blue to green and now once everyone is moved to Green instance see at this point when they are moving both instance are live but the moment you got all the users on the green instance now your blue instance can go to idle State you don't have to destroy it because in a blue green deployment you do need two service running or two instance running now what is a rule of blue one here so blue one is there sitting idle maybe you can use it as a recovery server what if something wrong goes wrong with the green server at that point you can say okay we do have a blue instance and just route all the traffic to Blue and once you are confirmed that the third instance is running perfectly uh you can just also upgrade your blue one to 3.0 and then again you're not using 3.0 Blue version but it's there now what if after some time you got a new updates now since we are into a phase where you want to upgrade your services continuously right uh there was a time when you used to provide the updates monthly but now because of this new service or new feedback from the customers we push the updates continuously now at this point let's say got 3.1 version now where you will update it on the green one no you have to do that on the blue now the same process which went for the green one will go for the the blue one as well so even in the blue one you will push the update and then again it's still private once it is 3.1 it is still private and now once you think everything is tested well it's time to go live what you will do is you will gradually move your users from the green instance to the blue instance and once everyone is moved you can you can change your green one as well and make it 3.1 so that's again just a concept you can do that using the cloud services uh so of course you also need a router in the front who will change this uh directions right so if a user comes user is not saying I want to go to blue one or green one user has no idea it's the L time between which you have to configure to mention okay which one is live and which one will be idle now it's not actually a good idea to use both of them at the same time if you think okay we have two instances let's use both again there will a problem when you want to upgrade it so the users who are using the the second instance will be facing issues so that's it from the blue green deployment let me know your thoughts in the comment section and let me know if you need such more videos in the future in this video we'll talk about Cloud native and 12 Factor app now basically whenever we talk about applications I'm talking about the Enterprise level application now initially those application used to stay in on premise basically every company will have their own servers and they will deploy the application on their own servers but then we have talked about Cloud right now most of the companies are moving from on premise to Cloud so basically all the services which we are using nowadays is cloud-based even the public application like Google Docs Dropbox Gmail everything is cloud now the applications which are used internally in the company they are also being deployed on the cloud of course not every company is moving into it but majorly most of the applications are on cloud nowadays now the moment you talk about Cloud applications we have two interesting terms here the first one is cloud ready and the second one is cloud native and then it's important to understand the difference between between these two see what we expect from a cloud application is the cloud services the moment you talk about Cloud it provides a lot of different benefits right benefits in terms of cost benefit in terms of scaling and benefit in terms of less issues right now if you want to achieve that of course you have to make an application which will use all those services and which will fulfill the promises right but what happens is the application which are deployed on the cloud are of two types one is cloud ready second is cloud native now what do you mean by cloud ready so let's say you already have an application in your premises okay or in your in your own local server now you want to move that particular application on the cloud you have to make some changes it's not like you picked up the application and throw it on the cloud no that's not how it works basically you have to make some changes to your existing application and you have to make it cloud ready maybe work with the environment variables maybe work with the configuration files and make sure that that you are able to push your application on the cloud and it should be able to use the services so you are basically making your non-cloud ready application cloud ready as simple as that but on the other hand if you really want to use all the cloud services and if you want to make it more effective you have to build an application as Cloud native now normally this type of applications are new application let's say you got a new project and then you have to decide now do you want to build on premise or do you want to use it on the cloud and the moment you say okay that's what we are we are going for we want to build an application which is for cloud make sure it is cloud native so that it can use all the features but how exactly you make a cloud native application in fact the name itself says right native it is meant for cloud right now if you want to achieve this we can follow certain standards and rules now of course in this world we have lot of developers lot of companies who want to build the application for cloud so they thought okay can we just create some standards in fact Hoku created the standards for you and you can follow them to build a better application for cloud basically a cloud native application of course right how do you define a cloud not application so just follow the rules and you're done the thing is this rules actually made long back and now we have few changes but let's talk about those rules which have been developed at that point and that is called a 12 Factor app so basically you have to follow this 12 rules or standards which is 12 Factor app which they called in fact there's a website as well and you can find the link in description which talks about all 12 rules in detail but I will try to simplify it so that you will understand in one go now basically these are the 12 factors you have to follow to build a cloud new application starting from the first one which is called a code base now basically what happens is we normally store all the coding files in your folder right so basically when you make a project you store that in your project maybe in ide or any edit which you use now what code says is if you want to build a cloud native application you have to make sure that your code base is is using a Version Control so example it says one code one code Bas stke in a revision control and many deploys so basically we can use something like a git here so inside Git You have to make sure that you store your project there in a local or maybe you can use some uh remote repositories let's say GitHub gitlab and we have multiple options there so you should have only one code base I know you want to use on a different environments maybe development environment uh staging environment or maybe production environment so even if you have multiple environments there make sure that you have only one code base and that to in one repository and then you can have a multiple deployments in staging in development in in production that's your choice or for testing the thing is you should have only one code base for one application yeah if you have one code base for multiple application that's why you're making rules so then make sure that you have one code base for one application so not multiple code base for one application and not even multiple app in one codebase so one codebase for one application second we have is dependencies of course when you build a project there are multiple dependencies which you work with example in Java we have to work with let's say I want to use some database connection so if you have to use a a dependency for database maybe you want to do it for logging you need one dependency for logging as well now tendenc is when you build a project in fact we used to do that in the earlier days when you build a project there's a there's a folder which called which which says libraries and this is where you put all your packages and files you say okay for this particular project I got all these dependencies and then now when you want to basically give this project to someone else maybe you have a Ops Team who will deploy the application now you will give all these jar files as well to them that's not a good idea there can be also a problem where you say okay this project needs this dependencies and there might be some version issues maybe you are using 2.5 for a particular dependency and the off stream are using 2.6 or 2.4 there might be some issues right so make sure that you have a separate file like a manifest file example in Java we use Maven so basically in this Maven you you you mention all the dependencies which you need with versions and then keep it separate don't link it to your codebase now when you share this codebit with someone else you also share this dependencies uh the Manifest file not the actual dependencies and they can download it with the version and the actual name so that's how you separate it next we have is configuration see we have to accept it we always love hardore right I'm kidding so basically what you do is in your in your code basically if you want to connect with a database you use a URL you use a username and password uh if you want to use a particular port number you mention that in the code itself so don't mention any of the stuff any of the configuration inside your code that's why it says so store configuration in the environment so basically what you should do is create a different file maybe an environment file or EnV file and save all the values there the advantage would be even if you change your physical servers or anything related to the environment you don't have to change your source code just change the environment variable and you're done so the idea is don't touch your code once it is built next we have is backing services so it basically relates to the above Parts as well so what it says is have a loose coupling between your application and the backing Services now this backing Services can be a database it can be some other third party Services which you're using example let's say if you're building an application and in this application you need some data from the third party so try to create a separate stuff treat that service as a resource maybe in future it might change let's say currently if you're using myql in future you want to use post G that switching should be easy right so in your code don't hard code the configuration uh don't specify which particular resource you want to use just say Okay I want this resource and then you can just configure it outside okay for this particular request I have this resource and you can use them so treat them as a resource not a part of your application next we have is build release and run one of the tendency we have is write the application run it maybe you can do that on the production as well so now we are doing it in the development environment on the production as well you will say okay I have a code here let me run it don't do that what you to do is you have to follow a three different process build release and run so what you do is if you have a project ready create a package build it maybe in Java you can do it with the maven and once you got your package maybe a jar file you can give a release to it maybe a configuration environment variables and all the stuff now this release will go to the running environment maybe whatever environment you're using there whatever servers you have whatever environment jvm you have there and then the thing is when you release it you have a particular version to it right and that is working the advantage would be even if you want to make a small change you will not do any changes in the release version what you're doing is you're changing in the build again you change it create a new package with a different version every time you release it you have a different version to it and if something goes wrong you can go back to the previous version because let's say now you have released 5.5 and then something went wrong with that you can go back to 5.4 or let's say in the 5.5 everything is working you got a new feature you just use it uh build a new one 5.6 and release it so versioning is very important there as well so create a separate thing build a separate release a separate run is separate next we have is processes now basically whenever we talk about web applications in the ear days we used to go for sessions or uh stateful services so what is stateful basically means is as a client if you connect to a particular server for the first request and then you share some data next time when you going to the server server knows you server knows who you are they have the information about you is a good thing of course right next time when you go there you don't have to uh tell everything you can simply say I was there U I came there yesterday and now I'm here with a new request the thing is by doing this you are limiting your resources basically you're saying okay you got the stateful service and what if something goes wrong with the stateful service a client will say okay I was there yesterday the data is lost now so the idea is instead of going for stateful go for stateless where every process is just a process it's not storing any data in the process so every time you get a new request you say okay I got your I got your request let me process it and if if there's any data involved the data should be coming from the attached resource it can be a database or some other server but make sure that you're not storing anything in your particular session uh so we call we call them as sticky sessions uh so don't do that so every process will be stat less where you don't have any data the advantage would be at any given point you can remove the process and it should not be any matter because the moment you remove the process data is not lost because data is stored in a permanent storage a database or maybe if you want to scale up maybe you want to get multiple process and a client will not even know which process it is going through maybe first time it went for process one next time is going for process two which are the same application but then we have multiple instance of it next we have is Port binding so basically every service will be having a unique port number for different type of service let's say if you if you're using HTTP we use 0 if you use a different protocol we use different port number there in the same way whatever service you are building attach a port number to it maybe because we are not sure on which physical server they are but we are sure what Ty of service they are providing so every service will have a port number so that's why it says export Services via a port binding and that's why when you build multiple Services let's say in microservices every service will have a different port number right that's how you search for it next we have is concurrency the thing is different programming language have a different way of doing multi- threading and multiprocessing now basically in Java that's the advantage we have a multi threading concept but the thing is even if you're using multi threading it can go it can scale to a limit because if you're using a particular machine and you're saying okay I want to use multiple threads and I want to use the complete part of this machine but of course machine has a limit there right of course you can can do a vertical scaling now you let's say if you're using i7 machine and with a 64GB RAM and now you're thinking okay we have to increase the speed of it and the capacity you can simply go for I9 processor and you can increase the hard drive to 128 or you can increase your RAM to 128 GB but still there is a limit how far you can go with it that is vertical scaling so what concurrency model says is in of going for vertical scaling you can go for horizontal scaling which is the scale out so basically instead of using one machine or one instance you can go for multiple instance that's how you should build an application you should be able to create multiple instance of the same service next we have is disposability the thing is every time in fact we whenever you learn a programming language you learn this right every time you open the resource make sure you close it every time you start the service make sure you're connecting to the different Services which is dependent on example let's say your application is dependent on a database so the moment you start your application it should be able to connect to the database but what about if you want to dispose a service it should be easy the moment you say okay I have multiple instruments now for the same service and now I want to dispose one service here it should be possible and also you should not lose any data it can be system failure it can be any problem but you should not lose data and that's why if you feel something goes wrong with the system if you want to shut down properly also save all the data in the database so that you can close it properly basically you have to close the connection properly and also even if there's a crash in your system if there's some exceptions close your resources properly so that's what it say it says fast startup and graceful shutdown next we have is De broad parity now this is where the new culture came up which is De Ops right now this article came way back but the culture came after that which is keep the development staging production as similar as possible see in the earlier days we what we used to do is we used to build an applic on your development machine and then there are multiple ways of deploying it on a cloud or a or a server so you bundle the entire application you give to the opst stream now let's say they are on holiday or they're busy with some other project they will deploy your project on the server maybe after a week and then in the development environment you have made a lot of different changes so there's a huge gap between what you're building now and what is being deployed on the server and it can also impact the environment as well so let's say on your development machine you're using Windows and with a different IDE different runtime uh maybe a lightweight process example for SQL I can give you example on your local machine using SQ light on the server you're using MyQ or post because you don't want to install post on your machine so in that case if you are building in a different environment and you're giving it to to a different environment there might be some issues of course the op stream will say okay we have to make all these changes they have to communicate and there will be a delay between the communication but what if the de team and off stream come together and they start working together so as a development team itself what if you are developing it and you are pushing the code on the server and not weekly or monthly what if you can do that hourly so let's say you made some changes it's everything is working properly you push that to a staging environment and you're testing there and everything goes well you can push it to the production environment so the the Gap will be less now and also with the help of some containers let's say Docker so let's say you're using a Windows machine there and you're using a Docker you're building an image now that image will go to the server even if that server is using Linux doesn't matter because you got an image which will run on a Docker irrespective of which underlying OS you're using so that's what this says in fact we also have a concept of cicd continuous integration continuous deployment or delivery so even that is emerging from here next we have is logs these are actually very important for a developer you know every time you are building an application if you want to see what is happening you use system.out.print or you print something on your console right and that's how you know what is happening right so in the if condition you put hi I'm here have you tried this let me know in the comment section but I tried multiple times but then better solution would be create a log file right or maybe you can log on the console as well but then it works on a on a developer machine right you can see a console directly and you can map it but if you really want it to work on a cloud you basically need a proper logging service and also you might be having a multiple process not just one so basically every process will generate a log so what have is first of all generate log from each service that's how you can monitor the application and what if something goes wrong we can check there and then you can aggregate all the logs and save it in one place so that you when you scroll you can see all the logs what is happening and the the beauty is logs are infinite every time your server is running and you want your server to run you can see logs continuously piling up so you have to use a better service to log everything properly so that you can watch it you can see what is happening and the last one we have is a admin process now basically basically you should be able to admin your application from the outside as well so maybe exposing some port or using some services so it should they should be also an admin service there so yeah that's how you follow a 12 Factor app and you can build a cloud native application initially it will be difficult but then uh think in a cloud first strategy right that's what you say uh don't build an application for a development machine and then you can say okay now you have to move to the cloud if you really want to use all the cloud service get a cloud first approach and build a cloud new application with the help of 12 Factor app in this series of microservices we are going to first start building a monolithic application and then we'll see how do you convert that into a micros service now what kind of application we are focusing now see the important thing is we have to understand how do you create microservices what are the Technologies or things which are there to help you to build microservices how do you connect different microservices but then we don't want to focus much on the example examp per se of course example can be anything it can be simple complex whatever application you building the tools which we are going to use will be common right so in this series we are going to focus on those tools and we'll keep the example very very simple now if we talk about microservices that's something we want to go for right and currently we are building a monolithic application so in this video basically or in this set of videos initial set of videos we are going to create a monoy application and then we'll try to break it down into microservices and then we'll see the real power of microservices how it works why do we need certain tools now in this particular video we are going to start with the monic application where we are going to build a quiz service okay a quiz application basically now when you say quiz what it means it simply means a user is coming to the application a user will get let's say 10 questions a user will attempt those questions it will be mcqs basically and then uh based on what you choose you will get that amount that much of score that's the entire plan right now to achieve that of course we need questions right so we need let's say 10 15 questions in the database so we are going to use post GIS which is our database for this particular session and then we'll also have a Java application a simple springboard application to do that and we'll start with the questions okay so we'll start with the question part and then we'll see how do you create a quiz from the questions and when you say questions you need to create questions you need to update questions you need to delete questions you need to read questions so basically you need to do cred operations with question and that's what we going to start here now to to achieve that we need to First create a very simple project right so what I will do is I will head back to Spring initializer which is our that's where you will get spring boot project of course if you have a intell idea ultimate version you can directly create from there or if you have STS which is Eclipse Enterprise version you can do it from there but this is good uh so let's say I'm creating a maven project and then so what I'm when I'm going so if you're new to Spring basically I'm going to start. spring.io uh see for this typ of sessions I'm expecting you to know bit of spring because we're not going to completely get into spring framework we'll I will show you certain things and we I expect you to know spring boot okay at least the basics of it okay so project we are creating is mavin uh the language we are going for Java of course you can choose quartl groovy here or for project you can choose griddle if you want but let's say if I going I'm going for Maven here and then uh the version of spring Bo I'm going for is 3.1 you can choose whatever version you want and then the group ID I want it to be uh com. telescope and the project which I'm creating is quiz app and then we can put some description uh telescope quiz app and we want to create a package of Jar uh jar and then the version of java 17 which I have in my machine now what kind of dependencies we need first of all we need spring web because that's very important we need to create a web application uh we also need to connect with database and which database you are working with we are working with postway so I will add that also I want to have a post um data jpa because that's how I'm going to connect with my spring application and database so I want data jpa and then I don't want to actually have bulky code at least for models so I want to use longbo there just again longbo is something you can if you want to use it you can use it or you can keep it option so that's my longbo there I think that's done so we got our project here so click on generate it will create a project for you you just have to unzip this so after unzipping you can see this is your project I want to open this project from my intellig idea of course you can use any IDE that doesn't matter uh you can use Eclipse you can use vs code that's your choice so what I'm going to do is I'm going to open my intellig idea which is the ultimate version you can also use Community version that works and I will click on open project and I'm going to open the project which we have downloaded on open and you can see we got our project ready here one more thing since we have our database in Portugues you can use any database so I'm going to use po so let me open POG here oh in fact we have to open PG admin and if I open PG admin it will take some time so asking for the password for my machine it is 0000 because this is my testing machine or for recordings so I will just open this again asking for password and you can see we got our PG admin ready here and if I expand this if I expand my databases if I expand uh quent DB this is basically my database which is I have that of course we should have gone for a good name but this works and if I go down to schemas so you can see we have table and at this point we only have one table which is question because that's how we want to work with and in this question table I already have have data so if I want to show you that I will just say view all rows uh you can see we have all this data here now this is what I want to fetch from the uh from the web application basically so you can see we have all this rows in fact we have 16 rows there are few questions which are from uh Java there are few questions which are from python uh of course I can change it to small letter so that it will be in the same range but that's fine uh you can see we got how many columns we have so if I try to see The Columns here you can see we got ID where we have the primary key uh we got category because that's how you differentiate different questions for the particular subject uh we got difficulty level which is for do you want to keep this test easy or difficult we also have options you can see we have four options here and then we got question title we got right answer so basically we have all this column of course we'll go through this once again when we want to create that model so that's what I want right so what I will do is I will just go back back to my intj and here I want to do certain things because I want to get that data now how do you get that data so basically let's say if I go to my browser and if I type this if I say Local Host colon 8080 SL questionsall questions basically I want to display everything in this page that's my first task okay so we'll divide the project into a few tasks so that it will be easier to follow up and then uh you can also practice one by one so basically this is the first Target let me get all the questions so that I can see on the browser of course we can also use use Postman uh to send the request and stuff we'll do that later at this point let me use browser and if I go back to my inj uh so how do I do that so first of all to accept the request at this point you can say it is not the site is not reaching okay first of all make sure that in your P you have all this configured so you can see we have dat jpa we got web uh we got postgress we got Lok so make sure that you have this Longo is optional as I mentioned before so if I go back to main now this is where I want to create my my rest controller okay so I will just right click here and say new class and I want this class to be named as question uh controller okay now this is where I want to accept the request so what I will do is I will just make this as a rest controller uh because I want to accept the request and also this particular question controller will only work with question of course uh in the future videos when we talk about quiz how do we create a quiz so a quiz will ask you hey what type of quiz you want to create uh tell me the topic name tell me difficulty level and based on that it will Generate random 10 questions now for that you'll be having a different controller so this is question controller you'll you'll be also having quiz controller right so I want to specify the path here as well what I can do is I can say request mapping and in this request mapping basically we'll mention the path so whatever request is coming for question this is the controller who will handle it right now basically what I want to do is I want to accept that request of SL all questions so for that we have to create a method so I will say public uh it should return let's say a string initially and we'll change it later and here I will say get all question that's my method name right and this method is not actually accepting anything because we are not passing any parameter we're just saying all questions and of course this should match with this so I will just copy this and you can see the URL it is the Local Host 880 because that's on the same machine the port number for my server is 880 by default and this is the question for the question part uh for the question controller once we have a quiz we'll have a quiz there and then uh we have our all question so here uh I want to map it with all questions right so I will say get mapping and here the path is my all questions so if someone is requesting for all questions this is a method I want to execute and what I want to return here very simple stuff I will say uh hi this are your question just to keep it simple let's see if this works and I will save this let's run this code okay how do I run this so basically to this is a spring boot app right and to make it work you have to basically go to the application right click run okay it says lbo requires enabling okay I will do that okay now there's certain problem to this project the thing is we have initialized the uh the I mean in our P file we have mentioned that we are using data JP right and and then we have not done any configuration for that is that the issue here it says fail to start the reason is U we had we have not mentioned any post URL okay that's the issue here uh so what I will do is let me just configure that so again you can just copy paste this stuff and you can use it later so the first thing you have to mention is the driver class name and the driver we are using here is POG driver uh the next one we have we have to use here is the URL and the URL is jdbc colon post SQL colon localhost colon now the port number for post G is 5432 and then you have to mention database name database name for us is question DB right uh then you have to set the username password for postgress so I will say spring data source username is pogress and password as well so we'll set the password as 00 that's for my machine and then you have to basically also enable ddl auto so ddl Auto if you want to create the table if you don't have it so I will say update uh I do have a table but let's say if you want to set this and also I want to so let me type it spring. jpa do proper. hybernate now this is where you have to set the dialect right so it is uh why is not coming automatically so it's org. hibernate do you don't have to byard this basically I also have nodes for it so dialect Dot psql Dil okay so once we have assigned these properties I think it should work now again we are not using them but then since we have added data jpa so it's become compulsory to add this so I will go back to my application and I will run this now you can see this time no problem the server is running on 880 and let's go back to our browser refresh and you can see we got the output it says hi uh these are my questions of course we have not any sent any questions but let's say this is the output but I want to return return the real questions right how would I do that so let's try to achieve that in the next video so in this video basically we created the project structure uh for the monic application in next video we'll try to uh solve this and also we'll see how do you do the remaining C operations in the question controller okay so till this point we were able to create our project and now we need to execute certain things right basically if a user is asking for all the questions we need to return all the questions not not just saying that hey this is your questions we need to actually print those questions so if you go back to your browser and if you execute this which is questions okay not questions it is it should be question because we are that's the request mapping enter it should print this it should print these are your questions right okay but then we want to print the actual questions which is coming from database and and I have shown you right in database we have all those questions here as you can see okay so how do we achieve this so what we can do is let's go back here and actually fetch questions from database now for this we need multiple layers so basically whenever you build a web application we use different layers there so this is your controller right so this is your main controller this is responsible to accept the request from the user and then the request goes to the service layer now the we have one more layer in between which is your service layer the work of a service layer is to do some processing let's say if you want to add two numbers now as a client says Okay I want to add two 2+ 3 now this data goes to the controller because controller is responsible to communicate with the user but then controller will not add these two values controller will simply say okay I got this values now I need someone to add this values so the request goes to the service layer now in service layer you mention all your business logic what you need to do when you get this values what operation you want to perform so that's the job of service layer now service layer can do any business logic it can do any process in any type of calculation you want but what if you want to fet data from database now in that case you have one more layer which is your Dow layer now this Dow layer is responsible to actually connect with database and fetch data okay now let's say on the service layer you are you want to get data from database of course you get data from the Dow layer but also you can process you can do some logical processing inside your service layer okay so we got three layers here and that's what we want to do here so basically let's say uh we are in the controller now and inside the controller I'm saying hey you know uh we just want to I mean instead of returning the text I want to return the actual data so what I want to return is uh I want to get data from the from the service layer now basically let's create a object of service layer which is question service and this question service will ask hey get me all questions most of the time you will have the same method names but sometime it can be different okay so it depends upon the use case at this point I'm assuming that in my service class I have the same method now the weird thing is we don't even have a service class so let's create one of course you can just right click here you can create a service class we can do that let's say new and I'm creating a new class and I will name this class as question service and then I just also want to make sure that this class service belongs to a service package it's a good idea to move things in a different package depending upon the use case or depend upon the type of the Serv class you're creating so this is service all the service classes go goes into service package all the controllers goes into controller package so we have to do that so move the package to service and it says it's not able to find one no issue let me just create a service package now once we have created the package now I can move move to the service package okay so you can see now the service is into service in fact for controller as well it it is always a good idea to put that in a different package so package I name this as a controller and now even this goes to the controller of course you can also drag it there refactor it will it it will work so if I expand this you can see we have a controll and controller package okay so in this basically in this service I want to make sure that I have that method so if I go back to my controller see we we do we not be working with this so we can just close this and application properties one set you don't need to do anything with that let me close it I just want to keep it simple we got two window open here controller and service so if I go back to controller of course I need the object of service so I will what I will do is I will say question service and this should be question service so you can see I'm creting a reference and also since we are using a spring framework I don't need to say new question service what we can do is we can use autowired here and then uh okay that's Auto wiring the only thing that we do is on top of this class since you want spring to handle this objects you can use annotations now if you want spring to create the object of course you can use add component but spring also provides you some different annotations which which does the same work of component but different names which is one of the one of it is service now of course you can also add component but since it's a service layer let's write service so go back here and you can see auto wiring done there's no problem with this there's a warning for auto wire field injection is not recommended yeah you can you you can go for Constructor or Setters but okay let's ignore that now once we got this we can get all questions but unfortunately if you look at this service class we don't have any method so of course I can type it here or I'm lazy I'll just go here and uh say Hey you know uh get all the questions but there's one thing before I do that see when you say you get all the questions will you get one question or multiple questions or we don't even have a model here so I think is how will you represent a question so if you go back to your database you can see you have multiple columns here right you can see we got ID we got category we got difficulty level option one option two option three and if I scroll a bit you can see we have option four question title right answer oh so many uh parameters right all the columns so to represent each table we create a class okay we call them as entities and we also call them as model in MVC model MVC patterns right so what I will do is just to represent that even before we get get all the questions it is a good idea to create a class which will be your question class now this is your model class okay and this will have those variables so you can think about a class design matching with the table design so class name table name the class fields are your table columns right and also the number of objects you have for a class if each object can represent a row right that's your omm object relational mapping okay so here what I will do is I will create those variables quickly so what are the variables so what are the variables we have so first of all we have an ID so I will simply say private integer ID that's the First Column uh the second one we have is question so I can say question title of course we need to match the variable name here and with the column name there otherwise we have to spe specify what column name we are using in a table okay so if you look back to your table and if you scroll a bit you can see we have question title here so automatically it will put underscore because in SQL it follows a snake casing rule so basically we have to put underscore between two two words but in Java we use camel casing we use a capital letter so the orm framework in this case we using jpa will take care of it then we got option one and likewise we got option 2 3 4 what I can do is as I'm lazy I will just say paste paste one more time because for the answer as well so this is option two this is option three this is option four and this is my right answer the next thing we need is difficulty level as we mentioned before we have three levels here easy medium difficult so for that also I can use a string of course you can also do that with the help of numbers but uh since in the database we already have created uh a column which is difficulty level which is of type string so we'll go with that so difficulty level you know the thing is I should be using a Capital One but the thing is here if you see database the difficulty level is still the same there's no underscore here so we cannot put a capital l so again when you design the application you design in a proper way I just did that you know I created this table with the with a name and then now in trying to match it okay so ignore that part so the good way is to have that capital L so that you will get underscore the table okay and here we need to make make sure that this ID is a primary key right the way you do that is by mentioning the ID as The annotation and also uh this ID will be autogenerated so what you can do is you can say generated value and you can specify this strategy which is generated type sequence okay uh if this doesn't work I will go for identity uh let's see how that works okay so now for the question also we have to use and annotation so basically what happens is we want this table to get mapped with the class right so if you want to do that mapping we have to use something call an entity which is annotation if you have a different table name so example here the table name is question itself so there's no problem but if you have something else then you have to use at table as well and since I'm using lombok here I don't don't want to normally if you don't use Lok for every variable here we need to create two methods getter and sets two string method also uh so imagine how lengthy the code becomes and that's it's a good idea to put uh at data here which is coming from lombok and we done so we got this question no problem now let's go back here in fact you know this also recommends that you use this it says typo so what is suggesting to okay it's not giving Su but it says there's a type of because we have not used Capital but that's fine I I can say I can live with that okay so if I go back to controller now uh so what do you think will I represent a string or the object of course it should be object this time right so we don't want to return a string we want to return an object but then how many objects we we're going to return one two multiple right so this should not be a simple question first of all let's import the package so it says there are multiple classes multiple choice import class okay there's a question in uh in in buil package as well I will use mine and then we have to make sure that we are ring a list of questions not one okay so we have to import the list as well done and now actually we can work with get all questions so what I will do is I will just ask the IDE to create a method called all get all questions and you can see the method we got in question service it's just that we have to provide some logic now of course the service here is not doing any processing is just fetching data from Dow layer oh we don't have a dow so what I will do is I will create a class I will name this class as uh question da and then we have to make sure that this belongs to a different package a dow package and we have to create a DA package here we'll say new package Dow and just move this to da done okay now this looks cool we got da as well and the thing is for on top of service we are service here right on top of Dow we have to mention it is a repository okay now the fun part is when you say we have a DA here and of course there will be method as well example if I go back here and when I say get all questions there should be a method in because see ultimately I have to use Dow right example I will just come back here and I will say Hey you know know I want the question Dow object here so that I can call the methods and I want this to be autowired so that's done and then using this Dow question Dow dot I can fire a method right so of course in the Dow as well we'll be having the same question same method which is uh get all questions right so this should be simple and then once you get all the questions we can return it but the thing is in question da we don't have that method so what you will say Hey you know just create the method okay even if I create the method we have to fetch data from database now if you know jdbc basically we have to write those beautiful seven steps right it looks like seven steps but the code is very lengthy you have to file the SQL query you will get lot of data you have to iterate between that data and convert that into list of questions oh lot of work right don't worry we got you covered that's what spring says spring says we you know one of the package we have introduced here if you go back to pom.xml one of the package we got here is data jpa right now with the help of data jpa what you can do is instead of creating a class we can create an interface and then we just need to extend a interface called jpa repository that's it all the things like fetching data from database saving data will be handled by data jpa I know that sounds like a magic right but then okay so before we do that so JB repository asks you for two things what type of table you're working with and what is the type of primary key which is integer here so you have to mention two things the table name actually you have to mention the class name which maps with the table and also you have to mention the primary key type that's it now I know you're not trusting me there right you'll be like hey that's impossible let's go back to our service and of course you can see it's not working this method is not there if this meth is not there there might be some other methods let's try it out I will say dot and can you see that if I scroll down there are so many methods here one of the method is find all it will give you the list of questions example if I jump here you can see find all gives you a list we just have to use it so it will return the list of questions our job is done are you still not trusting me just wait and watch let me just restart the server because we have made some changes of course we should have gone for Dev tool for the hot boot okay now let's go back to the browser and refresh boom can you see that okay this is a raw the thing is uh if if you are getting this type of output on your screen and I can get this is because I've have installed one of the plugin it is called a Json formatter okay so because of this Json formatter it gives you this beautiful Json look and you can see we got all the question does it matter the topic oh did I just forgot this topic name yeah we forgot the category oh how can I do that so if I go back here in my question we don't have category in table we had that so category okay now let's restart and go back to our browser refresh boom you can see we got the category as well so we got Java python we have two categories at this point which we are using it here cool so this is how you can fetch data right and we have done with the fetching part uh what about fetching by category so at this point we were able to data and make sure that before you go for the next video you actually practice this Okay cool so if you talk about fetching all data we are doing that but what if you want to fetch based on a category something like this I can go back here I can say hey I don't want all the questions I want to fetch based on a category that to it should be let's say Java and if you say enter it should only print questions for Java and if you mention let's say not Java but python it should print all the python uh questions and also I I want to add questions I want to delete questions maybe let's only work with add delete and update you can do it by yourself let's do that category part and adding in the next video okay so now let's add these two Services basically we want to make sure that if you want the to get the questions we should be able to get that with the help of category and also what if you want to add a question so let's do that so first let's work around this which is so if you remember uh this is what we were doing right so if we want to fetch based on a category let's say if I want to fetch questions only for Java it's not working at this point we want to make it work so what I will do is I will go back to my IDE and here let's add because of course we have to First add a controller right so what I will do is I will say public and of course this will also return the list of questions which we want and then I can say get questions by category of course your method name should make sense by reading it now this time this get question category will actually accept a value because you have to mention the category right so if you look at the address bar uh you can see this value will be keep changing so this will be Java this will be python or whatever topic you have there okay so let's accept that here so I will say string category so whatever data you'll be sending in the in the URL will be received here in this category but then how do you map it so of course we have to say get mapping because we are getting data and now in this bracket we have to mention the the request is going for the category this time not all questions and slash now of course the category words word is important is because that's what we are doing here right see question is by default for the entire controller you can see we got question here and then we are specifying that we want to go for a specific category and that's why we have to mention category here and then in this here in this curly brackets we'll mention uh the topic so whatever topic you're receiving so let's say if you want to fix it for Java so this particular method will will be called only for Java of course you have to do it for the other technology as well now of course we don't want to write multiple methods for each language or each topic right so it makes sense to put that as a variable so we'll put that in a curly bracket so whatever value you will receive here will be assigned to this variable but will it will it be happening directly uh no we have to actually put an annotation here which is called path variable so whenever we are passing a value in the path which is the address bar the address that particular value which is in the cly brackets will be assigned to this particular variable now since the names are same you don't have to specify the name here normally if you have a different name let's say here you don't have category you have Gat so in that case here we have to mention which variable we are targeting so we are targeting a variable cat uh but since they are same I will not be mentioning that I will just go with category here now once you specify that we need to return the values right of course we can simply do that we can return a question service of course we as a controller we don't have to do any extra work we'll simply ask our service hey service give me questions based on the category so I can say get questions uh by category but unfortunately uh okay we have to also specify the category here because how will service know which category you're talking about so basically here uh this particular thing you know this is not there in the service so what I will do is I will just click here and I will say hey give me a method so I want this method to be there in the service because service is responsible to give me data but service says how will I give you that I don't have it right of course you can do processing in service example what if you want to check for the category length uh let's say if you specify Java if you want to know length of the string of course why we will do that but the thing is we need to fetch data from database now and that's where the Dow comes in so we'll say questions question da Dot and we'll say hey question da find because see when you when you interact with da basically since we're using JP repository instead of using get we'll be using find because that's the standards we follow right so we'll say find okay but then we don't have any method if you see we don't have a method which gives you by category of course if you want a particular question you can get it by saying find by ID so let's say there's a question uh with the ID 50 so you can say hey I want question by 50 so it's easy to get with the primary key but now we are getting data from one of the column unfortunately we don't have it now you will say okay so you know I know what you're thinking you will say okay in that case we have to create a method which we have to Define everything how you're getting to fetch based on the category by using some SQL query okay so not there and not even completely because we don't have the method yet so let's find the middle way so what we can do is jpa is actually smart enough to do this what you can do is you can say find by category okay and then in the bracket you can mention string category and your job is done it's just that you have to specify what type of data it will return so this should return the list of questions now the thing is do we have to write an SQL query do we have to Define this method no the thing is category is actually a part of a table right so your data JP Offspring is smart enough to understand that a user is trying to find data based on a category so it is simple yeah if you want to do a lot of customization then it will not work that's why you have to use SQL oh that's why you have to use hql which is the hybernate query language or you can use jpql which is jpa query language so you can you can do that but at this point we don't need any specific hql query or JP jpql query for that matter and this will work okay so jpa is smart enough to give you data because the column is present in the table if you go back here now if you say control space you will get this method which is fine by category and you have to pass this particular string and that's it this will return the list of questions I know lot of things looks like magic here but that's that's spring framework for you now if I say everything is everything looks curred let's relaunch the application don't give me this dialogue every time okay uh let's go back to our browser let's say refresh and voila can you see that we got all the questions for Java Java and Java but yes if you specify something else here if you say spe if you specify let's say python it will give you questions for python if you can see but what if you specify something which is not there example let's say if you if you search for Cartland enter empty because we don't have anything for Cartland so that's how we can basically use this we can get the categories okay but what about add questions so we know now how do you fetch all the questions we know how to fetch questions by category which is one of the column we have now let's try to work around with ADD so what I will do is let's say uh if I look at my database I can do it from here itself and if I say Java you can see we have all these questions for Java the last ID we have for Java is 10 of course after that we have python questions uh I just want to know which is my last ID which we have so if I go back to my PG admin we got 16 questions now I'm trying to add one more question is 17th okay let's do that so what I will do is I will just try to create a method and this will be for adding string or adding question but then what will I return so maybe I want to return a message by saying okay it was successful or something and then I can say not get question add question and then in bracket now if you are sending a question from a client side of course right if you are fetching data on the browser maybe you want to send data from the browser or from from the mobile application doesn't matter the client can be anything here okay how will I I receive data so when you say add questions it should be a perform of a question itself right so whatever I will receive should be a question so that I can add it I don't want to receive a text file let's say CSV and then manually from the code I will just fetch up each fetch each value oh that will be tiring right or what if you can get the Json format so from the client side let's receive Json because that's what client is receiving right so if you look at the client this is the data which you're receiving right so this is a question basically uh in the Jon format so the same format we have to uh send from the client to server now question arise if you receive Json on the server side do we have to manually change it no that's where spring says don't worry I'm there spring will say you just specify the Json I will convert that into an object but you have to also mention one more annotation here which is called a request body because you are sending this data in the request from the client side to the server in the body okay so in a request you have multiple sections in the body you'll be having the data let's use that and create this question I will show you how do we pass data now once you have those question let's say we receive the question how will you add that in database again simple uh you can use question service by saying hey question service add this question for me and the question is question okay the service will say hey I know you want to add questions but I don't have this method we can say okay I'm a programmer let me create a method which is ADD question uh this should return a string uh and then here I will simply add it now how will you add it the way you did find all the way you did find by category there should be a method in Dow which you don't have to Define right so let's try I will say hey question da I know you are Superman or Superwoman do you have a method to add it oh we don't have ADD but do we have some other thing we have save in this save just say question and done there's no error okay this looks cool but we have to also return something right so what I will do is let me see if question save returns something so save returns uh the type S extends T the type okay so it basically works with the type of data so what I will do is I will just return manually string here and say return success you know we should also return failure if something goes wrong but yeah we'll do ex ra stuff you know how do you handle exception how do you work with the status code as well because till this point we are just working with data right but if you really want to use a power of HTTP we have status codes as well like okay okay you might be knowing about 404 right not found so basically all those are status codes like we have 200 for okay we have 500 for server error we have 400 series for the uh return data error so there there are different type of Errors which we'll see later but but at this point we are just ring success nothing much and okay we are ring success here in fact you know we should return things which are coming from the Dow so I will not return success I will return I'll just cut this part and paste it here the success should be returned from the service itself so if I go back to service this is responsible to return I don't know when I shifted to the next tab something went wrong there but I wanted to write success in uh in add question in the string okay uh this looks good there's no problem which I can see even if you get some errors we solve it don't worry okay so this is oh okay main thing is missing here we have to set the mapping as well right now what type of mapping you're going to get here the thing is when you fetch data from a server we normally go for get mapping which is get method when you want to send data to the server we use something called post so here we have to use a post mapping and in this post mapping we will be specifying the the address so we are posting a question and then we are submitting data right so question should be uh okay the main is question maybe I can say add yeah so let's say I'm using add here so question SL add will add a new question okay so now will this work that's the question oh not intended okay so what I will do is I will just go back to my we have to on the server first let's restart the server and how will I get this how will I run this now basically in the address in the browser basically what you can get is you you get the address bar and you can get data if you want to send data you have to create a form or something like that to send Json and that's where the tools using which you can interact with the apis so that's right these are apis which we have exposed we use something called Postman so there are a lot of tools available let me introduce you to postman this is how Postman looks like you can download it from the Google it's very easily available and in the post man basically you can see we get this type of prompt if you get some welcome screen you can close it now here we can specify the method which we want to go with get post or something and here we have to specify the URL so let's start with the uh this one this particular URL let's paste it here say send and you can see you will receive all the data so you got data and if You observe you also got a status here which is 200 so if everything goes well by by default it will give you 200 but of course you can customize it you can specify some other number like we can have 201 1 202 you can do that okay but now I don't want to go for get I want to go for add how do you add something in that case first of all let me just copy one of the Json because I'm I'm very bad with Jason I'll just copy this and here I will say post because now we want to send data right and then we have to change the url as well which is question slash add that's what we working now okay but how do you send data because if you do that now if you say send you can see it will say bad request something went wrong here it is says 400 bad request we don't have or something went wrong with this path we can actually check what went wrong if I can go back to my intell J um okay there's no problem here did I do something wrong here so that's question add let me just send a request method not allowed because we are by default going forget so post is actually working we are sending okay we are not sending data so what I will do is I will go to postman so let's try to send data this time so you can see it says bad request so we are not sending data so I will go to body and click on draw or you can select Json here or I mean Json data and you can paste a dummy record so we have this thing we already have this question I will just customize it I don't want to mention the ID I want it to be autogenerated and I want to customize the question as well just trying to increase the size of this I don't want to see the history okay so this question need to be changed uh so I will say this question is maximum value for byte in Java so what's the maximum value you can store in bite so let's put some options there of course you can do that in a form let's say 100 we got 127 or we got 255 and let's say this is 99 9 the right answer is 127 if I'm not wrong and this is easy so let's say easy and done so once we have specified the Json data what I can do is I can click on send and now we got a internal error okay let's see what's the eror is okay it says the id1 already exists duplicate value for primary key I've mentioned the sequence right so should I go for identity let's try with identity let's try click on send okay we got it so basically instead of using uh sequence we should be going for identity and you can see it says success I'm just curious to know the uh the ID for that so I will say get I want to get all questions and send okay you can see we got 17 so it is automatic so we have to say identity there because that's what we have configured in the database so that depends upon what type of column you are creating a database right right so 17 works and you can see we got the question as well and that's how basically you add so to add you have to use Postman or any API tool you want to use so we are saying we are fetching all this data cool anything else we can do here looks good so yeah we were able to fetch data by category and we were to we were able to add data as well now for this we can also use update and delete so if you want to use delete you should be using a delete mapping here okay so try that as assignment okay and let me know your answer in the comments so instead of using post you have to say delete mapping instead of using uh for update you have to use put mapping and we have methods as well so in Dow basically if you go to question service basically let's say I want to delete this so I can say question Dow dot there's a option of delete as well and there's the option of update there's option of oh in fact we say save so save is used for both update and save so I hope that makes sense so try it out and let me know in the comments now is it complete not exactly we have to do a lot of different stuff first of all we have to uh add exceptions here if something goes wrong we have to also specify the status codes so for that we are going to use response enti entity and then we have to also use question service or the quiz service because questions are done the quiz is remaining right how do you create a quiz how do you play the quiz playing will not be doing maybe let's see how it goes so in the next video let's try to add certain exception handling stuff here okay so now we have a project which is working right so basically from the user side we can get all the questions we are able to get the question based on a category and also we were able to add a question right so now this almost sums up our question service now of course we will enhance it so that we can handle the exceptions which we have not done till now and also we want to work with HTTP response status codes the thing is if you have ever went to a website of course you do that right sometime when the website is not available or when you enter a wrong link you get a error right which is very famous 404 now what are the stus codes and to whom it is helpful of course as a normal user when you look at these numbers of course 404 is very famous so it makes sense to remember it but there are multiple status Cotes which you don't have to by heart as a user it's the job of a developer to show some perfect m message or a good message to the user hey this is what went wrong right and normally we do that on the client side So based on the response which we receive from the server a a client side application will generate some message right so let's say if you request for uh a resource and the resource is there you will get a status by saying 200 which is okay you got the response so the status is okay here uh what if you got a error the resource is not there it is 404 right so file found so instead of printing 404 we could have print file not found right uh in the same way we have multiple status code so let me show you that here now we are on to the Mozilla documentations and you can see we have different status codes available here uh so basically we have five uh we got informational responses which is which comes under 10 I mean 100 to 199 and then we got successful response which is 200 to 299 uh redirection messages 300 to 3929 client error 400 to 499 and then server which is 500 to 599 so basically if you talk about uh whatever information you want to get you will you can send in this when you get success in terms of resource resource there resource got created anything successful will be part of this 200 series what if the there's something wrong with the client side I mean as a client you made some mistake but maybe entering a wrong path or something like that so that's where you will get for for what if something is wrong on the server side there's some bug something arised so that's your 500 Series in fact if you want to get more details we have more here example we got 200 which we're going to lose a lot so let's say when you request a data from a server and data is there you got the data so with the data also send the status code that hey it is 200 if you don't want to remember the numbers you can remember the names okay so 200 is okay what if you want to create something on the server let's let's say we created a question right so when you create a question the response you should get is created so that's what you have to respond from the server side and list goes on there are multiple options here which we are going to use uh in fact you can also specify 400 errors which is bad request whatever a client is trying to access is not there on the server maybe the client is not authorized to do that uh forbidden error not found ER if the source is not available and Method not allowed example let's say on the server side you're saying it's a method but you sending a post request so it's a method not allowed error and then we can also go for 500 Series in which there is something wrong with the server internally and bad gateway so something something with the server side okay so let's get back to our code so what we're going to do is first let's try to handle the exceptions and then we're trying to handle the codes so what I will do is oh before that we have one more thing we we need to also specify I mean how do you specify the status code so let's say uh you are into question controller right and now here you are responding with all the questions now at this point we are returning a list of questions so we can return only one data right how do you return data as well as status code to the client and that's where we got something called response entity so instead of returning a list of questions what you can do is you can create a new object here which is of response entity because that's what you're rning now response which is in a format of entity now it will have two parameters basically the first one is the actual data which you want to return and the second one is the status code so in this case when you are fetching data I can say HTTP status dot you don't have to remember the numbers you can simply say Okay so this works so in the Constructor of response entity you are passing two things the data and the status code okay it is giving you an error is because what you are rning is not matching so what I will do is I will just just change this to response response entity and then we are returning a list okay so this is what you can do so what you are sending is not just the data but also the status code okay now either we can do that here or or also we can do that in the service the reason we will be doing that in service as well is because we have to handle the exception right uh something like this let's go back to service and here I will do it for get all questions so here in of the list of it we can also say response entity here because I want to handle the exception as well so it will make much more sense to do that here and instead ofing this we can say new response entity and then we can pass it here we can pass two parameters in fact you have to pass two parameters the first one is the data because of course the find all is giving you data and the second one is the status so HTTP status. okay right now since we are doing that here we don't need to do that in the controller so here we can simply return the data itself which is which you are getting from the uh service because service itself is giving you response entity right because if you see all get all questions it is actually returning response entity so you don't have to write that there okay but this is a good scenario right so if everything works well it works well but what if you got the error now that's why you have to handle the exception so what we're going to do is we'll be opening a tri block and we'll put that inside a tri block and then we'll write a catch as well if something goes wrong I want to print the exception message on the console I can say e do print stack trace and then I also okay see the thing is we can return in try but we have to also return what if something goes wrong in that case this will not be returned right so what you can do is you can just copy this and paste it here if something goes wrong you can return a empty array so you can say new array list not array array list so instead of returning the questions we are returning an empty array but the status will not be okay the status can be here the bad request right so that's how we can handle this so we are handling the exception and we also using response entity here now with that basically we are using status code as you mentioned HTTP okay I mean HTTP status. okay HTTP status. bad request now if you go back to the controller here we don't have to do anything we can just do this we can get all the question return it so now the advantage of this is if I restart the server and if I go back to my Postman okay this is the this is the request we were sending right so now if I send this request once again you can see we got the data but with data we also got 200 okay right and that's what is working but what if you make some mistakes here uh maybe you are not authorized maybe you have done something in be behind the scene uh that only the authorized user should be able to get that and then a user is not logged in in that case you can you can return uh unauthorized something like that so that's that's where it is helpful uh we can also try it for other methods which we have uh example we got category here so instead of returning a list we can return a response entity we can put that here and the same thing we need to put uh we need to return in this format of okay we are doing that in a controller okay we need to do that in controller but also I need to do that in service so if I go back to service here so get get question by category so here we have to responds with response entity and then we need to return an object of response entity just put that as a parameter and the status code so I can put status code here which is HTTP status. okay right but of course we also need to get try catch here so I can simply copy this code from here and since I'm lazy and then we can replace the statement here in fact it will make much more sense when we do it for the ad of course for ADD also we will be sending a response entity but this time the type will be of type string uh the the primary data type you have send you're sending and then here we have to say new response entity so you will send the data okay no we will we are doing that from the service right okay so the job of service here is to respond it now right so where is ADD add is here so in this basically we'll be sending a response entity with the angular brackets and then uh you will be returning not just success so now we have to return the new response entity and now success becomes a part of that object now which status we have to specify here so it should be HTTP status dot think about this okay now since we are creating data on the server it should be created as we defined earlier okay looks cool right now let's try to call this particular method of course with the same data which we have here so I will say Post in fact I have a history here for the question add and then we got body let me say the maximum value for uh short I don't remember the short maximum value it should be 63 hopefully doesn't matter right so let's let's say one uh 2 3 4 and the answer is let's say three I'm not I don't remember at this point and I don't even want to calculate if you're thinking I should calculate and do it I'm lazy so you can see I'm sending data and I'm sending a post request and now when I click on send okay the thing is we need to restart the application see if you do that if you send this data without entity response entity it says 200 but we want 2001 which is created so we just have to restart the server with the new changes okay everything looks good and let's create the same question again I don't know it will create an issue no okay you can see we got success and this time the status code is 2 20 1 so the client side of course a client will not you not be using a postman right they will be using something fancy some UI tool mobile mobile or some react application which looks good uh in that case as a UI developer it is your job to accept the status code and display the appropriate message on the conso on the screen for the user so that's how basically we use this okay so now once you understood response entity and exception handling here uh of course we have not done the handling here but yeah you can you can take care of it so that's your assignment complete this part write the exception handling for this now in the next video let's try to create a quiz application where a quiz service will be using a question service to generate the quiz so see you in the next video okay so now we our question service is all set so basically we got a cred operation on uh question right so we can create question we can read question we can update and delete of course we have not added the update delete option here but then you can do that right so that's not important for this videos now if you go back to question controller we have worked with all questions we have worked for the question with category right so we are able to get all those things now the point is why we are doing all this thing is because we want to create a quiz application so let's say we have two different roles here we have an admin and then we have normal user as an admin I can create a quiz right so let's say I'm also a trainer so tomorrow I'm going to take a session in which after the session I want to arrange a quiz so they before I will just Create a quiz now the moment I reach there on in this session I will start the quiz right so basically the creation of quiz is handled by the Admin of course we are not going to add the roles in this particular project but imagine if you have something like that you can do it but we'll give an option for someone to create a quiz and now let's say I'm going to start a quiz so as a user they will fetch so once a quiz started they can fetch a quiz on their client application it can be a browser or it can be a mobile application and they can see the questions on their screen that's the entire idea here right so basically we should be able to create a quiz now how do we create a quiz let's say we have so many questions right if I go back to my post postgress which is here you can see we have so many questions we got 16 questions of different topic of course we have Java and python here now basically I want to fetch let's say Java questions right and we can also specify what difficulty level you want but let's say we want to fetch Java questions and five Java questions basically so from a client side you can or maybe when you create a quiz as a quiz Creator I can say Okay I want to create a Java quiz with five questions so my application will randomly pick up any five questions out of this of course we don't have to go in sequence it will pick up any five questions and it will be creating a quiz for you that's what we want to achieve right so how do we do it so in that case if I go back back to my IDE here we are working with the question controller we are working with questions and all the stuff which looks good so what I will do is I will just close all these windows because I want to focus only on few now so basically the first thing you will do is you will create a controller now how do you want to create a quiz so maybe I will go back to my uh browser or maybe Postman and here the URL I want to use is let's say something like this I want to create a quiz and then this quiz will be of type let's say the category or we can also do this something like this I want to create a quiz SL create question mark uh of course you can do it in the address bar you can send data in the address bar why why do we have to send data we have to specify right what topic you want to select and how many questions you want so we can do that on the browser or you can basically send from the Json okay so initially let's go with the browser version so I can specify the category here so the question mark category is equal to uh Java and then I want also want to specify so I to say and number of questions so I can say number of questions which is equal to let's say five and we can specify difficulty level as well um or we can mention a title so let's say every quiz needs a title as well so at the end I can mention the title equal to J quiz now basically this is what I want right I want to create a post request and then this is what I want to fetch when I click on send of course it will not work you can see we got an error here which is not found so let's create this one how do I do that I will create a controller here and we'll say new class now this will be a quiz controller so let's click on enter now you can see we got a quiz controller here so let's do the housekeeping stuff here which is uh this is AR rest controller let's specify the request mapping now this will be happening for the quiz right so the URL starts with the quiz okay so once we got our controller here we need to create a method right because we want to uh accept because if you go back back here with the postman we need a method for create right so we have mentioned quiz now we have to create a method for create here so I will go back here and say public now once the quiz is created of course I want the response so I will say respond response entity and I want to return a string and the method name let's say create quiz now here we are of course we have to mention the get mapping here so let's do that quickly so get mapping no this this is not get mapping this is post mapping right because we are sending a post request so post mapping and in bracket this is for a create URL so we got for quiz SLC create and then it is actually accepting few values right so you can see in the address bar we are sending the category we are sending number of questions we are sending the quiz so let's accept this in some variables so here I will say so if you want to accept the UR L variables you have to use request param this first one is category the second one is is the number of questions okay number of question can be int right int you know we can reduce the number of words here just by saying nums questions or maybe num q that looks cool so let's have num Q here oh not request mapping it is request pm and then we we need one more which is at the end now again we have to say request bam and this is for string uh title right so we have to accept this values now now once we got these values in our method we we can use it to create a quiz right and who is responsible to get a quiz of course controller will not do it right we can use something called a quiz service we don't have it yet but I want to do that right so at this point I will not return anything I will just return the new response entity and here I will say I am here just to see if everything is working and then I will also return the HTTP status code which is okay now let's run this to see if everything is working fine I will just restart the application okay looks cool let me just go back to postman and say send okay you can see it says I'm here at least the URL is working of course you can print the values as well but let's say this is working but we don't want the I'm here message right what we want is the data uh we want to get a quiz actual quiz now who is responsible to do that so of course I will not be returning it here what I will do is I will ask my quiz service to do that so I will say quiz service Dot and there should be a method but unfortunately we don't have quiz service so what we do is we have to create a quiz service so what I will do here is uh let me create a quiz service class and we also need object of it but unfortunately we don't have this class so I can just click here and say hey give me a class in which package in the service package P click on okay and you got a empty class here now first of all this is a service so this is a service what else we need so again we know all those things and that's why I'm going bit fast but logic is important so here when you want to create a quiz of course we want to create a quiz and it should be stored in a database as I mentioned before I'm going to take a session tomorrow so today if I create a quiz it should be there in the database otherwise when I go tomorrow there I will lose all the data so when I say create a quest it should create create a table which will have the quiz ready now if you understand quiz better see we have questions right and questions can be of different topic we have for some for Java we have some for python maybe we can also add for cotland we have different topics there right now when I create a quiz or maybe for one particular topic we can create multiple quiz we got Java 1 Java 2 Java 3 so we have different quiz available for Java and it may happen that the questions are repeated in few um in some some of the quizzes so let's say there's a question number five which is there in the quiz one and quiz two as well now in this case if you want to store this data in the database how will you do it so basically we have a quiz table and then we have a question table now we have to map it as well right so there are different type of mapping available we have one to one so when we are sure that one quiz will only have one question that is one to one one too many one quiz can have multiple questions which is true in our case but then in one too many you cannot have the same question in different quiz but in our case we have that two different quiz can have the same question right so we have to go for many to many now in terms of many to many we create an extra table we'll see we'll see that in the database once we do that but basically we need to work with the da as well so here we have to say quiz da I will say this is quiz Dow and okay am I using Auto wiring here let's do that okay let's do auto wiring here as well at autowired and now we need this interface as well so I will click click here I'll say create an interface quiz Dow in the Dow package right and if you want this to work you need to extend jpa uh not index holder jpa repository now this will take two things the type of the table you're working with which is quiz and the type of primary key which is integer but if you want to work with quiz we need a class quiz right so there are so many things we need to create oh we don't have a quiz class so let's Create a quiz class I know I'm jumping here there but that's how you build project right you have dependencies and you understand what other things we require so basically we need to create a class called quiz which will be in the model uh do we have a model here yeah we have so you can see we got quiz here and uh our question was not in model let me just move question to model okay so what are the things we need in quiz so not much actually we need only three things the primary key for the quiz table we need a title because every quiz will have a title so next time when I want to start a quiz I should know which title I want to play and also we need questions so that for that particular quiz what are the questions we have and we cannot have everything in one table because one quiz can have multiple questions right so it makes sense to have it in different table so we call them as mapping tables right so what I will do is I will just create a prim key here so which is private integer ID and then private string title and then private okay now since we have one quiz can have multiple questions so it should be list of questions and I can say this are questions import the package okay but then this also need to create a table right so I have to first mention this as an entity and also we need to use lombok to create get and seter and okay why there's the error in quiz oh it should have a primary key so this should be ID and also we want to this to be autogenerated so generated value strategy equal to generated type identity okay see we have done this we have done all of this thing for questions and that's why I'm doing this fast without wasting time uh we need to import the package for questions done why this is problem with the questions questions are okay we're not able to import the questions is it it's the same package then what's the issue basic attribute type should not be a container okay I think it is because of the mapping so basically if you want to achieve many to many you have to use add theate many to many oh that was a issue okay so my idea is becoming very very smart now I thought there is some syntactical issue okay so you can see it says you have to do mapping which is many to many because we have a quiz with multiple questions right so the mapping is important okay once we got quiz we can actually create this quiz now and that is done we don't need to worry about this particular interface now let's go back to service now it is the service responsibility to create a quiz for me so now question arise how exactly it will create a quiz so let's try that so first of all I will create a method which is the method which I want to create here so if you can see okay what I will do is I will not be writing from here let me go to the controller where I want to use this method and I can say I want to have a method name which is create quiz and this will take uh three parameters category the num q and the title right so this is the method I want now the advantage is if I click here and if I click on create method it will give me that method I don't have to type that manually and I can simply Create a quiz now now to create a quiz what are things we need we can simply create the object right we can we can say quiz quiz of course we need to import the package for this we can say quiz quiz is equal to new quiz and then we can set the values right so the quiz is we'll have set ID which is the okay set ID should be autogenerated so we can say set title title is coming from here but then quiz also wants questions right because we have three three things there we got ID which is autogenerated so we don't have to worry about it title is something we have specified but what about the questions now this questions will actually coming from the database right so even if I say set questions you can see we don't have that here so we have to get the questions first how will you get the questions again simple you can ask your Dow to do to give you that so I can say I want a list of questions I can say questions we need to import the package so if I say list of questions here and now we need to FES that from database right so I can ask my quiz da okay not actually quiz da you know the thing is the questions are coming from question da because the questions responsibility is for the question down not the quiz down so we need to get the hold on both so first let's get the question now because that's where the questions are now we cannot say find all the thing is when you say find all it will give you all the questions what we want is specific number of questions for a topic so what we can do is we can create some method we can say find random questions by category okay so what we are saying is give me questions and we also give me questions limited to let's say number which I have specified here five questions two questions and then with a category so of course you have to pass these two things you have to pass the category and you have to pass the number of questions you want right and we don't have this method if you can see the Dow is not having this method let's create one and we just have to create the interface right now you're thinking oh this should be easy right because the jpa gives you everything see jpa can give you things till a point right after that you have to get the data from by yourself and that's why you have to write the SQL queries now don't worry we'll not be writing SQL query we'll be writing the native queries here now how do you write a query so for a particular method if you want to execute a particular query you can use something called a query annotation you can see is coming from data jpa and in this bracket you can mention your query so basically you have to specify two things one is the query and second is it native yes it's native but what is a query now that's where you have to write the jpql query here so we'll say select start from question that's our table name Q where so basically we have to fetch question from the uh question table but then what should be the we Clause first of all the category should match with what we are passing so I can say Q do category need to match with my category so that should be equal to okay how do I specify this variable here now if you want to specify the variable because the value will be coming from the variable right you have to give a colon so colon category and then now you also need only five questions so basically we can say okay let's order bu because we want to do it in random so we can say order bu random so they are the in build functions so we can get random questions okay we will get random but how about limiting the numbers so that's where we'll be using limit of course teaching you the SQL now or the query language now will take a lot of time so I'll just simply use those data here and that's it you got your query okay looks goodt let's see if something goes wrong we'll debug it so that's your query and now once you got your query this will work now you will get your questions and you just have to add your questions in the service so we have done with that so once you got your quiz ready because this is what will create a quiz right now once you got your quiz ready you just have to use the quiz Dow to save the quiz so we'll say save and we are saving this particular quiz right so we have generated random questions with the title and now you are saving it okay looks like it should work but you have to return the response as well so I'll return new response entity we can return a message success if everything goes well of course we should handle the exception as well but let's do that later https dot created okay looks like we have done with the create Quest but is it really working first of all we have not okay we don't do mapping here we can simply do mapping that's done create done okay I hope this was work let's try restart the application go back to postman and say send okay we got success you can see we got two1 but I'm not sure is it really worked in the first attempt let's try go back to your tables you can see we only got one table here but if I refresh okay we got two more tables the quiz will have quiz right so I will say all rows But If You observe we only have two columns here we got quiz ID which is one autogenerated and this was a title right the quiz uh J quiz or title if you remember in Postman that's what we have done here J quiz and but what are the questions for this particular quiz what are the questions and that's where we have a separate table if you say view data here and you will see the quiz created so basically we have for Java and question number 18 8 17 6 and 19 okay do we have all these questions are Java remember this numbers uh 18 8 to 17 6 19 let's see if everything is going well I will just say view all rows I think 16 17 18 was that 16 okay okay 17 18 19 was that that's what Java and 6 and8 okay so we are right so basically this is working let me just verify once again it is start it should start from 17 yeah there's no 16 here so yeah it worked so basically we were able to create a quest with five questions now how do you fetch this Quest what if you want to you have to specify the quiz ID and get all the quiz questions how will you do that we'll do that in the next video and also we'll try to handle the exceptions and make it work better so we were able to create a quiz now it's time to fetch that quiz so when we create the quiz the data goes into a database right so we have created two extra tables so we already had a question table now we created a quiz table and then we created a table where we can have all the questions for a particular quiz just to show you that what I can do is I can go to my PG me you can see we got this table here which is quiz and uh quiz questions and if I open Quiz here we can see we have uh different columns we have ID and title and then in the quiz questions we have uh two columns one is quiz ID second is quiz questions okay so basically in this every quiz will have multiple question that's what we are saying here right but now what I want to do is I want to basically fetch this questions so let's say as a normal user if I want to attempt a particular quiz how do I fetch the quiz so I can mention the quiz ID and then I will get all the questions that's what we want to achieve here to do that what I will do is I will go back to my intell and here let's create a method so basically we have to do that in a controller now what type of request I want to send so of course it will be for quiz instead of create we can say get right and we have to also pass the ID uh so example if I go to my Postman this this was for creation of quiz right which we have already done so now this time instead of creating a quest we will say get the quest so Quest SLG get and I can specify the ID of the quest 1 2 34 of course when you have a good UI you can basically see all the quiz title and you can select one at this point we don't have a UI so let's get used to the numbers here and this will be a get request so if I click on send okay it says 404 at this point but we want to make it work we want to return the data so what I will do is I will go back to my intelligent and let's create this method so I will say public it will say response entity because that's what we are going to use now and this will return the list of questions right list of questions and we'll say this is get okay we have to import for list as well I can say Get quiz questions and basically we have to also pass the ID right so whatever ID you receiving from a client side we basically have to use it so I will say this is get mapping and I can say this is for get so for the get request but also we'll specify the ID as well right and that is going going into the curly brackets because uh the ID may change in future someone wants to fetch the ID 1 2 3 4 doesn't matter so here since I'm using a variable we have to use path variable and then this should be of type integer ID so of course whatever data you will receive here will be stored in this ID now and then based on that we have to fetch the quiz now here's a Twist The Twist is when you say you are fetching the questions of a quiz the problem is on the client side you are sending questions that perfectly make sense a question will have a question title a question will have options a question will also have the right answer because if you see the question model or question class here it also has the right answer I don't want to send this to the client that is risky right so what we have to do is we cannot send that so let's create a wrapper of this question which will only have I mean which will have all the columns except the answer and the way you can do that is by creating a new class and we'll name this as uh question wrapper and then uh we just need to put few variables which we have in question so what I can do is I can just go back to question and I want private ID I want this I want this this okay I think this number of things are enough we don't need right answer we don't even need difficulty level because we are fetching the questions right uh so we don't need that and also we don't need category so what I can do is I can simply go back to question wrapper and paste this here and since I want to use Longo so that I can I don't have to define the geta sets and also I need a parameterized Constructor so we can use lombo or let me just generate quickly here generate a Constructor which will take all these parameters now the advantage of this particular class is if you go back to your quiz controller okay I don't need this one no I don't need question so if you go back to your quiz controller here we are not returning the question class we are basically returning the list of questions which are question rapper which will have questions and options but not the uh answers and now basically how will you get the question of course is the job of my quiz service to give me the questions I will say Get quiz questions and I can pass this item ID whatever ID the quiz ID I have okay we don't have that method there so I will say return because return is missing okay so let's create this method so I will just click here and create a method in the quiz but what will be the logic now it's it's actually very simple we just need to fetch the quiz object from the database I think our job will be done and how do you do that so of course we do have the quiz da right so let's use the quiz da and if you can see the quiz da will ask you for if you want to get or to say find by ID and the ID here is ID itself and that's it it will give you the quiz right so let's store that in a quiz object so I will say quiz quiz okay there is some problem here it says this find by ID might not return any data right of course if you have a quiz ID which is there in the database it will return data but what if you are mentioning a quiz ID which is not there in that case it will throw a null error right or null you have to B basically handle the null values and that's where we got a new feature called optional so we can say this is optional data the data might come or not I'm not sure so let's say optional okay so what exactly we're going to do here we will get the quiz with the quiz we'll get the questions right because now the quiz object actually has the questions but then we have to convert those questions into question wrapper remember we are not returning questions we are rning question rapper so how will you do that so first of all let me get hold on the quiz questions so again a list of question question I can say questions from DB and this will get I will get from quiz so I will say quiz. getet so when we use optional we have to first get the object and then you can say get questions so get questions will give you these questions right so now we got the question from database but we can't return return it example if I say I want to return this data now we can't because the wrapper the list is of question wrapper not of questions so we have to manually convert each question into a question wrapper so we can use a loop here so let me create that object of question wrapper here so list of question wrapper I will say questions for users now basically this is questions from database which we got and what we want to return is questions for user and we'll say new add list so we got a empty list here and we just have to add data so what I will do is I will just return so that I will not get any error response entity ultimately we have to return the questions for user right and the HTP status as okay okay so but then before returning we need to add data because this is empty at this point we want to add data how will you do that so we can use a for Loop here so I can say for question Q in questions from DB so let's get hold on each question and then every time you will create a new question wrapper object so I will say question wrapper I will say this is qw equal to new question wrapper and then here we have to set the values remember in the question wrapper basically we have a Constructor so I can use that Constructor here so what are the values I have to pass I have to pass the title so basically I have to pass the ID first so where is my question rapper yeah so you had to pass the ID title and Option 1 2 3 4 let's do that quickly so here let's fetch the ID first get ID then we'll fetch do do get title then Q do get option one then Q do get option two then Q do get option 3 comma last one Q do get option 4 and that's it basically we created this object with all those values and once we got this object we just have to add this to the list so I will say question for user do add and we are adding this particular question which is qw okay so basically we were able to convert or we were able to fill this particular question for users with the help of the questions from database and I think we are good it looks good we can call this method and from the controller we are sending it it return data now I hope everything is set let's restart the server everything is done let's go back to our Postman and let's say send oh can you see that we got the questions now this is the same thing which we checked earlier right remember id8 id8 and all these are Java questions and what you got here is ID question title and options this is what a user will see now when user attempt this particular quest of course right we'll be having a particular UI and you will see one question at a time and then once a user has entered all the answers when you click on submit the data the answer data goes to the server and on the question on the server side we have to calculate then okay this scod and then we have to return but then we have not done for the calculation which we'll do in the next video but basically what we have done here is we were able to get a particular quest with a ID okay now once a client has received these questions and the client is answering that and of course a client will also send that data to the server that's why we have to calculate this code and for that let's see you in the next video bye-bye now it's time to create a function or a method which will calculate the result of course right when you submit this question on a client side basically because we were able to create questions we were able to create quiz and then we were able to get the quiz as well and now when a client want to send the responses on the server of course to get this Cod it's our job now on the server side to calculate it so what kind of response a client will send so let's understand that let's going let's go back here to the postman now if you say a quiz this is a quiz which we have created right the ID for this quiz is one and when I click on send here you can see these are the questions we have so in total we got five questions we got question number 18 question number 8 uh then 17 6 and 19 right now in our example the question number 19 and 18 they are same because we were doing some experiment and then we've added the same question two times and even if in random format it was able to get the same question that's weird but anyway the point is when a user is getting this questions and when they answer let's say for this particular question the answer is let's say three and then a user says two of course a wrong answer for this particular question let's say the answer is I'm not even reading question let's say the answer is D and then a user is selecting D now in that case it is correct answer once a user get the data from from the client side when a user submits the response how you can send that to server so what you can do is you can send two things see we we don't need all these things when you sending the response right you need only two things first first you need the question ID because that's unique for every question and you need the right answer which a user has selected so not right answer but a user response so can I say we are sending two things to the server where you have ID and response something like this so we will be creating a new request which will be for submit not for get but for submit and we have to mention the quiz ID so for which particular quiz a user is attempting that so let's say the quiz ID is one but now we want to send responses right now for the responses what you can do is you can just click on draw here and we want Json format but the data which we want here is different so of course we we are going to send the list of responses so we'll use list there and then each response will have an ID so let's say the ID in this case is okay we have to get the IDS as well so the first ID is 18 so the ID 18 the response selected from the user so we can say response colon whatever the response was uh now what we will do is first of all we don't know the answers right and what I will do is I will just remember this I'll try to remember this where we got 18 uh let me just write it somewhere I will write it here so we got 18 8 17 6 and 19 of course I do remember the answers for this but you should never trust your own knowledge you should always confirm from the from the database right what is something was wrong with the database itself so what I will do is I will just try to fetch the answers from database the way you can do that is by going to the pg admin and here we can fire a query so basically we want the answer right so let me just I don't need this I can keep it here so can I edit this query no let me just go to query tool so basically I'm into post PG admin now and I want to fetch the ID for particular question and I want to fetch the the right answer for that particular question I think the right answer column name is right answer if I'm not wrong let's try from question where in fact let me just put where below here where uh ID is equal to so now we need all these IDs here right so I can just paste it here so ID is equal to 8 or okay so ID can be 18 ID can be 8 17 6 and 19 so that I will get all the answers here and run this you can see we got the answers right so for question number 18 and 19 it's three so it's that becomes easy question number six is this question number eight is this and question number 17 is this so normally we are sending the response itself maybe you can customize it and don't send the answer you can send the option number so a b c d that that works but there's only one problem if you want to randomize the option so on the server side let's say you have a b c d in sequence the answers or the options but then when you're sending that to client maybe you want to randomize it there in that case it will not work so it's good to check the right answer instead of checking the options okay so this looks cool uh we got the answer here so what we will do here is the response for 18 is this I will just copy this response for 18 is this and then I can just copy this I'm just trying to recreate this so fast forward I will complete everything now so as I mentioned fast forward we got all the options in fact we got the response created here right now this is what you are going to send on This Server so example when I click on send it should basically calculate the values and send the result so how many are correct now at this point I have entered Ed all the correct answer so of course the score should be five right that's what we want so how do we get this so it's very simple let's go back to our intj so since we are sending a request for the submit so I will say post mapping and this will be happening for submit and we can also accept the quiz ID so let's say this is the ID for the quiz now for this we need to create a method so I will say public now what we need to return so I can simply say response entity and what we want to return is a score right so we can return score with the integer value and I can name this as submit Quest and what are the values we have to accept basically we have to accept two values one the ID which you are submitting here the quiz ID and second is the responses right so let me accept that here now this is a path variable so I will use that and it will go into integer ID and next we need to accept the body right now this is getting sent in the J format so it will be request body and then we'll store that in a object now which object now basically we need a object which will have two values the ID and response so let me create a response object so I will say response response and we are not sending one response right we are sending multiple responses so I have to say this is a list of response but unfortunately we don't have response class right as you can see we don't have response so we need to create one but why it is taking response respon is it okay there's a inil class called response and that's why just taking it but I want to have my own class of response okay let me create a class here so basically in this particular model we can create a class called response so I will say this is a response now this will be a very simple class just like question wrapper the thing we need here is so first of all I want to use lombok here which is data and then I also want a Constructor so I can say required argument Constructor it will give me parameter wise and default Constructor and we just need two variables so I will say private integer ID and private string response so basically we got the variable name as response as well uh so we got response class in which we have response as a variable I know that sounds weird but we can we can go with that okay so we got this class right and once we got this class I can just import that class here control space and importing done now once we got the response we need to calculate now how will we calculate so basically that's where your service class comes into picture so I will simply say return and I will ask my quiz service to calculate or maybe I can say or maybe I can say calculate result and then we have to pass the ID the quiz ID and we'll pass the responses of course right you will not be doing any calculation in the controller so let's do that in the service and let meate create this method in the service so method created now this is where the actual fund happens right uh let me just verify the controller once if everything is good okay looks good let's go back to service and this is where you have to do the calculation so here we need to add some Logic the first thing we have to do is we have to get a quiz object because that's how you will get the quiz right because in one quiz you have multiple questions so with this ID here we can get the quiz questions uh so I can simply say I want to get this from data database I will say quiz dow. find by ID so give me that quiz question and I have to pass the ID here now the only thing is find by ID will give you optional uh data so we want the data so we can say Get So when you say get you don't have to put optional here which we have done before if you can see we have used optional here but that's your choice the better option is this because here you can check if it is present or not it's better to handle this way to write a better software but since we just are learning learning phase we can try this now once we got the quiz it's time to get the questions from the quiz and we'll put that in a list so I will say list of question and I will say questions is equal to this will be coming from quiz so quiz. getet questions okay now once we got the quiz question now it's just comparing the values in the response and in the questions that's it that's the only thing you have to do but how you do it let's try so of course we have to iterate to The Loop and I can iterate with response I can say response response colon responses so basically we have to just iterate from the list and check each answer now even if it is right where you will put it so how do you count the variable so what we can do is we can say in right this is a variable which you're creating and default value will be zero and then every time you see a right answer you will just increment it now how do you compare so basically basically we have to check if my response. get response if it is equal to uh the question so we have to get the first question from the question right okay so responses and the quiz will be on the same format so basically we have to compare this response from the questions right uh we have the question here so what we can do is we can also use a variable to iterate maybe you could have just use a normal variable even that could have worked so I can just say every time I do this I just have to say i++ here okay not in the if condition of course and then here we have to get the question right so I can say questions. get with the index one or with with the index I and then we have to get the right answer so we just have to compare with the right answer if it is successful then we can say WR Plus+ that's it okay I just hope hope it will work because we are using i++ and bugs are a part of a life right it says one more bracket missing is it so I need to close this one then I need to close this one done okay why there's a problem here okay so basically once you got the response the right answer you will return the value so you will return the new response entity object which will have the right answer and also the status code which is HTTP status. okay now let's verify now since we have all the right answers the output should be five that's what we expecting so we start properly done let's head back to our Postman and say submit oh we got five now intentionally I will just make one question wrong let's say this is not four this is not three this is four so that's the wrong answer I've given send we got four okay let's make one more wrong uh let's say this is 128 I'm not even know the other answers but other options but let's try send you can see we got three so that's right we were able to send the response on the server and we got this score okay so we are slowly building this thing right in fact this is the only thing you you do uh so we got all the services which we need for the microservices so what we have done till now is building a application where you have everything in one project of course this project is simple uh in Enterprise world we make complex project but that's not important right the logic is not important important is how do you break down this project into small parts how can we differentiate between the quiz service and a question service so there will be two different services like microservices and we'll see how do we interact with them so that's your monolithic quiz application so that's it from this video where we were able to create this submit this and I will share this project on GitHub as well you'll find the link in description but we'll enhance it so that you can make a microservices out of it so finally we are moving towards microservices of course we have talked about the theory of microservice then we move towards building a project now the project which we have built is called a quiz application and it was a monolithic application the reason we started with monolithic is it will make you understand how do you move a monolithic application to microservices and also it will also help you to understand that why we have to go for microservices the thing is whatever application which we have built now is one big application right of course it's a small application but imagine it's a big application you have multiple Services now what we have done is we have a application which we have done if you have not seen the video the entire project that's fine I will give you a complete walk through uh somewhere in between and then we can get started but just to get started here what we have have is we have a quiz application right and in this quiz application if you can see we have two controllers one is your question controller and second is a quiz controller so let's say if you want to create a quiz now of course a quiz can have multiple questions and then you can also select which topic you want to uh build a quiz on so maybe you want to give a quiz on Java or maybe python or maybe any other subject doesn't matter and also you need a way to add questions remove questions update questions and also read questions right now that's what we can do in the question controller now since everything is in one place in one application imagine you know imagine this application has multiple uh Services now apart from questions and quiz let's say we also have user service where we are asking user to create an account or maybe we also have a payment Gateway so that if someone wants to give the quiz maybe they have to pay some amount or maybe we also want to give them a certificate so we we have to all those Services now when you do everything in one application that's your monic application right what we want to build is we want to build microservices so each service which I've talked about like question service quiz Service uh user service maybe we have certificate service these all are individual service and they are running independently they're not dependent on each other of course if you want to give a quiz you have to first generate questions right you'll you will get the quiz from the questions right so that's what we want to do so the quiz and question will communicate and of course quiz will also communicate with the certificate right because once you complete the quiz you have to also generate a certificate maybe quiz will also ask for the payment so everything is connected here right so or maybe they're connected with one big thing which is which is quiz here but the idea is we have to create small Services which will run independently now is it that easy to convert this project into two parts example let's say we want to make sure that the question and the quiz will be separated so there are two different microservices of course course you can you can have some more microservices but just to keep it simple let's say we have two microservices and here when you talk about the quiz service basically it will it is going to use the question service now what we were we had before in quiz is this so basically if you want to create a quiz you can use a create URL and then you can create a quiz you can use you can get a particular question number of questions from a quiz by specifying the ID let's say uh I take different sessions I teach Java I teach python I teach let's say a blockchain so what I can do is for different batches I can create different quiz so quiz one quiz two quiz three now that's for this week next week again I have to create three different quiz so that will be four five six right so that's my quiz ID so if I specify a particular ID it will give me all the questions for that particular quiz and also we can also get submit the quiz basically so let's say if participants are attending this quiz and they have submitted some response we have to calculate a score and that's what we are doing in the quiz controller but then if you say generate or Create a quiz you need to get questions right and that will be done with the help of question controller so you can see here the question controller has the option of getting all the questions getting the category and add but if you see nowhere we are basically giving away the questions now that's something we have done in quiz itself because if I jump to the quiz implementation this will make sense more when you have seen uh the previous videos so if you if you click on the create quiz here so you can see this is where we are generating the questions and that is coming from the question that means the quiz and question are integrated tightly these are tightly dependent components we have to find a way to make them decoupled right so basically we have to make we have to make sure that they are decoupled right now to do that we have to make some changes now what are the changes we have now first of all when we talk about this create quiz which is here we don't want to use question doubt directly what we want is the question controller or the question service will be separated it will have its own database the weird thing is or not exactly weird but the thing is if you open your PG admin because we are using post database here okay it's asking for my password let me enter that and let me look at the database what we had so if you can see we had only one database which is your question DB right and in this question DB we had different tables if I see the schema uh here we had different tables here right so what we want to do is not just Separate Tables now we want to have separate databases so question service will have everything related to question and quiz will have everything related to quiz here and they will have a separate database that also means that when you go back to the code the quiz application or the quiz service will not be having question Dow so let's say there are two different servers quiz service and question service now when you say hey I want a quiz the quiz service which is there in other server will request to the question service hey I want want few questions right so basically it is not in the same database they are communicating over the network so that's something we have to understand of course there will be a lot of changes in this and we'll understand those changes one by one so let's say we have separated the two different Services now the problem is it it is possible that if you want to scale up this application normally what happens is when you have one big application you scale the entire application right but what if you have different Services now in this case if you think hey you know uh the quiz service will need more instance than question service let's say for an example now in this case you can actually replicate or you can actually have multiple instance of a quiz service but not question service let's say question service we have two instance for the quiz we have 10 instance let's say I'm going to a big conference where we have 2,000 participants maybe I want more instance of it so that's something we have to understand that we can scale them individually also now since we have multiple instance they will have different IP address right how do you search so let's say now we have question service right have we have two instance and quiz now have five or 10 instance let's say when a particular quiz instance is requesting to the question which instance it will pick so we have to also do load balancing right between these two which one to select okay that's one what else about the first link so let's say the request comes from the client and then you have these two different microservices do you think a client knows which service to go to of course we don't want client to remember all the URLs right for the quiz and for the questions what we can do is we can basically send the request to one particular API Gateway now that API Gateway is like a door for all the services and that will decide where to go so that's one thing we need so we basically need load balancers we basically need API Gateway and also we only have two service right let's say we have 10 service each service will be able to call or any service will be able to call any other service how they know where they are how quiz service knows where the question service is do we have to remember the IP address or do we have to do something for that and that's where we also need to go for something called a registry where they can search each other right so that is called a service registry so we need API Gateway we need a load balancer we need a service Gateway and also what if something goes wrong what if the particular service is not working so a user is requesting for a quiz and quiz is dependent upon question what if question is down in that case quiz service basically waiting for the response right so we have to also do something called fail fast right so you have to say hey if it is not responding let us know that it is not responding so that we can find Alternatives right so you have to find a way to get the fallback you have to also respond to the client if something goes wrong and that all comes under this microservices you know building microservices is easy but connecting them and achieving all these things will take time and in the sub subsequent videos we we are going to implement each each of of them so we'll try to implement the API Gateway we'll try to implement the service registry and the load balancer and also the circuit breaker which will what is circuit breaker we'll talk about that later so now let's create our first service from this particular monic application and the service which we're going to build now is your question service so first of all if you want to build this of course we have to create a new project and in the entire series we are not going to work with one project we'll be having multiple projects so every micros service is one project in intj or any ID which you use right so there might be some confusion where which service I'm using so the recommended way would be to practice along the video so that you will not get confused if you're just watching a video and then when I'm switching between the projects you will get confused so when you are doing it by yourself you will know on which IDE I'm into right and also observe here this is where the application name is also there so we are into quiz application now once we create a new project this will also change so what I will do is I will create a new project so let me go to the same website which is start. spring.io and here the project which you're going to select is Maven type and the language is Java 3.1.1 this is the version I'm selecting I'm not going for any snapshot version and based on when you are watching this video you can select any stable version okay group ID I want to make sure this is com. Tesco and the artifact ID is now since we are building a question service so we'll say question service okay and then we can give the description here so that's it so we are we are building a question service basically here now as I mentioned before there are different uh Services which are going to build right we have a quiz service and a question service here we have to add some dependencies now what are dependencies we need so first of all we need web because that's a web project we also need post because we are building we are connecting with the database as well we need longbo because we don't want to write much of the code we want to uh get some code from the Lomo for the data sets and since we are connecting with with database with the help of jpa so I will also need jpa now this is something which we have used before as well right nothing new here now what is new the thing is when you are building the entire project we are we need two more dependencies one is your open Fin and again I will not describe what this does at this point we'll talk about this later and next we need is urea client okay again why do we need all those things we'll discuss that later at this point just say we have those two things and once I download the project I will basically going to comment it now I'm getting it is because in future we will need those dependencies and instead of copying them we can simply uncommented right so at this point just ignore it but when you are trying this on your machine also get this comment it as I'm doing and then we can get forward okay now after this we'll simply click on generate I'm just checking if everything is good looks skirt I will click on generate now it will give me a project in zip format I will click I will simply unzip it so unzipping done let me go back to my inj and here I will open the project so I will say open so basically we are into that particular file now and our project which is unzipped you can see question service and click on open now this time we'll do it in a new window because I want to use the existing window as well the existing project so you can click on new window now if you're using eclipse in Eclipse basically you just have to import the project in one Eclipse you can see multiple projects on on the left hand side so that's fine whichever ID we are using so we I will just click on the new window in fact I will say don't ask me again because all the project which I'm going to create will be in will be opening in the new window now when we do that this is the new window right now if you can see uh okay let it load first so basically you can see the project name here which is question service now if I switch back to other project this is quiz app okay just remember this is quiz app app this is question service okay so this is monotic and this is the micros service so it's downloading dependency which will take some time okay looks like dependencies has been downloaded if I click on Maven there's no problem here everything looks clean and then if I expand the external libraries you can see I think we got all the libraries required but as I mentioned before we don't need those two dependencies at this point right so will'll go back to the pal file which where you have all the dependency mentioned and I can simply comment these two things I don't need Ura server I don't need open fin at this point so comit it out and everything else will need that okay so we are just commenting it what next now of course I can build the entire project by typing but since we have already done that work right so what I will do is I will when you go back to your SRC and Main you can see Java is actually empty we only have one thing here which is the question service application now in this I will not I will need all those things of course we'll make some some changes to make it more like a microservices an individual service or a independent service basically so what I will do is I will go back to my project and you can see in this particular project which we have done before we had all these things right we have a controller in which you had a question controller quiz controller at this point what I will do is I will just copy this entire stuff and paste in here so you can see we got all the things and actually we don't need all things so if you see controller we don't need quiz controller in this this is basically a microservice for question so delete in da also we don't need quiz da because the moment you say quiz da that means you have a different we have the same database no we want to use two different databases right and uh in fact let me also do that once I delete all these things so let me delete we will need question we will need question rapper and we'll need response as well we don't need quiz so we'll delete that again we'll discuss why do we need question rapper and response later and in the service again we don't need Quest service so we'll delete that as well so delete and done okay so you can see we have deleted of course this project will not work because we have deleted so many things and one more thing since we have made the changes in the pal remember we have delet we have commented the things here so we have to reload this you have to remember whenever you make some changes in the palom file you need to reload your changes and once I do that you can see now it looks good we don't need P file anymore now let's close it now these are the things now one more thing we need to also create a separate database for the quiz right now since we are not working on quiz we are only working with questions so let's create the quiz database later okay I thought we'll create but doesn't make sense now we are only working on question service so let's only focus on question service now here if you go back to your controller of course you have all these things and we have to import the package uh lendy things right so now since the thing is we have changed this name right the quiz app and that's the issue so no no issue I will CL quickly import everything the reason I'm not cutting this part of the video is because I think you should know how do we uh import something if something is not there import done question Dow I don't think there is a issue there's a issue of of this import so let me import the questions question class as well and let me go back to question is it dependent on anything nothing looks good question wrapper dependent on nothing looks good response dependent on nothing looks good question service now it is dependent on a lot of different things let me remove the Imports which is not making sense and let's do the import here question Dow question and then if you go down things are looking good here okay one more file which we forgot to import is this the application. properties which is empty at this point so what I will do is I will just go back to my previous project application properties we'll need anyway we'll need all these things so we'll copy this and paste it here right so all these things we needed this is basically for post uh database connectivity right we need to mention the driver we need to mention which database we are working with which is question DB uh the post username the password for the ddl update for for the database table creation and then Dil okay nothing to change here but we'll keep it open I don't need wrapper response at this point so I will close that question also I don't need it now let me Focus only on the controller now what are the changes we need to do here the thing is in the earlier scenario oh first maybe I think we should first run this to check if everything is working with this settings we just need to say get all questions let's see if this works run okay yes I need lombok okay no problem you can see service started on 8080 so let me go back to my browser and say Local Host 8 080 slash all questions so it is working you can see we got all the questions but now we have also mentioned that we want to make this as a independent service at this point in the in the earlier project everything was mixed up right everything was there in one project but now we want to make it different so in the question controller we have to add some more requests so what are the things we need so what we will need is I will just write comments here first uh so first of all we need to find a way to generate the quiz the thing is in the earlier scenario the quiz was responsible to generate questions right but then it also needs access to the question database we don't want that now the quiz service will not be having access to the question database which is in two different servers so if question service needs a quiz it is the job of your question service to generate it so basically quiz service will request the question service hey give me questions the question service will say okay let me give you the questions and it will generate the questions so BAS basically I need to have a method here called generate or create your choice so I can say generate this is one thing this one thing URL we have to work on next we need to also get the question for a particular quiz let's say we have a quiz database right a quiz service quiz database in the quiz database it will have its own data about the quiz but it will not have data about the questions right for the questions it has to go to the question service so what if your quiz service says hey you know I have the quiz number but I don't have a question for it give me the questions so maybe we have to find a way to give them the questions based on the quiz ID or based on the question ID because we don't have the quiz ID right on the question database okay so it should be based on the get questions but it should be based on the quiz ID so I'll mention the bracket on the question ID not quiz ID question ID and also now we need to get this score as well you know why we have to calculate score here in the earlier project we basically calculated the score in the quiz service if you remember if you go down this is where we have calculated the results now we don't want to do that in the quiz service because in the question service we have all the questions even the right answers is there with the questions so in that case we need to do that here so we'll say get score so these three things we have to implement in the question service to make it independent now how to do that that we'll see in the next video okay so basically we need to do these three things right if you want to complete this we need to say generate get question and get score so let's start with generate first now if you remember in the earlier application which was our quiz app in this basically if you go back to your quiz controller this is where we were generating the quiz right now this time we are saying that it will request I mean of course this create will be there on the quiz Service as well but it will request the question service to create the quiz or to get the questions so what we can do is I can use something like this in my app so here the request will be coming for the generate so a get mapping and this the request will be coming for Generate as we mentioned before and the method name let's say public is get questions for quiz and it will return something right so what do we return here now basically if you remember when we talk about quiz application or the before the earlier application it was getting questions but this time what if we don't return questions we just return the numbers so that see anyway all the calculations is going to happen on the question service right so this getting score getting the questions anyway is happening in the question service then why quiz has to know about the questions we can simply return the list of numbers the IDS of the questions so we are going to return we're going to return response entity but that will be specific to the list of numbers so we can say list of integers okay and we are going to just return return it here but the question arise who will be getting a us questions of course everything we have to every time you want to need some you need something you have a question service right so you will say hey question service get questions for get questions for Quest and then of course we have to pass some parameters right even from the quiz service when it is requesting to the question service we have to basic basically specify some parameters here and when you say parameter of course if you want to generate a quiz or if you want to give a list of questions you just need two things tell me the category the topic name and tell me how many questions you need so I can simply say and this will be coming from the questions the quiz service right so quiz service will request to the question service and it will also pass these two values so we can simply specify request param we will need string uh category and we can say category name and here we can say request Pam and this will be your number of questions yeah know things are going on different line maybe I can just get this here this looks cool right so now we just need to pass these two values from the quiz service just remember that okay but then we have to pass this value here as well so I can pass category name and I can pass the number of questions and we don't have this method so let's create this method so I will just right click here or I will just click here and say create this method now in this method how do you get the questions okay now instead of typing the code what we can do is if you remember in the earlier project when we said Create a quiz and if I click on this this is the logic right this is what we wanted to do I can simply copy this because that's what we need nothing fancy create quiz and we are getting this so this is a thing right and out of this we just need this line because we anyway in the Dow layer if you see in the Dow layer so I'm into the quiz application the monic application in the Dow layer not of this the question Dow we had this thing right we just need to get the questions for the category based on the number of questions we just need to get this anyway in our application we have that so what I will do is I will go to quiz service I just need this line of code I can just copy this go back to our new application which is the question service and paste that's it this is the thing thing we need now we have to make some changes the changes of course we have to pass the category name and the number of questions right now in the da of course it also has a DA here so if you see the DA for the question da we already have that particular method here the difference is this time we don't want to return the questions what we want to return is the question ID so that will be integer and here we don't need to return the star because star will give you everything we just need the ID so q. ID and with the same thing so we'll need the category we'll need the number and based on that I just need to F the question questions that's it now this will return the list of integers right so in the question service it will return the list of integers and oh this should not string this should be integer I made a mistake here this should be integer and here also so we have to say integer right and now it will it will work and the only thing is we have to convert this into a response entity so we'll say return new response entity and then we have to pass two values here the first one is questions and the second one is the status so we say status. okay and that's it so basically if when you say generate quiz this is what we need to do okay and now so that's question done generating questions done so next we need to do something for the get questions now get question is post mapping and here we'll say get questions now why do you need to Define this this is very simple this this is basically if a quiz service is requesting for the questions for a particular ID we can do that with the help of get questions and here we'll say public now this will basically return the list of questions right so I will say response entity and here we have to pass the list of questions then the thing is we don't need to return the answer right because if you see the question it has answers as well we don't want to return answer what we want to return is the wrapper because in wrapper question wer we have ID question title and options we don't have answer so what we are going to return is question wrapper and we'll say get questions from ID now since we are saying that we are going to return questions for the IDS right so of course we need to request for the ID as well so from the quiz service I'm expecting that we will get some IDs and since it was not one ID it is multiple ID so we'll say list of integers and we'll say question idas so basically that's what I'm getting from the quiz service basically and we just need to return whatever question service returns to us so question service. get questions from ID and of course we don't have this method yet so we'll create that method so I will say questions ID so let me create this particular method here okay so method created let's go back to our service and you can see we have this particular method here now let's define it so the thing is for this particular ideas we have to communicate with our database to return the IDS to you right so how will you do that so it is bit compx so just be with me so we don't need to return questions right we need to return the question wrapper so of course we need to get the question wrapper first I will say question wrapper this is what we want to return so I will say wrappers is equal to new add list now this is what we want to return so here we can simply return new response entity and then we need to pass two things we need to pass the wrapper and the status I will say HTTP status do okay right now here we can't send empty right of course we need to return questions based on the question ID now how will you get that it's very simple get the IDS and get the questions out of it so the way you can do that is what if you can create a list of questions here and we we'll call them questions and we need we need to fill this at least from the database So based on this ID we we will fill this and from this question we get we'll create a question wer okay now how will you connect your database to get this data we have have D layer so we can use it so what we'll do is we'll iterate between these IDs and get one question at a time so I can say for integer ID colon question IDs so for each question we need to get the question first and the way you can do that is by saying question Q is equal to or maybe we can directly add that in the list right so I can say question so whatever data you're getting from the database you will simply add that to the list so we can say questions. add but what you will add here now this is this is something you need to get from the database so you will say question Dow dot there's a method called find by ID which will ask you for the ID and we got the ID the only thing is this will return optional so you will say get so that you will get the value okay now what we are doing is we are simply passing these values and getting data from database and we are adding that in the questions right but then we don't want questions we want rapper which will not have answers right so what we can do is we can iterate between the list again we can say questions question is equal to questions and from this questions we'll copy data into question rapper and for that we'll say question wrapper is equal to new question wrapper and then one by one we'll convert the question into question wrapper not converting but copying data so we'll say wrapper. set ID that's the first thing we have to set and this will be coming from question now so question. get ID that's one you know I just hope there should be some library to do that that I will say Set uh question title so we'll say question give me title and then rapper do set option one so we'll say question do set option one and I can you know I can just simply copy copy it now and we'll set this as okay this that was one this should be two this should be three this should be four and this will be two this will be three and this will be four okay now once we have added the question so basically we have we are creating one question time we just need to add that to the list so we have wrappers do add we are adding this wrapper here that's it and now once we have added it we can simply return the value right so I think we have generated we have done for the get question IDs as well there is some error somewhere it is because of this okay I think we don't have the Constructor so I will go back to question wrapper and say I need a no argument Constructor now it looks good yeah okay things are looking good I don't see any problem here okay so that was for the get question IDs right now we need to do one more thing which is for the uh get score this is where we need to do calculation so what I will do is I can do it for the get score now now when you say get score basically a quiz service will send the responses which a quiz service got from the client so number of student students are submitting the submitting the quiz to the quiz service now quiz service will send this data to the question service to get this score and each set of responses will get one score so single score basically we have to only one score so here we'll say get score and I forgot to put the method name it is public it's a return response entity but then that will be just an integer now we just we want to return a score right a score will be a number and we can say get score now it will accept certain things right in fact it only needs uh list of responses so from the quiz service you will get a list of responses not response entity the list of responses which to import the package now what is response here so if you see response we only have two things here question ID and the response this is basically answer so let's say uh if you get a question and you get four options now that question will have an ID that we we need and also for that question what is the option selected by the user and once we know it we can get this score right for each question so I can say responses right now how do we calculate score we we will not do that we'll ask our service to do that so we say question service. get score by passing the responses right but we don't have this method right so let me create this method in this service right okay now how do you calculate score now basically we have done this code before right so if you go back here in the quiz service if you can see we do have this entire logic here right now what I will do is I will just copy this logic and of course we don't need everything let's copy it copy it so copy and paste okay first of all we don't need quiz now right this is we don't even have anything with this quiz here uh we don't need list of questions anyway we got responses what we can do is for every response in fact we don't need this int as well you can remove this we just need write okay see anyway the response has an ID right so what if whatever response I'm getting okay so for the response ID I need to get the question right so what I can do is I can say question question I just need to import the package I think already done okay question because see why do we need question here is because question has the answer right now we can pick one question at a time and the way you can do that is by using question da and you can say hey question da give me the question for a particular ID so find by ID and how will I get the id id is then the response right so I can say response do get ID and for that particular ID I need the question first and okay it will give me optional so I will say get for this particular let's say in responses we have two things right we have ID and we have answer now with each ID I can get the question first because the question has something called get right answer so here I can get this so what we are doing we are comparing the response with the right answer if it is same then we are saying Plus+ otherwise we don't need to increment right simple stuff okay and that's how basically you calculate this code okay now if you look here uh for the entire question controller we have done with this methods so this is basically what we have built our question service now this question service is going to be used by the quiz service now how we are going to do that that we'll see in the next video so basically basically in the last video we have talked about the question service right in this we were able to add questions delete question of course we have not tested it that's what we are going to do in this video but we have uh created all these endpoints we got all questions we got category add generate uh then we got get questions and this is basically if if someone is asking for a particular question based on some question IDs so let's say if you pass 5 8 12 and 13 so these are the question idas you passing so what it will return is a list of questions right and just questions not answers and that's why we are using a question wrapper here and then of course we have also added the get code which will calculate this code but the thing is we have not tested it right so this that's what we want to do here we want to test this particular uh application and also since we're talking about microservice we will be creating multiple instance of it right see the thing is in the world of microservices if you want to scale a particular service you can do that so example in your application you have five microservices and if you want to scale like let's say one or two you can scale them individually right and that's what we're going to do here maybe I want to run two instances of this uh question service right or maybe three instance so how do we do that how do we run multiple instance that's we check okay so first of all let's run this code and let's see what happens so it's already running so what I will do is I will just restart so that if in case if it is not running on your machine you can restart it and one thing to observe if you see here in the command line it says it is 8080 so the port number where you are running this particular instance is 8080 okay and now I will go back to the post man so I was doing some testing before this uh before recording so I got this particular links here so I will show you one by one so let's say I want to get all the questions of course you can get the questions from the browser as well so if I go back to Chrome so it is ad8 and then uh the URL is questions SL all questions why this URL if you go back to your IDE if you can see we are saying question and then slash all questions it will return all the questions I just want to test that enter and you can see we got all the questions here okay and now I will go back to post man for testing other things of course I can do all questions from here as well so I can send a get request so this is a get request and if you have if you don't have this of course you will not have so you can just enter this you can send a get request and mention this URL which is HTTP colon localhost colon 8/ question do all questions and then when you click on send you can see you will get all the questions here that's what we wanted right we got all the questions same that the same thing which we got in the browser we got here of course we are also sending the status code which is 200 okay and that's what you can see here okay that's the first request right but if you see your uh code we have done some more URLs let's not talk about category at this point or maybe we can do that right so we can say I want uh category Java right so this a URL we want to enter and when you say send you can see we get all the Java questions now right so these are the get request how how about the post request I don't want to add questions I let's say I want to generate a particular quiz uh so we can do that but again for that we need to me mention these two things we'll do that later at least we can test get questions right this is important now if you're sending a list of questions or question IDs I want to get the questions of it so what we can do is we can send the get questions and important thing is it's a post uh thing right it's a post method so if you see we are doing po post mapping so we have to send a post method and then here we have to say questions SLG get questions but then you have to also pass a body right the the list of IDs so if you see in the code we are accepting a list of integers here so you have to pass them so basically we have to pass adjacent in the aray format so I will go to body and let's say I'm I'm passing this questions here 2 4 7 9 5 you can send any numbers you have to just make sure that you are sending this questions you know two so I'm sending two four is here and seven and nine so we got seven n and five as well so basically remember this thing static exception this is what we are trying to fetch and also at least once remember this this terms and two strings okay these are the questions we will be getting so what I will do is I will just go back there and send the request and you can see we got this so question number five was about static and we got it question number four is here four was about exception and we got it nine was about two string we got it uh five was about static we got it okay so you can see we got all the questions which we mentioned in these numbers of course if you change these numbers let's say I'm I'm fetching three as well so two three so whatever ID you mention it will simply give those questions to you so three talks about uh unsized bullion variables let's see if three is that question uh yes so according to our test it is fetching only those questions which are mentioned so yeah this things are working we can also check for get scod but if you talk about get scod and if you go back to the IDE so what we are doing in get scod is we are sending this URL which is get score and this is a post mapping thing to remember and then we are specifying the uh list of responses now what a response basically response has two things one is ID and second is the answer this is not the actual answer this is the answer given by a participant right or whoever is doing the whoever is playing the quiz so this will the ID and any option which which they are selecting we just need to confirm right but how will you send this data of course uh from the question controller we have to when we are requesting for gets code we have to pass this responses so basically we have to send the Json of all the responses list of Json right so I will go to get score here and this is the URL right so get score question SL get score remember we are working with the question service now and that's why every everywhere you will see question slgs code and here in the post so this has to be post because we have to submit data right we have to send the questions or sorry responses now how will you send data of course you have to go to body and this is a response so you can type it manually by watching it it's very simple you just create a array because we are sending a list and in each element you have a Json response right so this is the ID of the question and this is the answer which a user is sending so this is the ID this is answer this is ID this is answer and then on the back end it will get calculated so randomly I'm assigning some values in fact we have done the same thing when we created the monic application so I'm using the same data and when you says send you will get a response in terms of score here if you can see we got three okay so it says visualize what is visualize option never tried it anyway so we can see we got pretty it's it says it is three and that's what we wanted right of course if you out of these five questions I guess there are five responses three are correct and you can also verify that let's say if I make one more mistake let's say it is I'm not sure if it is was four or five I don't know the answer even uh let's say for 19 what is the response for 19 we can check for 19 the response is the correct answer is three okay and uh initially it was four so it was a wrong answer let's make it right so it should be scored should be four now send and you can see we got four so this is working right and that's how you basically test it so all the URL I mean most of the URL are working we have not tested for Generate we'll likely do that when we are doing uh the quiz service not a good idea but uh I know it will work if not let's do debuging later okay so we were able to do that with one instance right and the instance is at 8080 which is 88 now it it may happen that you have multiple instance and every instance will have a different port number how will you do that how will you get a different port number so what you mention here in the application. properties and you can see by default we have not specified any port number so default port number is 8080 if you want a different port number you can specify here but then I want 880 as well and then the new instance will be having some different port number the way you can do that you can run multiple instance you can just go here on top in inj if you're using Eclipse you will see something like a configuration run configuration you can select that and you will see an option of edit config so just click on edit config here and this is the by default config you have so every time you build an application intell or any IDE you will get a default config we want this but with some changes what I will do is I will just copy this in fact you can see there's a button here copy configuration click this it will create a new configuration for you of course it has a different name what we can do is we can also customize this I think yeah you can just say instance 8081 so we are running a different port number now but then just by changing a port number here will not work this is just a name if you really want to change the port number you need to go to the modify options and you will also see these options in intell as VM options so when you click on modify option you will see add VM options you will see the same thing in inj but not in this sorry in Eclipse but not in this format it will have a tab the VM options just click on that and mention your option here so the option is you have to say d and server. Port so this is basically how you set it and you say equal to 80 81 so now what you're doing is you're specifying my new server port number server port number for this configuration is 8081 and when you say apply okay now you will see two options here one is already running right and you can run the other one as well so yeah so this is by default 880 this is 8801 you can click on run now it will launch one more instance so in total you will get two instance of the same service you can see this particular service is running on 8080 this one is running on 881 right so what you can do is you can go back to your browser so you can say this is running on 880 and I can just copy this URL first try with something which is not there so let's say 882 is not there so that's what you will get but if you try with 881 enter you can see we got a data so this data is coming from different instance this is coming from different instance right and if you refresh both are into running state so we got two instance of course you can run M multiple instance and you can shut down one of the instance let's say I'm shutting down the 8080 now and 80 8081 is working so if I refresh this you can see 8080 is shut down now 8081 it is still running so that's how basically you can play with multiple instances now once we have ran this it is everything is working what if you have a quiz service now of course once we build quiz service it will run on one instance let's say some other port number uh let's say 809 90 that's a port number we will go for and when you're sending a a request from the quiz service to the question service and if you have multiple instance if you have only one instance there's no issue right what if you have multiple instance which will which one it will select so that's what we want to do right that's the experiment we are going to do with this microservices tutorial so that's it from this video in the next video let's try to build the quiz service now once we have completed the question service now let's build our second micros service which is your quiz service so we are expecting that when a client sends a request client will basically interact with the quiz service not with the question service and then the quiz service will interact with the question service so basically we have two microservices interacting so that they can share data okay that's the entire idea here so we have done with the question service of course we'll make some changes there to make it more effective but at this point for us question service ready it is working let's focus on the quiz service now what are we expecting in quiz service of course we need three things the first one is see we are not doing any admin or user login here here is just that imagine when you expand this application if you want to make it big uh you also need the admin service or the normal user but at this point let's say we have three things create of course the admin will created but we're not checking that so it's create quiz next is what if participants wants to create uh play the quiz right or they want to submit the answers now in this case we have to create a method using which you can get the questions for a particular quiz so let's say tomorrow I have a session and after this session I want to uh start the quiz so that people can play but then I have to create the quiz today right so I will use create quiz and then tomorrow when I want to play the quiz I will simply call a meod called get with a quiz number because of course we can have multiple quiz so I can specify the ID so we have to say get slash the numbers and then the third one is to get the score so of course as a participants they want to know their score so we just need this three right but to do that we have to create a new project this is a new micros service so we have to get a new project so what I will do here is I will go to our favorite website which is start. spring.io and this is where you can see we have uh again we have done this before so we'll do that quickly so we basically need a mavin project for Java and this is the version we are working with 3.11 or 3.1.1 and then we have to mention the group ID so till this point we are working with com. so let's stick to it and then uh the artifact ID I will say this is a quiz service okay so before this we have worked with the question service now we are working with quiz service okay looks cool uh I want a packaging as jar the Java version I have is 17 and then now it's time to add the dependencies now basically we'll need the same dependencies which we have used earlier so of course we need web that's one then we need data jpa because we are interacting with the database next we need is postgis because the database is postgis of course you can use uh the in memory database as well but at this point we are using Post in fact in the entire project we are using post so let's stick to it and apart from this to reduce your code we need Longo so basically this is the thing we need right apart from this we need two more dependencies which we have done in the uh question Service as well one is your open Fin and next is your UA client so basically this we need these two at this point again I will I will not explain what these two are once we are question service is ready or quiz service is ready then using this will make much more sense so I'm generating it but then I will comment it before we even start coding so looks good I will just download this so let's click on generate and you can see our quiz service is downloaded so we have to unzip it first that's a first step now once the unzipping is done uh let's open that in the intell J so I'll go back to my intj and you can see this is your mon application this is your question service and we have talked about this before for right uh whenever you get confused which window I am into so just focus on this particular qu thing which is quiz app or question service so I will open a project now so I will say open and we need to go to downloads quiz Service open now this is the new project which we got and it is indexing it will take some time okay I think the external Java files have been downloaded and indexing has been done so if I open this of course you will get this particular files which is by default uh in the pal file we you can see we got all these dependencies at this point I don't need this two so I will just commend them so comment save and every time you make some changes in your pom file you have to make sure that you are reloading your Maven dependencies and done no problem here so that's done with the pal XML let's go back and if you can see SRC main we have this file which is quiz service application nothing fancy here just a simple spring application and in the resource we have application. properties which is empty at this point we'll fill this up and then uh nothing nothing much so we want to basically build a quiz service right now we we don't want to build this from scratch we have done that in the mon application so I can simply go back to my monic application which is quiz app and what are the things we need from here so we basically need this controller but we don't okay so what I will do is I will just try to copy everything and then we'll make the changes anyway we need these files right uh we don't need all but we need some so I will just go back here to my quiz service and paste inside this so I will say paste and you can see it will download it will get everything not download it will get everything here and then we have to also make sure that we are updating your application. properties so I will go back here we do need all this data so I will just copy this and paste it here and done so these are the things we need right uh let me just close this one anything specific yeah U there's one change here so basically we don't need to work with the question database now so we have two different microservices right so question service will interact with the question DB and the quiz service will interact with the quiz DB so here I want to work with the quiz DB we have not created this till now we'll do that okay so let's do that first so open your PG admin okay so PG admin is asking for the password let me just do that and okay so let's expand the servers and here we got post if you can see we have question deut now this time what we need is the uh qu quiz DB so what I will do here is I click here and say Hey I want to create a new database and this time the name is quiz DB so let's use that and click on save so you can see we got our quiz DB here okay and whatever tables you want to create of course we need a quiz table so that will be generated automatically because that's the setting we have done so at this point you can see we don't have any table but if you go back to inj so if you can see we are saying update so it will create tables for us if we don't have it but based on what so we have to make some changes in the code first of all uh in the controller since we have copied everything we don't need question controller because that's a part of the question service so we can delete this next do we need Dow yes we need quiz Dow because we need to interact with the quiz database we don't need question Dow what about the service in service also the same thing we need don't need question service let's delete that in the model basically we need all the reason being see the quiz makes sense is because we are generating generating a quiz and these are the things we have there we'll make some changes here I think there are some changes um we need question reason is see in the question service we do have question right and that perfectly makes sense there but in the quiz as well when you are interacting with a question service and if question service gives you a question where you will store it in fact I think this is optional so let's keep it now and actually we don't need question what we need is question wrapper why quiz need to know about the answers right so that's that is there in the question so we don't need this but let's keep it there for some time and then later on WE delete it we do need responses is because when a client is submitting the quiz the client will send the data in the response format so you have to accept that as well and nothing much these are the files we need and we'll make some changes okay so basically in the quiz controller what are things we have the same thing nothing changes here right we need to create the quiz for sure we need to get the questions for sure we need to uh calculate right these are the things are there so what are the changes here nothing much of course we have to do a lot of changes in this service uh which is your quiz service because this time things are going to change here because not everything is coming from database of the question service right of the question DB basically we have our own database and a question or the quiz service basically cannot interact with the question database we have to do that with the help of question service so how will we interact with this tool so let's try to understand understand that in step by step so we'll do that in multiple sections so here uh let's try to do that in the next video so let's fix the application because we have copied the code from the existing service or existing application uh the monic application I'm talking about so this is where we have copied the code right so what I will do is I go back to my quiz service and for me also I'm finding it difficult to understand which Pro I'm working on so I have to look here for the quiz service so that will be complex anyway so the first thing we have to do is is if you look at the controller let's solve this first the problem we have here is the project name right so in the earlier one the project name was quiz app so that's the issue so let's delete this and let's quickly import the things we need so this is coming from the quiz service and here also question wrapper we need that uh nothing much here let's go back to the quiz service and Here Also let's get rid of all this unwanted Imports let's say quiz Dow question Dow okay we don't need question Dow right so this will not be used let's remove it in fact I will not remove it I will just commented because there are certain things which is dependent on this and you can see the entire stuff here is dependent on that so what I will do is I will just comment this entire section because we need to change this now the reason being uh in this particular create quiz when we were doing it it was using a question Dow we don't do that now I mean we can't do that now because we don't have a question data B with the quiz service okay so we'll saw that and then let's import the question wrapper let's import the quiz let's import the question in fact this is also going to change we are not going to uh see even this is B basically connecting with the question Dow and this will create issue later so we'll solve we'll we'll resolve that later let's say responses this is need to be imported here quiz D is interacting okay so that's one and next we need to also change our Dow so if I go to quiz Dow any import issues here yes there is a issue let me fix that this is not needed now cool let's go back to service everything looks clean in service let's go to controller uh here we forgot to import the responses let's do that okay so we have got rid of all the errors but still it will not work first of all even before to make it work we'll make some minor changes see here when you talk about quiz uh create quiz we are passing three values right so don't you think instead of accepting three values we should accept one object which will be easier to handle something like this let's say request uh body and let's create an object of quiz maybe dto for the data transfer object and we'll say this is quiz D we don't have this class yet but if you have this class what we can do is let's create this class here so let's create a class with dto this just to have those three values together so what are things we need so we need a category name then we need a questions a number of questions and also we need Title so we need these three and I don't want to create uh geta sets for this I will simply say data this will do our job okay this looks cool nothing fancy and what where where do I want my quiz dto I want this in model because this is just a model right so let's keep it there and now we are accepting one value but while sending it I will send it individually so I will say quiz dto do get category name uh quiz dto dot get number of questions and here we have to send title so I will say Quest D do get title okay so that's the thing we need there nothing fancy okay so we have resolved about the quiz uh the create quiz but again this create quiz is calling create quiz of service and we need to make some changes here uh before that I will also make sure let me just check what are things we need here so when you say okay let's let's try to solve this create quiz problem only let's focus only on one thing so here when you say create quiz with this uh things are looking good no problem with that and since we don't need question let me also remove that so we don't need these questions anymore so we can delete that it will get a table for us we don't need that table okay so let's get back to our service and I think this is where we were using question right so at this point we'll comment the entire section here in fact let's comment the entire method but if I comment the entire method it will create issue so we need to return this right so I will I will comment entire stuff except this line because we just need to get rid of Errors right so this should work and here also I will comment the entire part except this right so this will work see what we want to focus now is only on one thing right which is create quiz so when a client is saying so the client will be admin When an Admin says Create a quiz it will send a request to the or it will call the quiz service and this is where we have to quote right so let's try to understand how do we create a quiz let's understand what are the changes here the this will be tricky now the changes is when you say create quiz the problem is when you say create quiz we are initially we were asking the question Dow which is interacting with the question database to basically create a question but this time we have we have mentioned right we have a separate service so this need to interact with the question service now that will be tricky so our quiz service need to interact with the question service to get the question and also this time we are not getting actual questions what we are going to get is the list of integers now why list of integers because we don't want to store uh the question data we just want the ID so when you say Create a quiz let's create let's take five questions from the from the Java concept cept and we don't need the questions we just need the numbers so ID 5 12 some something like that will be the questions right so here I will say the list of integers I want that but the problem is this line here will not work we need to change it now change to what basically we need to call the method of a question service so if you go back to question service and if you look at the controller this is what we need to call we need to call this method which is get questions for quiz how will you call this how will you call in fact you can't call a method what you call is a generate method this is a request right so from the quiz service we have to send a request so let's go to quiz service so here this line will not work so let me just write in comment uh we basically need to call so call the generate URL So when you say generate URL what is a URL so if you go back to your question Service uh if you remember we were we were running running this multiple instance right so let me just run this service once just to show you how this looks like so if I go back to the question service application just run this and by default it will run only on one port now which is 8080 and we have also talked about this if you want to run multiple instances we can change the port number you can see here we have 8081 here but at this point we have only one right and if you want to say generate the URL is actually it's on the same machine so I have to say HTTP colon SL Local Host colon 8/ generate okay so how do you send a request from one micros service to other micros service so basically from the quiz service we have to send a request to the question micros service and for this we have to use something called a rest template so there's a concept called rest template how rest template works is you can write some code here so basically rest template is an object inside this so if I can show you that it's a class basically so you can see rest temp template is there a part it's a part of spring framework so using rest template it it has certain methods which we can use uh to send the request right so it will send the request to the other Ser other server so you can see that it has so many methods to do that so we have to send a request to the question service now the URL which I mentioned before is HTTP colon localhost colon 8080 and then we have to mention the URL which is uh question slash generate right this is a URL where to specify and you will say okay this is good so using rest template I can hit this URL and everything is good not exactly the problem is here we are mentioning Local Host that means we are saying that it's it's in the same machine but what if you have in some other machine okay you will say okay we can use an IP address but then if you understand microservices properly we are not sure where we are running the instance right so I don't want to depend on the IP address next I don't want to hardcode the port the port number here how do I know on which Port it is running right so we don't know these values at this point we are to development stage we can do that but when you are into production you don't know this values so how do you solve this problem and that's where uh we have to use we have to introduce some new Services now this is where this is the first time we are basically interacting between two different services and if they want to interact we need two things the first one we need is a f Cent now basically what is Fang fing is same almost same as your HTTP web request the difference is it provides you something a declarative way of requesting the other service okay so you don't have to hardcode all the values you don't have to use something rest template where you specify everything it will help you to declare what you want and what are the apis you want to expose I mean what are the API you want to hit so we'll see that the next we need is a service Discovery okay now this will be interesting is because if you try to understand the quiz service is trying to search for the question service so that means the question service need to be discoverable so it has to we have to discover it how will you discover and that's something we have to solve right and let's understand how do you discover so to do that we have to use some server using which you can discover a particular API or particular micros service and one of the famous there are multiple options one of the famous one is Ura so it is from Netflix which is Netflix Ura so we have we need two things we need a Ura server and all the microservices they have to register themselves to the Ura server and then one microservice can search another microservice from the Ura server using Ura client so every microservice which want to search will also be having UDA client so they will search basically and so by doing this we are solving a problem of IP address and uh port number we don't need to mention that and with the help of fing you can actually request directly to the service with the service name sometime we also call them as a naming server right that also makes sense because every service will have a name now we don't have to depend upon the IP address and the port number so how do you work with UDA client how do you work with UDA server now that's what so even before we solve this we need to solve that okay so again I will just comment this so that will work and we'll remove this so even before we continue with this we have to get our Ura server y client and the open Fing so now let's understand how do you run a urea server so basically we want to have something where different microservices will register themselves and then of course they can also search about each other so let's say we have 10 microservices and then if one microservice wants to connect with another micros service how will they search so what we're going to do is we will create a server a service Discovery server and then every client here will register itself so they become a client and then we have a server so which server we are going to use there are multiple options but then one of the most most famous and common one is Netflix Ura server so what we are going to do is we will create a Ura server now this is not that complex if you're thinking hey we have to write a lot of lines of code just follow me in few minutes I will tell you how to create a UDA server so the first thing is you have to create a project so you have to go to start. spring.io and here you have to select mavin project and Java of course you can go for GR as well that's your choice so we can see we had Java and then the this is a version and the group ID which we have following here is com. telescope and the artifact ID and let's say I'm I'm saying this is my service registry okay uh so this is my server basically and then I want this to be jar and 17 version now what are dependencies we have to add only two dependencies basically the first one is we want to run this project so we'll say this is web and the second one is we need to create a urea server so if you can see the we have an option of urea server here just select that so this is basically a spring Cloud Discovery and that's it we just need these two things and click on generate and you can see we got our project so let me just open this and unzip it so unzipping done let me open this project using intelligy idea so I'm opening this these are the projects we working with we got two microservices question service and uh the quiz service right now the idea was when a quiz service want to create a quiz it will use the question service there are two different Services right and in order to search for the question service we will need a Ura server so basically question service will register itself to Ura server and then a quiz service will be able to search it right so let me open that project now the Ura Ser project so I will say open downloads service registry open and now we are opening our UA server project so it will take some time for the dependencies and okay still doing it so let's wait for that so by the time it is downloading the dependencies let me open the project and you can see the pal file we have mentioned two things things here uh we need a web project and also we need a urea server so both things are there let me close this and if you go to SRC you can see we have a service registry application and then if you go to Resource we have application properties so basically now in order to make it work we need to change two things the application properties and the main application right now if you want to enable the Ura server once you got dependency that's your first steps so you have to add the dependency the second step is you have to put an anation here called enable Ura server that's it so simple right so just by doing this we are enabling the urea server but we have to write some configuration one of the thing is what is the name of this urea server in fact going forward we'll also name our services example if you go back to your uh let's say question service and in this if you open your application. properties you have to also mention the name of this particular service so when you have multiple microservices it's important to have a good name for it right so that you can search it so we don't have a name yet we'll do that later but at this point uh what I will do is I will just go back to my service registry and let's make some changes here the first thing you have to mention the name you have to mention the port number it is going to run so let's do that first so it is spring. application dot name so this is a property and the value I'm going to set here is service registry okay that's the first thing the next thing we have to mention is a port number where this particular server will run so it say server. Port even if you don't mention by default Ura server runs on This Server which is 8761 now part from this we have to set three more properties the reason being when you talk about a client a Ura client urea client going to register itself to the Ura server right so let's say we have three things here we got our Ura server we got two clients here which is the uh quiz service and the question service of course these two Services the quiz and question they are going to register themselves to the Ura server but what about the Ura server do we want to register itself of course not right Ura server should not be registering itself and also we also don't want to fetch the registry so we'll make that false and we also set the host name for it so let's say to do that we have to say urea do instance. host name and the host name is Local Host the next thing we have to mention is about the fetch registry and we'll make this false and also I don't want it to register itself so I will say register with urea false okay so you have to mention these three things as as well so we don't want to register itself and then uh we don't want to FES registry as well and then we are specifying the host name which is Local Host so that's it uh we are registered now we have also have a port number and this this will make your urea server work so let me try this what I will do is I will just run this project I hope this will work so yeah so it says it started but which port number so you can see it is starting on port number 876 and anyway default was that that even if you don't mention so how do I verify so I can just simply go to my browser and just mention Local Host colon 8761 and this is your Ura server running so you can see it says spring Ura but if you see here this is important for us instance currently registered with Ura at this point we don't have any micros service which has registered itself to Ura server they're not even running right so how do we make it connected how do we make it work so that's what we are going to see now so let's go back back to our intj and here if you see this is our this is running right that we don't have to change make any changes here but the changes we are going to make is in the question service so let's R run question service because the quiz service will not run because we have we have not done the changes yet so let's run the question service and see if this works now first of all even if you uh run this it will not register let me just show you that once so I'll will just run this code uh it will take some time to load and you can see it is running on 8080 port number but if I go back to Ura server you can see it is not registered even if I refresh it's not registered so how do we do that so the first thing you do is you go to your P file if you remember when we were creating this project we have added two more dependencies one is your urea server or urea client and second is your open Fame so we are not using open fan at this point we are using the UA client so let's uncommanded so refresh so you can see loading done now this particular project which is our question service we have added a urea client that's the first change you have to make let's restart and let's see if that works again sometime we learn from trial error right uh there's no problem you can see this is important this is the Discovery client right so we have done this sotion status is okay or it's in good status let's refresh this and see okay can you see that we got it we got one application registered now which which is this application we don't have any idea and actually we know because we have run the question service but what's the name of it we want a good name for it right so what I will do is I will go back to my application. properties and here I will say spring. application. name I will say this is my question service and save this restart and go back here let's refresh and you can see we got it so we got question service registered and the beauty is if you run this with a multiple instance we have only one instance now what I will do is I will just go to the configuration remember we have also ran this with a different port number let's run that one so now we are basically trying to run two instance of question service and if you go back here and if you refresh can you see that we got two instance and we also specify here so this is on port number 8081 the second the first service is on default port number which is 880 so yeah that's how thing works you know it's it's awesome so we got two two connections Here and Now basically your service basically your quiz service should be able to find it but again the problem is even if your quiz service let's go back to our quiz service and let's see what's wrong there I know we are working with multiple projects so at this point we are currently shuffling between three projects the quiz project the question service I mean the quiz service the question service and the service registry so if you go to your quiz service we want to say that this particular create quiz is going to search about question service right and then we mentioned if if you can register yourself to Ura server you can search it but how you going to do that how do I request and that's where there's a concept of f so now we basically know how to run the urea server and if you can see here uh we have our urea server running and then we have ran only one particular project which is your question service but then we have we are running two instance here one on port number 8081 and second is your default port number right and then we have also made sure that the service name is this which is question service right now what I will do is I will go back to my quiz service here so if you can see we are into question service let's go back to our quiz service now this is where we were running the project right so if you go back here in fact we are not running the project we are making the changes and the problem here when we say create quiz I want to uh get the questions from the question service right so we want the communication between these two so the first step is done now we got a server so basically the services can search about each other uh in fact the quiz service can search question service wice ver is not possible is because I mean we are not doing it yet is because the quiz service we have not enabled the Ura client okay so it is not wasting itself uh but we'll do that in some time but if you go back to your quiz service this is what we were doing before and now we want to use the question service now to achieve that we need to use some rest template we have talked about this before right because how will you you send request to the question service so we can use rest template but then for that you have to mention uh the IP address port number and all the other stuff what if you can use some Shortcut way to search the service now one of the shortcut is to use Fang so there's a concept of open Fang or the Fang which is a client which you can use here now what fing says is you know instead of you handling everything all the IP address and port number or all the configuration you tell me which service you want to connect to I will connect it for you okay that sounds good right so the way you can do that is first of all you need to create a fing interface now I will do that in the general package of course you can make it some in other packages let me just create a package here and I will name this package as fing okay and inside this I want to create a file now this is a file which helps you to connect your quiz service to the question service now if you try to understand what are the things we want to use from the question service only three things three particular methods uh so basically we want to generate the questions second we need to get the questions for a particular ID and the third one is get score we just need to call those three methods right all the other methods which are there in the question service will be used only for that service but then between the quiz and the question service I want these three methods so what I will do is I will create a simple interface and the point to remember is when to create the interface so the interface name you can have uh anything let's say this is the quiz interface and now how do you make sure that you have Fang here so you have to use an annotation which is the Fang client but the problem is you can see it is not importing is because uh we don't have the dependency yet so what you can do is you can just go to your XML file and this is where you need to uncomment this part so this part need to be uncomment and also we'll also what we'll do is we'll set the Ura client ready for this anyway we are changing making the changes right so let's unable urea client and the fing both and this is done so you can see now F package is there so now if I go back here and if I say control space you can you can you can see we got the package let's import that that's the first step in fact the Second Step the first step is to add the dependency the second step is to add this annotation here now in this annotation you have to mention which service you are connecting to so basically this Fame client here will uh we have to mention which client what's the name of your service now the name of the service is mentioned in the Ura server so if you can see in the Ura server the name is question service right this is what we want to use here so in double codes I can mention question service in fact everything should be in caps so that's question service and once we done once we have done that the next thing you have to mention is those three methods of course right when you say a quiz particular quiz service need to access the question service we need to mention which Services we are going to access or which methods we are going to call now how will you get those three methods it's very simple what you can do is you can just go back to your question service and this are the three methods so again all the other methods we don't want we just need to focus on these three methods right so what I can do is I can get these things copied from here and go back to our quiz service and paste it here now of course this is a interface right and we are defining methods we don't want it the actual definition will be there in the question service here we just need to declare them not defining just declare same goes here we are not defining the methods we are just saying that these are the methods which we have which we can use okay and then this part as well let me remove this part and put a uh semic colum and then we have to import the packages as well for the question wrapper and for responses so that's it these are the only change you need now right so now by doing this we are basically saying hey quiz service if you want to connect to the question service I will help you so fing basically searches for this thing it will help you to connect to the question service but also if you want to use this particular feature in your questions question service we need to create this object I mean we need to use this reference so where do I use this we let's go back to quiz service and here we don't want the question out right but we need need the quiz interface so quiz interface and we'll say let's say quiz interface and we have to autowire this okay now whatever call you want to make so initially we were thinking we will be using rest template to make the calls to the question service we're not going to use rest template we are going to use this quiz interface to make the calls now how do you make the call so let me remove all these things here so the first thing we need to do is we need to get the questions right and we don't want actual questions we just need IDs if you remember so what we can do is we can create a list of integers and we'll name this as questions and then we can use the quiz interface to call a method but which method so if you go back to your question service and who is responsible to give you the questions so you can see this method which is get questions for quiz is returning you a list of integers right now this is what we want so let's go back to our quiz service and we can say quiz interface dot get questions for quiz and you have to pass two things the category and the number of questions right that's what we we are getting from here and that's it what you will get is list of integers okay but the problem is it will give you response entity right we want only the list of integers so we can say get body so basically uh in the question service we have mentioned that we are going to return uh the response entity so what we'll do is we'll just say get body so that you will get list of questions and then based on these questions let's Create a quiz and for that I will create a quiz object I will say quiz quiz let me import the package I mean of course the same thing we have done before as well and if you remember I deleted that code so we have to write it once again uh so let's set the title for this because that is something we got from the user and the title goes as title now it's time to set the questions so I will say quiz do set questions now set questions will accept the list of integers right so I can simply say questions we just need to make one change which we forgot to do before is in the quest now when we are saving the questions we not saving the actual questions this is something we need to change we are basically saving the integers right so the change we have to make is not questions uh it's not questions but question IDs and then we are not submitting the questions we are submitting only integers now when you change this initially it was questions right but this time we are going for uh the IDS right now in this case when you only have numbers we cannot go for many to many what you do is in database we use something called element collection so when you have have a numbers or a specific type which is only one so you can use element collection but yes if you have a entity type a different table then many to many Mak sense now going back to quiz service here I think it should work now so set questions there's still some problem I think in the quiz service no so set questions are not working oh okay the name is changed right so it's not set questions but set question IDs that's what IDs and then this will take questions now once that is done it's time to save the quiz in the database so what we can do is we can use our quiz Dow and we can say save and what we want to save is the quiz and done we are saving the quiz so what we have done here is we wanted to create a quiz but then if you want to create a quiz you need to request to the question service right now question service will give you the questions and that's what we have done here and the way you can interact between these two services and the way you can your quiz service will ask question or ask for something to the question service is with the help of fing client which we have done here we are specified The annotation there and we can access these three methods which we are trying to do we are using only one method here which is get questions and yeah once you got the question we created the quiz and we were able to save the quiz we have not tested it yet but I'm thinking it will work so let's only test this part which is Let's ignore all the other one let's only think about this create quiz and to achieve that what I can do is I will just run this code once okay there's one more thing if you want to enable the quiz uh the fame client we need to mention here enable F clients I think that's the only change we need uh let's run this I hope this will work okay there's a problem it says it is already in use okay so we forgot to do one thing in the application properties in fact we forgot to do two things so when we are using the Ura server uh we need to use a name so I will say this is my quiz Service uh the next thing we have to mention is a port number right so by default it is using ad8 which is used by something else so let's use 8090 that's the port number we are going to run this and things looks cool let's run this and here we go you can see that this is running on 8090 so that's good let's go back to our browser where we have our Ura server running you can see it so says only one server or one service running but after refreshing you can see we have quiz Service as well so now both the the uh application or both the microservices have registered themselves to the urar server with different names one is a question service which has two instance running and one is a quiz service but now we are saying that we want to run the generate quiz how do we do that so basically if you want to do that we need to pass the quiz dto data to make it work okay so now it's time to run the project right so in fact if the project is running we want to test it so let's create the first quiz and let's see if the communication between the quiz service and the question service is really happening so what I will do is I will open my Postman because that's what we are doing here so let's go to postman and okay so what is a request so the first thing you have to change is the port number so we are running on port number 8090 the next thing you have to mention is the quiz and the URL is actually quiz SLC create so if you go back here uh that's what we are doing right okay so we are into quiz uh controller yeah so it is quiz SLC create so let's go back here and so that's our URL so we have to also send data right so what kind of data we want to send it's very simple uh if you can see we need to we need to accept three things that's your quiz DD which has three things the category name the number of questions and title okay so what I will do is I will create a Json here the first thing we need to mention is the category name so that's category name and the category name we have is let's let's say Java we want to get questions for Java the next thing we want to mention is the number of questions so I will say num questions and okay how many questions we want let's say five questions and the next thing we want is the title so we'll say title and the current batch of uh Java which is going on the batch name is Springers let's say Springers quiz one that's the title for the quiz and okay so with this data I will click on send and let's see what happens okay we got an error that's weird okay this is a problem of 500 so let's check our server and let's see what is going wrong here something wrong with the fame client okay so it says not able to find something what is not able to find okay so path SL generate is not found okay that's weird but if you see the exact URL let's see so we are hitting the question service that's good and then directly we are jumping on generate oh okay so if you remember if you go back to your question service the generate is basically after question so we forgot to put the question there okay so it should be question SL generate so if you go back to your quiz service and quiz interface where is that yeah so you have to say question SL generate in fact for all not just for this it should be question slash getet questions questions SL getet score say save it and restart the server I hope this one this time it will work okay there's no eror let's try once again click on send oh it says success that's good but is it really working let's try that so there's no problem here you can see it I think it it tried to fire to the question service now there's only one way to check if the data is there in the database so let's go back to our PG admin which is not opening I don't know why okay PG admin okay so if you can see we have a quiz table so basically the quiz DB is a database and the quiz is a table and if you try to view the data here yeah so you can see that we got the quiz and the title is Springers quiz one and if you see the questions and we got five questions and these questions are actually coming from the question service because quiz has no idea about the question database right so the quiz service is actually interacting with the question service oh finally this is a video where we are basically interacting with the two services cool uh let me just check the urea server is it saying something okay things are looking good in urea okay in fact it is not showing the health uh we should have added the actor as well you can see actor is not added yeah but then okay let's try to do that later so yeah that's how it works that's how you are basically connecting with the help of Ura server so I hope you have enjoyed it till now where we are basically able to run the project and things are working out the two services are running out also I wanted to check we have two question service right so when a quiz service is calling the question service which one is getting called let's try to understand that in the next video so before we get into the load balancing we have to complete certain things in the quiz service now if you remember of course we have create we have created these three methods right uh so in the controller the quiz controller we have create we have get so basically for a particular quiz we can get the questions and then we have a submit quiz right so when you say submit it will give you this score now if you look at the implementation so if you see for the create we have done the coding right so the create quiz is done and it is working we have tested that before right so I will just remove this comment from here because the create quiz is ready but if you go down we have two more methods to work with we have get quiz questions and then uh we have one more which is the calculate results of course uh we can change this method name if you have if you have some other method that works uh we can go with a consistent word which is get score so at this point just imagine get score and calculate result are same because both are getting the score right now these two things are not implemented okay so we want to implement this so let's start with the get quiz questions now what we are trying to achieve here is for a particular quiz so let's say we have an ID right uh so what happens is every time I I can create multiple quiz for the same topic so let's say I'm running a batch in this every week I want to uh Create a quiz so every quiz will a different name and different IDs now for that particular ID so let's say if I create a quiz fifth quiz so the ID will be five and in that particular quiz I have let's say 10 questions so for that quiz I want all the questions and then you can see when you say questions you don't need answers for it so you need a question wrapper so basically you need a list of question wrapper so how will you get that first of all we are not storing the questions in the quiz table so if you verify that once again uh let me open my PG admin and if you see uh we do have quiz table here so if I say uh view all data so if you can see here uh we have number of quiz created so I was doing some testing last time we have we have only created one quiz I was doing some testing for different features and then I've created this two but ignore that let's say I want to work only with the quiz number two okay the ID two and then we have one more table here and if You observe this we are not storing questions we what we are storing is question idas so for a quiz let's say we have quiz number two for this quiz which is which has five questions you can see we only have numbers but then if you want to conduct the quiz I need questions as well now question arise okay question arise who has this questions and the question service knows the actual questions the quiz only has an idea about the numbers not the actual question so now we have to again interact between the quiz and the question service so how will you do that it's very simple so first of all for a particular quiz ID I need the quiz so we'll take that so I will just uncomment this part and also I will make some changes here so instead of optional let's say I don't want to use optional it's a good it's a good practice but just for example here I'm working with quiz directly right I'm using this get method so before we get the questions from the question service we need to get the IDS of it right so let's say if I specify I want to go for two I want all these IDs which is mentioned here now how will you get the IDS so we have to get that from the quiz ID itself so if I go back to quiz it has all the question IDs right so in order to do that let me go back to quiz service I will use a method so in fact I can remove everything from here and let's keep it clean so basically I need to get the IDS right now that ID will be list of integers right that's the type of it so I say list of integers and I will say question ID is equal to and then we need to get that from quiz so we'll say quiz. getet question IDs now once you get the question IDs I can request my question service to give me the questions for this IDs and if you remember if you go back to your quiz interface which is also a proxy between the quiz service and the question service we do have a method which is get questions if you can see get questions for ID it is actually accepting the list of integers okay so we are good we can just request to that particular interface so I can say quiz interface and the method name is get questions for or get questions from and we just have to pass the question IDs okay now what it will return so let me just go back here so it this method returns a list in fact returns a response entity list question wrapper so I can simply get that and paste it here again this is not copy pasting this is code reusing why you type the same thing multiple times right but you don't do it so when you are practicing the code you type it by yourself so you can see we we got response entity list of question rapper and we'll say this is questions equal to in fact me need to put that here cut paste yeah so you can see we got the list of questions and this is the thing we need to return here so we have to return questions so that's what we wanted to return right okay wait we just we don't need to do all these things we can simply return the response object so I can say questions this should be good okay so this is done so this particular method is done which is the get questions and of course we need to test this as well so how will you test this it's very simple just restart the server and we are not making any changes in the question service right so I can simply restart the server here and let's see if this works so let me open my Postman and now what request I have to send so basically we have to send a get request now and then it is in the quiz and what method we have so if I go back here the request we are trying to send is this so we have to say get and we have to pass an ID right so I can say slash getet slash and the ID is let's say two of course we have that two and I can click on send and you can see we got a response which is the questions the actual questions so we got for 177 and we can verify that 86 so it starts with 177 let's verify from database so for the Quiz 2 177 1886 so is it right 1886 that's right so we are getting the right questions so this is working uh now we need to work on one more thing which is the get score so what if you want to submit a quest now we are calling a calc calculate result but again this is not ready so again we have to interact with the question service because question service is responsible to get this code uh we have done that before so let me just go back to question service and in question service if you see the controller so this is where we are saying get score we is calculating the score from the question from the question service and all the operation we are doing in the in the question service not in the quiz the only job of quiz service is to request right so quiz is being very lazy here but that's how we build a system right now what are things we need so what I will do is I will remove everything from here the only thing we have to do is we have to call the get score method this particular method which is get scod so who will do that so it is quiz interface. getcod and whatever responses you got here these are the responses you have to send nothing much I will just simply say responses and what you will get is the result now in which format so basically if you go to quiz interface it will return the response entity integer so you can just copy this and paste it here this is what you're going to return and say let's say this is this code okay and that's it in fact you can also say write us code that also works and we are returning in fact we can return the object itself we can return this code because that is of type response entity integer and we got it right so this is so simp simple nothing to do in the quiz service everything is happening in the question service we're just requesting it I hope this will work let me try it out so restart the application because we have made some changes and now after making the changes let's go back to our Postman and so we are getting questions there's no problem with that this time we have to submit the quiz okay responses right and instead of typing it you know somewhere we have done the response right somewhere it should be there get score yeah this is here so get score in get score basically we have to pass the response right so you can see these are the response we have the changes we have to make is we're sending a post request the port number is not 8080 it is 8090 this time and uh the quiz and the get score is it the get score method let's verify that quiz controller oh no it's a submit I know we have used different uh terms here but let's do that so we'll say submit and then uh we can also pass the ID for the quiz so let's say we are working with Quiz 2 and these are the response we have I'm not even sure if you have the same questions but doesn't matter let's say get let's say send and let's see what happens we'll change it if it doesn't work and you can see we got three okay so this is working right and if you make any changes uh I think we have done this last time as well but let's say if I make one more change uh if I remove the space from here which is incorrect and if I click on send you can see the score now is two so this is working right so get score is working and uh the get questions is also working working so by doing this basically we have made two microservices which is running there's no problem in that one microservice is able to call Second microservices and whatever features we wanted to implement it's working right but now remember in the previous section we have talked about we have two question service running so if I go back to the question service here we have two instance which is running which is getting called we are not sure right is it the 80 responding to the quest service or or 881 responding to the qu service how will you get to know that and there's a concept of load balancing okay so we'll we'll discuss that thing in the next video now let's talk about load balancer the thing is when you have multiple instance of a same service right let's say uh even if you talk about Mo application where we have one application we can have multiple instance of it that's how you can scale it right uh we call them as horizontal scaling where you can create multiple instance so whenever a client sends a request now based on which instance is free or available the request goes to that particular instance so let's say we have three instance and then one and three are busy the request goes to Second instance right that is called load balancing so that one instance will not be busy with all the request so you can balance the load between instances if we can achieve the same thing in the microservices of course every service imagines of one application so let's say we don't have a lot of load on the quiz service so let's say we are not creating multiple insts of it we have only one but let's say the question service is is very busy so what we can do is we can create multiple instance of it so let's say we have two instance now when a request goes from the client side to the quiz service when the quiz service wants to access a question service we have two options there right now based on the load on each instance we can decide where to send the data where to send the request right that is called load balancing now in the earlier days a way back we used to do everything manually but because of some tools and some libraries it has become easy now and with recent versions of spring we get that by default in the system so the moment you get your urea client and fing and if you expand your uh so let's expand this so let me expand this external libraries we do get those jar files by default so if I go down and if I expand this you will find the load balances somewhere so you can see we have by default load balances here so we don't have to add them manually but the question is how how we are going to use it so you don't have to do any configuration the moment you use f class C it will automatically search for question service and whichever service has or whichever instance has a less request it will send the request there and even if you have two instances and if if you don't there's no load basically it will switch between the two instances and you don't have to worry about it right that that sounds good right uh but then how we are going to implement this or how do how we are going to check this now even if you have not done any configuration and whatever we have done till now the quiz service was actually sending request to the first instance and the second instance of question service one by one alternatively and the way you can see that is let's go back to the question service and let's only Target on the get questions okay so we in the controller which is question controller and every time you say get question let's say or let's see which particular instance it is using now how do you differentiate between these two instance with the port number right so one instance is running on 8080 the second instance is running on 8081 so what we can do is uh we can just print the port which is getting used and that will solve our problem right now to in order to do that we need to get uh get hold on something called environment now in the spring framework we do have environment so we can just use that environment the the variable is environment as well and then let's Auto this so you don't have to instantiate this now once you get this environment I can go back to the method which is get questions and here let me print it so I want to print the environment now this has something called a proper and which property I want to Target I just want to get the port number right so the we can simply do that with the help of local. server. Port so it will tell us which Port it is running on and then it will print on the console now after making the changes I have to make sure that I restart both the instances of question service we don't have to restart the quiz one so let me restart so we are basically restarting both the services which will take some time and I think it's done both the service haven't restarted one is running on a 080 the second one is running on 8081 so let me go back to my post pan and we request to the quiz okay now what we are getting is get questions so for a we're sending a get request for question id2 let's just send and you can see we got the response right but let's verify which instance is getting used so if I go to the first instance you can see this this has been used and this has not used yet which is 8081 let me send the request once again and you can see it is again sending the the first one let's try once again and now again it is using the same one refresh okay let me try once again multiple times okay now you can see after multiple request the request is going to 0 at1 so it is going in random so basically whichever service is free so if I can send multiple request and when you get lot of request to one particular instance of course it will D deviate you can see response request is going to 081 and also 808 Z so alternative it will send not exactly alternative but uh it will check which is the free instance right so at least we are getting this by default which is your client side load balancing and at one point I thought it's not working but yeah it's working you can see the request is going here and we don't have to worry about it right because both the instance are same so it Respec to which instance it is calling to we getting the same output right so that's how basically you can load balance let me try once again it it looks cool so you can see request going to H one and also to 0 Z so that's how we can use this that's how we can use the load balancer so the way it works is with the help of Fame client it will help you with the load balancing and you don't have to do it anything manually and if you want to print it you just have to print environment. get property and the property name is local. server. port and environment variable is this which is coming from Spring framework so just make sure that the environment which you're using here is coming from the spring framework not from hybern because in the hyet as well we have the same environment type so that's it about load balancing and we're doing it with the help of f client and Ura client now till this point we have done so many things in this project basically we created two different microservices and we have also gone through some Concepts example if one micros service wants to call the second micros service how will they discover so for that we have installed also we have worked with the urea server urea client uh if you want to make sure that one instance is able to call the other one or the one service is want to call the other one what a shortcut way so we have used fing as well now there's one important concept of course microservice is a huge concept there a lot of things but then one thing which is very important is the API Gateway now why do we need API Gateway see the thing is if you talk about a user now user is looking at your microservices in fact all your microservices combined as one application right now user has no interest to know okay you have quiz micros service you have question micros service for user that's one application right so the way to interact with the application should be only one URL the problem which we are having now is we have two Services right we have a question service and we have a quiz service now both these Services have a different name different port number and when you want to access them so basically we need to we have to mention the port number as well right example if you want to work with the quiz service you have to work with 8090 if you want to work with the question service you have to say 8080 or 8081 uh if you want to individually access them without quiz service right now the thing is apart from this are few more problems let's say you want to also achieve authentication now you have multiple microservices and one if a user comes to access one micros service you will say Okay enter your username password and you're like okay we got the username password the user can access the resource but for that micros service what if a user says I want to go I want to access the next microservice now at this point again the next micros service will say okay tell me who you are prove that you are that person again a user will enter username password do you want to give this experience to the user where user is logging for every microservice to access of course not a user should access only once what if you want to maintain the log file what if you want to achieve some cross cutting concerns in at one place and that's where we can use API Gateway so this API Gateway becomes the entry point or the interface for the user how do you do that now in Spring Cloud basically you do get the API Gateway and we just have to use it and as we have done till now for every new thing we have to create a new project I know that's the only part of microservices which is tricky is because every new thing is a new project of course as developer developers perspective when it comes to the Ops maintaining it huh get a trouble but the thing is we are learning micro Services right it has a lot of features and we can adjust one or two so the first thing you will do is uh you will go to this website which is start. spring.io and here what you will do is you will create a new project MAV uh the so from we making the video now we got a new version between the video so earlier we were working with 3.1.1 now we got 3.1.2 it should be good let's try this we got com. Tesco as my group ID and the artifact ID is API Gateway okay and jar version 17 because in my machine I have Java 17 here now the dependencies we only need two dependencies here the first one is Gateway because of course we need API Gateway and also we need a Ura client because it has to register itself to the service disc to the Ura server and once you got the two dependencies of course you can add actuator you can add Dev tools which you can use in the other projects as well but just to keep it simple I'm going for two Gateway and ureas Discovery client with this let's download the project so you can see we got API Gateway in this machine I already had one zip file and that's why you can see in bracket it is one so let me unzip it so unzipping done uh let me open this in my intellig idea so so open so navigated to the folder and now after unzipping it I'm opening the project and this is where you got your API Gateway and that's it this is your API Gateway is it okay so we have to make few changes the first thing is if you look at the SRC we got two files here in Java we got one file in Java which is API Gateway application and then if you look at the resources it is application properties which is empty and that's it this this is your project API Gateway now why this API Gateway with because of the name no because of the library which you have added or the J file which you have added which is the starter jpa right uh this is your API the thing is if you want this to work you have to make some changes because by default if you try to run this project it will run on 8080 and we already have that Port busy right so just to give you a number of Port we are using so we have two question service instance running on 8080 8081 we got a quiz service running on 8090 we got our urea server running on 8 8761 and API Gateway we to run on some other server so we'll set two things here first is cloud. application. name so let's set this as API Gateway the next thing you have to mention is a server number let's say 8765 port number and uh we need one more thing in fact we'll need two more things we'll do that later so at this point we have done the configuration and now let's run this and let's see if this works I'll right click this and say run so there's running okay cool and the port number is 8765 I will go back to my urea server to check so I went to Ura and the number of application running is you got two question service running we got one quiz service and we got API Gateway as well which is running on port number 8765 so we got API Gateway so now this is your front now what this also means is if you go back to your Postman remember we were sending request to request and and the port number is this but then we don't want directly to access the quest service I want to use it through the API Gateway so this API Gateway port number is 9 8765 so every time you send a request for the question service or for the quiz service this is a way you have to go from here you will go so once we get into API Gateway Now API Gateway has no idea what this quiz is example if I run this code or run this particular URL is not working because the API Gateway has no idea what this quiz is now this quiz is a URL which who knows about this URL the quiz service if you use question there who knows about qu question it is question service API Gateway has no idea but what API Gateway knows is the name of the service how we have used the UDA client so what it does is it checks okay so we have API Gateway knows about two things the question service and the quiz service so that means if you want to use a quiz service from the API Gateway here before the quiz you need to mention in caps because that's what we have mentioned quiz service so now we are sending request from the quiz service so API gway will connect to the quiz service because of the Ura client and then it is able to communicate see ultimately we are trying to connect with the quiz instance or the quiz service if you run this code or if you run this URL still not working okay we have to do one more thing in the code of application. properties basically here of API Gateway we need to set one more thing which is the locator so we have to mention uh spring. cloud. gateway. discovery. locator by default is false and you have to enable this so what you're doing is you are you are asking the API Gateway hey I'm allowing you to search Okay locate the services in the earlier before this we were not allowing it so now we are allowing it to API Gateway you know enjoy your enjoy the search you know look look for the services with the help of you a client okay so it looks cool let's go back to our Postman I hope this time it will work send it worked so we are basically trying to access the quiz service but we're not talking directly to quiz service a client sending send the request to the API Gateway and API Gateway says okay uh I need to search about the quiz service it will discover the quiz service from the UDA server and then send the request that's what is working but then I have one little issue here which is this particular thing I don't want to mention quiz service here uh I mean in capitals of course you can also remove the entire part here we have to do a lot of configuration for that and we just want to make it simple at this point uh this is for beginners so we'll not be removing this there are some steps as well to remove this but at least what I can do is I can make it lower case instead of upper case so I can say service the quiz service and now when you send request it's not working because the capital Works small is not working at least this looks cool right right compared to the Capital One so how do you make it work uh you can use one more property here which is the lower case so this is spring. cloud. gateway. discovery. locator. lowercase service ID because we don't want to go for Capital it will search for the lower one I mean it will convert that into lower and now make after making the changes let's restart okay restarting complete and now let's try send it worked right so this this is basically how we use API Gateway with a lot of configuration that can be an advanced part of this particular series but yeah this is about the series on microservices I know we have done so many things the number of projects which you created is huge in fact I'm teaching you all those things and I'm working on different projects while I'm talking to you see as a trainer we have to be very attentive because when you're working somewhere and if you take a gap in between that works in videos it will not so sometime even I get confused which service I I have to go I'm I'm talk I'm telling you that we going you have to go to ction service but in my brain it takes some time to register which project I have to open right maybe there should be a yura server in my brain as well that will be weird anyway so we were working with this three four projects the quiz service the question service the service history API Gateway just to go give you a brief when a user wants to play the quiz first we need to create the quiz right but when you say create quiz where are the questions now question belongs to the question service right in the question service we can add questions we can remove questions we can search for a particular question we can uh generate questions for quiz we can get questions for the particular ID or list of IDs uh we can also calculate this score and everything we have done in the question service in the quiz service basically we were asking the user what you want to do you want to create a quiz okay I will do it for you but actually quiz service doing nothing you know it just says hey hey question generate a quiz for me smart right so the thing is we have two microservices and then we were also able to do load balancing because of uh UDA client and F but then we don't want to directly interact with the each individual services and that's why we have used API Gateway so as a user I will interact with the API Gateway API Gateway will interact with the other services and uh how do you search between different services and that's why we have UDA client and UDA server and we have seen that as well I know uh when you are doing this project there will be a lot of issues so let us know in the comment section and also you can join the Discord Channel you will find the link in description uh and you can ask your question there you know people will be happy to help you there and we will also be there to help you so that's it from this video and that's it from this series basically and I will also try to work on the advanced part of it and until then enjoy the other tutorials coming up