π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
welcome back aliens my name is the ready and let's continue the series on Spring framework now till this point we were able to work with spring Boot and also we have created our first spring boot web project and of course we don't want to discuss everything about web we want to do that once we start with the
00:15
project but there are certain things some Basics we have to understand in terms of Basics we have talked about how do you map a particular request so we have done for two request here one for the homepage if you can see here and one for the about page and in fact we have
00:30
done one more if you remember for the login as well we have done this so in total we got three requests right now in this request basically what we are doing is we are sending a request from the client to server and then on the server side basically we are mapping it and returning a data now when you return
00:46
login page or when you return uh this text like welcome to the Disco or we don't teach we educate basically what we are doing is we are sending the text back to the client right we are not sending a HTML page in fact we have talked about it when we use rest controller here it Returns the data but
01:02
if you remove the rest from here it will return the page name so basically your spring framework will say okay it says welcome to the Disco that means I have to search for a file called Welcome to the Disco right and then it search for the file but we don't have that file we just want to send data we don't want to
01:18
work with the UI part here we just want to work with data that's that's more important right and it's working but let's say you don't want to return a normal text see most of the time when you talk about any particular application you want to return data for a particular entities right example if
01:34
it is a e-commerce website uh you want to return the products right and product will not just have a name product will have other details as well the product ID product name uh product price product category so it will have all those things now same goes for let's say if you want to book a flight in Flight as
01:49
well you'll be having flight details you'll be having a timing you'll be having a flight number and all those details and think about any application we normally work with a entity and in the world of Java we represent that with the help of objects so that means if I want to return some details it can be
02:05
multiple entities not just one so if you talk about the eCommerce website if you search for a particular product let's say if you want to search for a laptop now it will give you a list of laptops so not one list of laptops now the idea is how do you send that data from server to client in which format will you send
02:21
as a normal text of course not right because on the client set as well if it is a react application or a mobile application they cannot accept a normal text it will be difficult for for them to convert that into a UI or read read it from the text and that's why we have to use a Json format it can be XML or
02:36
Json nowadays we are using Json so we'll use that so basically what I want to do now is I want to return the data not the data normal data I want to return the object the object will have the data which I have to return back to the client now this is tricky not in terms of spring this is tricky in general so
02:51
what I will do is I will create a new controller and this controller is responsible to return uh let's say products so I will say product controller and we are going to use this later in the project so I will say product controller okay a simple product controller nothing fancy and then this is responsible to accept the request for
03:09
the products so when I say public uh return a string at this point I will say string but we'll change it later and I will say get products so maybe I want to return all the products in the database and of course in this video we're not going to work with database we are just going to return the dummy data which we
03:26
have so a list of products and I want to return something here at this point I don't don't have the data now since we are not working with database let me create some fake data and the way I will do that is first of all I need a class which will represent a product so when I say a product what it should have product ID product name uh let's say
03:42
price let's say we have this three so I'll just right click here and say new I want to create a class which will represent a product and this will have certain things so it will have private we can also use lombok here then we don't have to type lot of things so what
03:57
we can do is we can get the lombok so that in fact I'll first of all type this so I'll say private in Broad ID then private string broad name and private in price let's say we have this
04:12
three later on we can have we can increase this so at this point we got three and now I want to use longbo why do we need longbo is is because when you use a private variable of course I have to create data sets for this Constructor as well so instead of that I can simply use longbach Library which will help me
04:28
to create those things behind this seene uh which will make it bit easy to read and how do we get longbo it's very easy you go to pom file and you add the longbo dependency here so somewhere here okay but I don't remember the longbo dependency or the group ID and the artifact ID what you can do is you can
04:44
go to your browser search for mvn repository and search for lombok this is the project L Lok let's get this and go with the version which is look which looks stable so this one it got 3,000 use so I will just pick up this one copy this and paste it here okay now once you
04:59
do that that of course I have to remove this line as well uh I will save this and you can see there's option of M and reload I will do that so that in my dependency I will get lombok as well so if you expand external libraries and if you search you will find lombok somewhere it should be in org lombok is
05:15
L okay project Lok is it yeah so you can see we got Lok here right now once you have that you can go back to your we can minimize this we don't need external dependencies U the size of it and go back to your product not product controller product and here we can add an annotation called Data so this will
05:33
give you the GS as well and now once you have this you can go back to your product controller and this is where you can return the data now normally if you talk about the layers if you remember we have talked about different layers right we got controller and if you if a controller wants the data now it will
05:49
not ask data from database it will ask data from the service layer now this is where we have to introduce new layers because you you should not be writing any business logic inside the controller controller is just for for accepting the request and responding to the client not doing any business logic okay so we have
06:04
to get a service layer which will have a business logic so what I will do is I will create a service layer here because see when you talk about oh what I'm creating let me delete this file not needed yeah so here from get products what I want to do is I want to get all the products from the service I don't
06:19
want to write the logic here so let me create a new class and this class will be responsible for the logic so I will say product service and I want this to be in different package so what I will do is I will put all the controllers if you can see we have multiple controllers here controller should go in controller package uh the service classes should go in service package and the product go
06:37
should go in the model package so there's a concept of MVC model view controller so the job of a controller is to accept the request from the client responded so in view we basically return the page now here we we're not returning a page we rning data but still it's a part of MVC the model represents data so
06:53
if you talk about the product here this represents the data right so this is model package so I will say new I want to create packages uh one for the controller now this package will have all the controllers so we can just move that there refactor and I want to create
07:09
a service package as well so I will create a package which will have the service because in future we going to get multiple service classes so I will just move that here and then I want to create model as well in fact once we start with database we will also create a repo package not a class I want to create the package this is for model and
07:26
we'll move product to model okay so now we are creating those packages it will look good and also easy to find so all the controllers will be in controller all the models will be in model all the service will be in service okay now in this service class basically I want to create a method which will
07:44
return me the least of products so I will say list of products and I will say get products we can have different name but that's fine and we can return it now since product is not part of this package I will just import this
08:00
and we got it and we have to also import the list so we got it and now our job is to return the list of products now the question is where do we find this list we don't have a database so time being we can create some dummy data so I can create a list of data so what I will do
08:16
is I will say list of uh product and I will say products is equal to and let me create uh the product like this aray dot as list and I will say new product okay I want to have a Constructor as well
08:31
which will accept this so what I will do is in the product I will add one more all argument Constructor here so that it will create a Constructor for me so this is a feature of lombok I'll just go back here and say I want to create the list of product which will accept the first one product ID then I I will say iPhone
08:48
price is let's say 50,000 okay it's not accepting all arguments Constructor but anyway we'll try to run this and see what what goes wrong so and then I will also say new product and this time we'll add one2 and let's say we only have two now we just
09:05
want to see right if it's working or not I will say Canon camera because that's what I can see on this on on the desk and then I will say price is 70 okay so we got this two data it's not working because of Lok I think when you run this it will it it has to enable the Lok uh
09:22
thing just see if this is working okay there's no compile time issue so of course that's a longbach problem and it says 80 port is not working it may be because the port is busy with something else what I will do is I will change the port number so this is the problem with
09:37
uh with my machine every time I close my in idea is not able to close the port number it may not be issue with you uh so it says 88 is busy so what I will do is I will change the port number so we can do that so if the port number 88 is busy I will say server. Port is equal to 8081 so now this time it will run on 081
09:54
even 081 is busy that's weird that's weird I know where I'm using all this boards so I've not restarted this machine from a long time so maybe that's the issue and I've not closed the runtime properly so 8086 okay there's some other issue then
10:09
it's not the problem of this so what I will do is I just try to solve this first okay longbo is not working properly as expected I'll say mavin reload project okay what I will
10:25
also do is I will also make this as a component uh and it looks like most of the ID problem not this and for the service class we can also use component or we can use service package or service annotation so basically for different layers we can use different annotations
10:40
ultimately they are same even service package service annotation uh behind the scene is a component you can see when I hold here it is also a component but uh it is similar but there are some differences which only works for a specific use case and it's still giving this problem so maybe longbo is not working so what I will do is it's fine
10:57
we can just remove this and and create G sets it normally works I don't know what's going wrong there so I'll stick to my basic thing so I will right click here and say generate and let me know if it is working for you on your machine there might be some issue uh right click
11:12
generate Constructor all okay so we got the Constructor now and now it should not be an issue and you can see this is solved some with the longbo okay once we have done this uh next we have to return this products so basically we got products we
11:29
can return it in fact I we can add one more now since it it is looking good uh I will say 103 I'll say sure mic and price is let's say 40 I'm not sure what's the maybe 10 okay so we got three products now and
11:46
we are returning so as you can see all the logic we writing in the service package in the controller nothing now what controller will do is controller will get this data from the service so in here we have to create a product service and we'll name this as service
12:03
is equal to new product service now this is what we don't want to do right we don't want to create the object by ourself we want spring to give it to us and once you got the service I can simply say service because service will return the products so when you're asking for the controller controller will say okay let me get from the
12:18
service so service. get products but it will return the least of product not one it will return the list of products now this will not work is because we are not writing atate or wire so we need the object so we can ask for it so the object will be created inside the spring
12:34
container because of this service annotation which is component behind the scene and you can simply autowire it now some some certain things are missing here on top of this we have to say rest controller and on top of this method we have to say request mapping and for
12:49
which request so whenever I say slash products I want this so whenever you say/ products you have to return this so simple right uh we this work that's the question let's try so what I will do is this time I hope it will work I will just relaunch this I don't know why it
13:06
is saying the port number is busy okay okay now this time problem is different it says uh product required for a bean of type int could not be found the parameter is zero of the Constructor in model required a bean of type in that
13:24
could not be found okay so let me go back to the product it's fun to debug the applications right so it says Pro ID we are returning int for the first one and in the service we are passing int not an issue okay is it required to
13:41
have a no argument Constructor
14:01
okay this time we got another error it says port number 8086 was already in use okay let's solve this looks like there's some issue with the IDE what I will do is I will just close this okay reopening done of the intelly idea let me just Port change the port number to
14:18
8090 I don't know why all the erors are coming today there might be some problem with the ID or maybe I've done I'm doing some mistake let me know in the comment if you can find my problem here some time you can't debug your own stuff uh okay doesn't look like a problem of the
14:33
server is something wrong with the code I'm not sure we are writing rest controller we are saying that this is a service we are returning the products mapping is done properly uh the product service we are
14:50
returning the products we have added it service here things are looking good I don't see any problem is it the issue with the lombok library let me just get rid of lombok Library restart package lombo does not
15:08
exist I'm not even using them remove launch NP I'm not sure what is going wrong and things are getting lendy so I will just restart the entire system and it will work and let's try to see what
15:24
was wrong okay now now this is a part which I'm recording later after some time in fact few hours and we know something right whenever something goes wrong if it is not working uh after trying for making some changes if it's still not working we what we do is we
15:39
restart our system so I've done the restart and I've also ran it it was working so I'm just doing that once more in the video so I will just relaunch this nothing changed you know port number is still same I just restarted my system there was some issue with the uh
15:55
maybe the system or the intelligent I'm not sure but now it's working you can see it says stormcat started on 880 now how do I test it if it's working or not so one thing you can do you can open your browser and go to Local Host 880 and search for products okay still not
16:10
working because the port number we have changed it is 8090 enter and now you can see we got our products uh so I I also installed a plugin in my browser which is pretty printed maybe because of that I'm getting this option but you can see we got the products here I know this
16:26
doesn't look good because in the real world we want to see the p page right uh the design but then we have talked about it we are not concerned about design here we are concerned about the data and we got it here this one we of doing it we can also get this data on another tool so whenever you want to test your
16:42
apis basically what we doing here is we are building apis which will be used by mobile application or it will be used for uh a browser using react application or angular and this will send request to the API API will send data so this is
16:57
one way another way you can also use this Postman but how exactly we use Postman we'll talk about that in the uh next video but at this point uh we are able to run that on the browser just wanted to finish this video with working output so yeah that's it from this video where we have seen different layers see
17:13
you in the next video