Spring Boot Tutorial for Beginners (Java Framework)
Aug 15, 2023
Spring Boot Tutorial for Beginners (Java Framework)
Learn Spring Boot in this full course for beginners. Spring Boot is an amazing framework for building Java applications. It makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. 💻 Code: https://github.com/amigoscode/spring- … ✏️ Course from Amigoscode. 🔗 Subscribe to Amigoscode YouTube channel: http://bit.ly/2HpF5V8 🔗 Full Stack Spring Boot | React | Postgres Course: http://bit.ly/2NGmUx3 🔗 Visit Amigoscode.com for more courses: http://bit.ly/2JgGW0w 🔗 Follow Amigoscode on Instagram: http://bit.ly/2TSkA9w 🔗 Join Amigoscode Closed Facebook Group: http://bit.ly/2FbuIkx — Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://medium.freecodecamp.org
Content
0 -> Hi, welcome to amigos code. My name is Nelson.
And in this crash course, I'm going to teach
4.83 -> you everything you need to know about Spring
Boot Spring Boot. It's an amazing framework for
9.9 -> building Java applications very fast. Recently,
Netflix just announced that they'll be switching
16.41 -> their entire back end to Spring Boot. And this
is because Spring Boot is so solid, that there's
24.84 -> no need for you to reinvent the wheel. So they
provide things such as connectivity to databases,
30.99 -> jetty servers, metrics configuration, and pretty
much instead of you having to configure the low
39.21 -> level code to get up and running, you can simply
use Spring Boot and get off the ground very
45.3 -> quick. So first, I'm going to show you exactly
how to use Spring Boot dependency injection,
50.46 -> and how to implement to an interface where we will
have an in memory database, and then I'm going to
56.94 -> show you exactly how to connect to a real database
running on Docker. So without further ado,
64.62 -> let's get started. Alright, amigos, welcome to
amigos code. In this video, I want to teach you
71.67 -> exactly how to use Spring Boot 2.0. Spring
Boot is by far one of the best frameworks
77.22 -> for building applications in the Java world. So
if you want to build an enterprise application,
83.52 -> you can use Spring Boot, or if you want to
prototype an idea and see whether you know,
90.3 -> it works that you can use Spring Boot because
it makes it very easy to build applications. And
95.76 -> basically, Spring Boot provides you a platform for
building back end applications. And not only that,
102.57 -> you can also build web applications as well. So in
this video, let's go ahead and build this awesome
108.93 -> API, where any client can submit a request. So
these requests could be a get request, post,
118.5 -> delete, and put. And then we're going to take
that request, and within the API layer or control
126.48 -> layer, so this is where we receive the requests,
we're going to send that to the service layer. And
132.9 -> basically, the service layer is responsible to
handle all the business logic that you may have
137.82 -> within your application. After you perform some
business logic, then you might decide to use a
145.35 -> database to perform some various CRUD operations.
And basically, so this layer right here,
152.58 -> so this layer is responsible to connect to any
database of your choice. So the cool thing about
159.15 -> Spring Boot and dependency injection is that you
can start with an implementation. And then if
165.27 -> you want to switch to a different database, you
simply have to change one line of code. And I'm
170.55 -> going to show you exactly how to do that with the
pendency injection. So after you submit the actual
179.04 -> request from any clients, so the clients could
be an iOS app, a react or even an Android app,
186.54 -> the request will go through all these layers, and
then go back with a response to the client. So the
194.67 -> response could actually be, for example, a JSON
payload, or an image, or a status code of 200 500,
202.89 -> so on and so forth. So if you want to learn the
best framework for building Java applications,
208.08 -> stick to this video, because there is a lot to
cover. And I'm pretty sure that after this video,
213.51 -> you will be very comfortable building back
end applications with Spring Boot. So let's
220.53 -> go ahead and get started. So the very first
thing that I want you to do is to navigate
225.06 -> to this website start.spring.io. And basically,
this website, you can see that it's it's titled
233.04 -> spring initializer. And basically allows
you to bootstrap your application by picking
238.62 -> dependencies and configuring the actual project.
So go ahead and generate a project with Maven,
245.28 -> we can see that you can be Gradle. And then
you have the option to code with Java kotlin
252.27 -> or groovy. So I'm gonna stick with Java. And then
for the actual version, make sure that it is above
258.51 -> 2.0. So I've got a video on 1.5. And basically,
I just want to make sure that you guys have the
266.82 -> latest version, so that you can use it within
your latest projects. So then go ahead and click
276.15 -> on this link right here. So switch to the full
version, basically, right here, you can, you know,
281.01 -> configure your project metadata. So go ahead and
change these according to your company or project
287.7 -> name. But for this tutorial, I'm going to leave
everything as is apart from the Java version.
293.82 -> So I'm going to pick Java 11, because I do have
it installed. But if you don't have Java 11 go
299.31 -> ahead and pick Java eight because it will also
work. Then, if you scroll down, you can see that
305.67 -> right here you have a bunch of dependencies. And
this is why springboard is pretty much one of the
312.3 -> best Java frameworks for building applications.
So they give you a lot of flexibility with these
319.65 -> dependencies, which simply means that you can
install one of these dependencies and start coding
326.37 -> right away instead of you having to configuring
and you know, setting up things. So basically,
331.47 -> you just have to install these dependencies,
and then start implementing whatever you want.
337.38 -> So you can see that we have a bunch of sections
right here. So web server web applications with
344.82 -> spring MVC, and Tomcat at jersey WebSockets,
template engines, security, so they make it very
351.78 -> easy for you to configure security within your
applications. So you know, it saves you the hassle
357.75 -> of having to implement security the right way.
So basically, they're doing the difficult job,
363.12 -> which is implementing the security, and then
giving you this nice package that you can just
368.16 -> plug in play. And then you have SQL, so my sequel,
h2 JDBC, Postgres flyway for database, migrations,
377.94 -> Mongo, and then you can scroll down, you can see
that they have a lot of great things. So for this
384.15 -> video right here, let's go ahead and focus on
picking this dependency right here. So web server
392.13 -> web applications with spring receipt and Tomcat.
So I'm going to take that and then generate a
398.31 -> project. So right here, you see that I've got
this demo zip, which I'm going to open in my
403.77 -> desktop. There we go. So now I'm going to click on
this zip folder. And if I delete the actual zip,
416.04 -> you can see that now I have this folder right
here. So for this video, I'm going to be using
421.35 -> intelligence, which is by far the best ID. And if
you want to learn about intelligence, go ahead and
427.92 -> check my video on YouTube, which I show you how to
download, install and configure IntelliJ properly.
434.55 -> And if you are using NetBeans, or Eclipse, this
will also work. So inside of IntelliJ. So I
442.68 -> actually recommend you to download and install
IntelliJ if you don't have it to follow along,
447.15 -> and I'm pretty sure that you're going to learn
great things with IntelliJ throughout this video.
451.35 -> So what I'm going to do now is pretty much click
on open. And then navigate to desktop, and then
460.71 -> open up this demo folder, and then open up the
actual POM dot XML. So I'm going to open that up,
468.15 -> and then open as project. So I'm going to allow
and just give you a second, Maven is resolving
477.84 -> the dependencies for this project. There we go.
So that's nice and done. So let's go ahead and
485.82 -> explore this project. So open up demo. And inside
demo, you can see they have couple of folders,
492.21 -> but the ones that we really care about are
the palm dot XML. So if I open that up,
497.37 -> and see that this is what you saw when configuring
the project with spring initializer. So you saw
505.86 -> the group ID artifact, name, and then version.
So this is the version right here. And then this
515.07 -> is Java 11. Basically, the actual dependency
that we picked was this one right here. So
521.13 -> Spring Boot starter web. And by default Spring
Boot ships with Spring Boot starter tests for
528.18 -> writing unit and integration tests. So go ahead
and close this because we don't need it. And open
537.07 -> up the source folder. So open up source, and
then main. And you can see that you have Java,
544.54 -> and then inside you should have a class called
demo application. And inside of this class,
553.06 -> you have this public static void Main method.
And this is how you pretty much just run your
560.65 -> spring application. And if I play so make sure you
play this. Just give me a second. You should see
570.19 -> that I have spring up and running and you can see
that it says that Tomcat started on port 8080. So
579.28 -> right here, we could change this port but this is
the default port for Tomcat. So I'm going to stop
585.55 -> this and you can see that everything is up
and running fine. I'm gonna stop this and then
591.34 -> collapse this. And then inside of resources, you
have static templates and application properties.
598.93 -> So start is where all the web resources
live for web applications. And then you
606.76 -> have templates. And this is where you have all the
templates for your web application. So usually,
612.88 -> you would use something like mustache or ej s. And
then you have this application that properties.
618.46 -> So this is where all the properties for your
application do live. And the cool thing about
624.19 -> properties is that you can define properties for
different environments. So you could have a demo
630.28 -> environment, a test environment and a production
environment with different values for the actual
636.97 -> properties. So it's pretty cool. So I'm going
to cancel out of that. And now that you know
643.81 -> the actual structure and make sure that you can
start the application, you've successfully managed
650.77 -> to start a Tomcat server with Spring Boot. So
now let's go ahead and implement our API with
658.87 -> all of those resources, controllers, services,
and defining a database as well. So if I go back
667.9 -> to this diagram, so remember that I said we had
the API layer controller, or controller layer,
679 -> and then we have service layer for business logic,
and then database for data access. So let's go
686.05 -> ahead and start things right here. So we're going
to start right here. And in fact, this I mean, put
692.86 -> a dot right here, so you know what we're doing. So
if I remove that, basically, let's put just a.so,
701.23 -> we know where we are, with things. So right
here. And then if I collapse that, so basically,
711.7 -> we're going to implement this section first,
and then move our way up. So basically, we're
718.24 -> going to implement one step at a time, and then
see everything working fine, and then implement
725.95 -> some other functionality. So let's go ahead and
go back to IntelliJ. And the first thing that I
734.02 -> want you to do is to create a package. So let's
go ahead and create a package. And right here,
742.69 -> simply call it API. So this is where the API
will live. Go ahead and create a second package,
750.49 -> and simply say, model, go ahead and create
a third package, call it service, go ahead
759.07 -> and create a fifth package or actually a fourth
package and call it so we need the actual da Oh,
767.08 -> and I think this is it. So we have API d o model
and service. And basically these packages are
778.75 -> representing this diagram. So if I go back, and
perhaps you haven't seen me, like right clicking,
789.16 -> so new, and then a package. So basically, I tend
to use a lot of keyboard shortcuts. And this is
796 -> how I am productive with IntelliJ. So if I press
Ctrl, and then enter, you can see the keyboard
803.02 -> shortcut down below. And basically, then that
gives me the ability to create a new package.
809.08 -> So the same if I, for example, create a variable.
So if I say var, and then ELO equals to two. And,
822.61 -> and then if I press Command, and then why you can
see the keyboard down below, right? So I've just
830.44 -> deleted that line. So also, let me go ahead and
say, Okay, so now what we need to do is actually
837.52 -> define our model. So what is the actual model for
this application? So if I go back to this diagram,
845.59 -> so let's go ahead and define a model. So I'm going
to copy this stereotype. And you can grab this
852.01 -> diagram, you can find the link in the description
below where you can download and see exactly,
856.87 -> you know, everything that we're doing here. So
basically, the actual model, so oops, so the model
863.65 -> for this tutorial will be a person. And basically
a person will have an ID. So when ID. And let's
873.82 -> keep things simple and have like, let's say a full
or actually name, just like that. So obviously,
881.29 -> you could have more properties. But I want
to keep it simple for you see exactly what's
885.88 -> happening. And so things you know, become really
obvious to you. So we have this model right here
893.44 -> called person. Let's go ahead and define this
in actual code. So go back to IntelliJ and in
899.935 -> side of the model package, go ahead and create
a new class and simply call this person. Right,
908.32 -> so the kind it's a class, and then press enter.
So now simply go ahead and say private final, and
915.49 -> then the ID will be of type U ID. And simply call
the Id go ahead and simply save private final,
924.25 -> and then string. So this is the actual name. And
then I'm going to add these two constructors. So
931.42 -> you can see I'm using keyboard shortcuts again,
and then the getters. So just like that, and there
938.56 -> we go. So now we have a model that we can work
with. So if I go back, so now we have to define
946.42 -> the actual database section right here, right, so
let's define the actual interface that will allow
954.37 -> us to have any implementation for our database. So
inside of the DA, Oh, go ahead and create a class,
962.98 -> and then simply save person and then the A Oh,
so instead of like class, change this to an
969.94 -> interface. And this is the actual interface where
we're going to define the operations allowed,
976.87 -> or the actual contract for anyone that wishes to
implement this interface. And the cool thing about
984.25 -> it is that we can then use dependency injection
to switch between implementations with just one
991.09 -> line of code. So let's go ahead and define our
very first method. So for our database, we want
997.87 -> to insert a person into the actual database. So
we will simply return an integer. And basically,
1006.09 -> we're going to mock a database using like a
list. And basically, we will return zero or one
1011.94 -> depending whether the actual data is persisted to
the actual database. So let's simply go ahead and
1020.28 -> say insert, and then person. And this will take
a user ID, which is the actual ID, and then a
1030.6 -> person just like that, and that with semicolon.
Now, let's also go ahead and create a default
1038.28 -> method. And basically, this will be the same
thing, or actually sorry, this will be integer.
1044.91 -> So it will be the same thing. And let's simply
call this ad and then person. And right here,
1052.74 -> we will simply take person, right. And what this
allows us to do is to actually generate the you
1060.42 -> IDs, ourself, or the actual ID, so you would, and
then let's call the ID equals two, and then UAT,
1068.42 -> dot and then run the mu ID, and then simply say,
return. And then insert person passing the ID, and
1078.29 -> then the actual person. Yep. So basically, we have
two methods, one that allows us to insert a person
1086.39 -> with a given ID, and the other one without an ID.
And the ID is simply randomly generated. Now let's
1094.91 -> go ahead and create a class that will implement
this interface. So open up the DA o package,
1102.32 -> and then simply create a class and call it person
and call it fake. And then person data, access,
1112.94 -> and then service. And then this guy implements
and then person, da, oh, just like that, and then
1122.27 -> implement the methods. And we need to implement
insert person, right. So right here, let's go
1132.2 -> ahead and define our list. So private, and then
static. And then and then list. And this will be a
1141.98 -> list of person, simply quality b equals to a new,
and then array list just like that. Or, in fact,
1153.11 -> we could even initialize this inside of a static
clock, if we wanted. So now to insert a person,
1160.94 -> all we have to do is simply add to this database
right here. So for the sake of this, simply go
1169.67 -> ahead and say dB, dot and then add, and then new
and then person and simply take the actual ID,
1178.25 -> and then simply say person, dot and then get name.
Right. So And finally, let's simply go ahead and
1185.93 -> return one. So we know that it always works. So
the insertion always works. So now let's go ahead
1193.67 -> and actually define the actual service. So inside
of the service package, go ahead and create a new
1199.55 -> class. And then simply call it person and then
service. And right here inside of this class,
1206.42 -> let's see if we have a method to insert a new
person. So simply go ahead and say public,
1213.35 -> and then int, and then and and then person. And
basically, this will take a person. And what we
1223.07 -> want to do is simply say return. And right here,
we need to actually get a reference of the actual
1231.23 -> person, da Oh, so let's go ahead and simply say,
private, and then final, and then person, da, oh,
1238.97 -> and make sure that you get the actual interface
and not the concrete class. So don't get the
1244.88 -> actual fake person data access service, because
I want to show exactly how to use dependency
1250.91 -> injection. So then what we need to do is added to
the constructor. And now we can simply say person,
1258.02 -> da, O dot, and then insert, and then person. So
remember, we had two methods. And if I go back
1265.97 -> to my interface, so person to you, oh, and in
fact, I need to call this the same. So let's be
1273.08 -> consistent. So person, so insert person, actually.
So now you can see that the error went away. So
1278.36 -> basically, we have the option of providing the ID
or not. So in this case, let's go ahead and have
1284.9 -> it, you know, randomly generated, because we don't
care. But if you need to generate the ID yourself,
1291.44 -> you know that you can do it. So now that we have
the service done, let's go ahead and implement
1297.68 -> the actual API. So inside of the API package, go
ahead and create class, and simply call it person.
1305 -> And then resource. Or actually, let's go ahead and
save controller. So in Spring Boot world, we tend
1312.23 -> to name these as controllers. So and then simply
say, OK, and then let's go ahead and also have
1320.18 -> a reference to the actual service. So private,
final, and then person service. And then right
1328.58 -> here, we need to add this to the constructor. And
then let's simply go ahead and have a method that
1335.12 -> adds a person, so public, and then simply say,
void, so we don't want to return an integer in
1341.99 -> this case. So we want to, so we want to return
a status code of 200. Or if anything fails,
1348.53 -> we can pretty much throw an exception. So or
you could also have your custom exceptions,
1354.2 -> if you wish. So let's go ahead and simply say add,
and then person. And this will take a person, so a
1362.21 -> person just like that. And then what we need to do
is actually say person service, dot and then add
1370.01 -> person, just like that. So up to this point, we
simply have normal Java classes. And to be honest,
1378.95 -> we're not even using spring framework at this
point. So let's go ahead and configure everything
1385.01 -> to start using spring framework. So let's go ahead
and open up the actual da Oh, so fake person data
1395.09 -> access service. And you'll see right here inside
of the actual service, we have a reference to it.
1401.96 -> So person, da O. And basically, we are trying to
use dependency injection. So if I was to run this,
1408.98 -> this would wouldn't work because this person
deal is not instantiated. And you know,
1417.95 -> there's no way for this constructor to know about
it, right. So this is where we use annotations to
1425.24 -> instantiate beans. So open up the fake person
data access service. And the way that we tell
1433.76 -> spring that this class needs to be established,
created as a beam so that we can inject it
1440.33 -> in all the classes is simply by saying act and
then repository. Right. So you could also say add,
1447.92 -> and then component. So this is the exact same
thing. But repository makes it obvious that this
1454.4 -> class is served as a repository. Right. So now
that we have a repository, let's go ahead and
1461.97 -> configure the actual service. So for service,
you might take a guess. So this will be add and
1467.46 -> then service. Right. And you could also use a lead
component if you wish. But I usually prefer to be,
1474.54 -> you know, specific. So anyone looking up my code,
know exactly that this is a service. So now that
1483.42 -> we pretty much annotated the actual data access
service, and right here we are injecting right
1491.79 -> so we are injecting into the actual constructor.
So the way that we inject is simply saying add and
1499.08 -> then auto wired. So basically, we are auto wiring
into this interface, right. So now, because we can
1509.79 -> have multiple implementations of this interface
right here, we must have a way to distinguish
1515.67 -> between them, right. And this is when use at
and then qualifier, right? So add qualifier.
1522.72 -> And then right here, you can give it a name, so
fake and then da O. And basically, I need to go
1530.04 -> back to my fake person that access service. And
then right here, I'm going to name this as fake.
1536.01 -> And then da O. And what this allows me to do is
to have multiple implementations. And basically,
1544.2 -> all I need to do is for example, if I have, for
example, a Postgres or a MongoDB implementation,
1550.77 -> I simply say Mongo, just like that, and then
I don't have to change anything. So let's go
1558.06 -> ahead and use the fake da Oh, and now this is why
it's correctly. So finally, let's go ahead and
1565.92 -> open up the actual API package. And then personal
controller. Let's also go ahead and simply say,
1572.31 -> at an auto wired, basically, springboard injects
the actual service into this constructor. And
1580.23 -> if you want to learn more about dependency
injection, go ahead and check my channel,
1583.92 -> I made a video specific on dependency injection,
and why it's important for you to know about it.
1590.43 -> So now, remember that I said that this class right
here, so the API, if I go back to our diagram,
1598.65 -> is this one right here. And right here, we
can specify HTTP methods. So get post, delete,
1606.96 -> and put. So these clients right here, right, so
they have to issue some requests to our back end.
1615.51 -> So right now, there is no code that tells that
this method right here will be served as a post
1623.91 -> or put or get or delete. And also, this class is
not available as a rest controller. So to do that,
1633.45 -> we need to use a special annotation and simply
say, add, and then rest controller, right. So
1639.48 -> this is a rest controller class, that we can have
some methods, and then expose some endpoints that
1645.78 -> clients can consume. So in our case, what we
want to do is have this method right here to
1655.02 -> be served as a POST request. So basically gets is
for retrieving data from your server posts is to
1665.07 -> adding a resource to your server, put modifying,
and then delete for deleting a resource,
1671.73 -> a resource from your server. So to tell spring
that this method will be used as a POST request,
1681.21 -> we'll simply use the annotation at and then post
and then mapping. And this is all we have to do
1688.8 -> now. So obviously, we want to add a person from my
client, right? So go ahead and download postman.
1700.2 -> So postman allows you to pretty much just serve
as a client, because we don't have, for example,
1706.29 -> a react application or an Android app, or you
know, you we don't have any, any any client,
1713.22 -> right. So we can use postman as our client, right.
So go ahead and download postman, it's an amazing
1722.07 -> client. And what we need to do is actually send a
person to our server. So before we do that, so let
1731.61 -> me actually clear everything here. And basically,
go ahead and say post, and then this will be local
1740.13 -> host, column, and then AD AD. And then for slash,
and then simply say API, forward slash, and then
1748.5 -> v1, and then person, right. So after you define
the actual endpoint, go ahead to the actual body,
1757.74 -> and then simply say raw, and then instead of text,
go ahead and send a JSON. So we want to send a
1767.01 -> JSON payload to the actual server. And remember,
we have name, and let's say that this is James
1774.72 -> and then bond. So basically, we want to send James
Bond to the server. So in order for us to do that,
1782.43 -> we have to do few things. One is we have to define
these properties in our pojo so that when we send
1792.06 -> this JSON spring knows to take this property and
then convert that into a jar. Have a class, right.
1801.39 -> So in order for us to do that, what we need to do
is open up person. So inside of the model package,
1807.84 -> open a person. And then you'll see right here in
the constructor, let's go ahead and define some
1814.98 -> properties. So JSON property, and this will be
the ID. And then right here, so if I put this on
1823.29 -> your line art, and then Jason property, and this
will be the actual name, right? So you could also
1830.64 -> define more properties. But as I said, Let's keep
this simple so that you understand exactly what's
1837.03 -> going on. So if I open a postman again. So right
here, you see that name. So this name right here
1846.12 -> matches the property that we have in our class
person. And if we wanted to send the ID, so we
1854.25 -> would simply say ID and then pass, you know an
ID, but we are generating the ID from the server,
1863.57 -> therefore, we don't need to send it. Now what I
need to do is actually tell that we are receiving
1872.93 -> this JSON payload from the actual body. So inside
of person controller, go ahead, and you see right
1882.29 -> here where you add a person. So right here, simply
annotate this with ADD, and then request. And then
1890.9 -> body, right. So we are simply saying that we
want to take the request body, and then shovel
1897.8 -> that inside of this person. And then because we
have, so right here, so because we have these,
1905.06 -> this property right here, we will simply turn
this JSON object into a person. That's what
1912.14 -> we're saying. And the final thing that we have
to do is actually define this path right here,
1917.09 -> right? So right here before rest controller simply
say at and then request. And then mapping. And the
1929.24 -> actual mapping will be API, v1, and then person.
So this is all you need to do in order to create
1937.25 -> your very first endpoint with Spring Boot. Now,
what we need to do is actually test it. So let's
1943.94 -> go ahead and simply play the application. So as
you can see, Tomcat started on port 8080. Now,
1955.1 -> if I open up postman, and then simply send this
request right here, you can see that we have a
1966.83 -> 200 status code. And this means that the request
went through fine. Now, obviously, we don't have a
1975.98 -> way of testing this, right, because we don't have
an endpoint to retrieve people from our database.
1983.12 -> So let's go ahead and define the functionality
to get people from our database. So let me go
1990.84 -> ahead and open up the IntelliJ. And if I collapse
that, so go ahead and open up the person da Oh,
1998.91 -> and let's go ahead and say that this will return
a list of person and say, select all people. And
2009.83 -> then let's go ahead and open up the person data
access service. So we need to implement that. And
2018.5 -> then right here, what we need to do is very simple
within the actual database, and open up service.
2026.06 -> So right here, simply create a method public. And
then list of person, get, and then all people and
2042.05 -> then this will return person, the Oh, don't and
then select all people. So now we need to define
2051.38 -> the actual resource or the actual method that will
serve as the get request. So simply say public,
2059.96 -> and then a list of and then person and then get
all people and then what we're going to return
2072.29 -> is the actual person service dot and then get
Oh people, right. So now we need to tell that
2080.72 -> this method will be served as a get request.
And the way that you do it is simply by saying
2086.78 -> add and then get and then mapping. So you see
that everything has the same pattern at the post
2094.16 -> mapping at getting mapping. Now, let me go ahead
and pretty much just restart the server There we
2105.83 -> go. So the actual server started on port 8080. So
open up postman. And then let's actually try and
2117.32 -> send the actual same request. So we're going to
send James Bond can see that worked. And now let's
2126.26 -> go ahead and try and perform a get request to our
server. So the exact same URL, send, and there we
2134.9 -> go. So you can see that we have an array with one
person that comes from our database. So this is
2141.32 -> awesome. Now let's go ahead and perform a nother
post. So so right here, so change this to Nelson,
2150.2 -> and then Mandela, you can change this to whatever
name you want to be honest, I'm going to send that
2156.53 -> you can see that it's 200. And then simply change
this to Anna, and then Jones, send. And now if I
2167.48 -> perform a get request to our server, you can see
that we have three people. So James Bond, Nelson,
2176.12 -> and Anna Jones. So as you see, springboard is
just incredible. It's super fun. And as you saw,
2185.15 -> like, it didn't take us long to implement this
functionality, right, we're just using annotations
2190.73 -> to drive our implementation, which is awesome.
And we can pretty much deliver results very fast,
2198.17 -> using Spring Boot. Now, let's go ahead
and implement the other two HTTP methods,
2204.26 -> the ability to update a person, as well as
deleting a person. So open up IntelliJ. If
2212.09 -> I collapse this, and then open up the actual
da Oh, and let's define the actual interface,
2218.78 -> or actual method. So right here, simply say int,
and delete, and then person by ID. And right here,
2228.71 -> we need to pass the actual ID, so you ID and then
ID. And let's also define a couple of things. So
2235.94 -> let's also define that we want to update a person.
So this will return an integer, and then simply
2242.93 -> say, update. And then person by ID. So right
here, we're going to pass the actual ID and the
2254.72 -> actual new person that we want to update, right,
so person. And also, let's define one more method.
2263.66 -> And this will be an optional of and then person.
And then simply say, select person by and then ID,
2275.21 -> and then simply pass the actual ID inside. And
there we go. So now let's go ahead and open up
2283.04 -> the fake person data access service. And we have
to implement all of those methods. So I'm pressing
2288.77 -> Alt, and then enter, and then implement methods.
And you can see that we have these two methods
2294.77 -> that we need to implement. Okay, and error is
gone. So in fact, let's go ahead and implement,
2301.37 -> select the person by the first. So to implement
this method, what we need to do is actually search
2309.26 -> within our database and see whether we have a
person with the given ID so this one right here,
2315.86 -> so the way we do it is simply by streaming our
database. So let's go ahead and use Java streams,
2322.16 -> dot and then stream. And if you want to learn
more about streams, go ahead and check my video
2328.4 -> on Java streams where I cover this. So let's
go ahead and simply say filter. And right here,
2336.14 -> we get a person. And then let's simply say that
person dot and then get ID dot equals to ID,
2344.51 -> right. And then what we want is dot and then
find, and then first. And there we go. So now
2353.99 -> let's go ahead and open up the actual service,
and then simply implement the same method here,
2360.05 -> so public, and then this will be an optional, and
then person, and then get person by ID, and then
2372.6 -> simply pass the actual ID. And you can see that
it's almost the same thing that we're doing right
2379.14 -> here return and then person do dot and then get
or actually select person by ID, and then pass
2389.76 -> the ID and then open up the controller. So person
controller, and this will be a get so basically
2399.81 -> We went to public. So this will be person and then
get in then person by ID. And then we're going to
2411.21 -> pass the ID, and I'm going to show you exactly how
do we get that. So Id, and then what we want to do
2417.57 -> is simply say return. And then person service dot
get person by ID, lowercase ID, and then.or. Else,
2429 -> and then no. So right here, you could actually
have a custom message or a custom exception,
2436.92 -> or a custom code thrown to the actual client. So
for example, a 404, with a message saying, user
2444.36 -> not found, but for this video, let's go ahead and
keep it simple and simply return now. Right? So
2451.14 -> now what we want to do is, if I open up postman is
you see this ID right here, right? So I want to be
2458.97 -> able to copy the ID, and then simply say, forward
slash, and then pass the ID. And then when I send
2467.61 -> the request, I should get that person back. Right.
So the way that we get the ID from the path is by
2475.74 -> simply using an annotation, and the annotation is
called. So this one is at, and then path and then
2484.5 -> variable. And the PATH variable that we want, is
an ID. So Id, just like that. And basically, this
2495.51 -> is a type right here. So now let's go ahead and
define the actual mapping. So this will be add,
2501.6 -> and then get and then mapping. And now because we
want to have the actual ID in the path, so inside
2511.2 -> simply save path. And then what you want is double
quotes, and then curly brackets, and then pass ID.
2519.93 -> So basically, this will allow you to have forward
slash and then some ID. And then right here,
2527.16 -> you're simply grabbing the ID, and then turning
that into a unit. Right. So let's go ahead and
2535.17 -> test this. So if I pretty much just restart
the actual server, there we go up and running.
2546.48 -> So if I open up postman, now we need to issue
some requests. So you can see that, you know,
2555.51 -> every time that we restart the server, we lose
everything from our list. That's a disadvantage
2560.49 -> of having an in memory database. But for this
video, you can see exactly how everything works.
2566.52 -> So let's go ahead and send a POST request there.
And then let's get everyone so remove the actual
2575.79 -> you with there. So send. So we have knelson, right
there. And basically now we can grab Nelson's ID,
2587.34 -> and then do a forward slash, and then paste that.
Right. So now if I send, you can see that we
2596.37 -> have only one object, right? It's not It's not an
array, but it's an object. And if I was to change
2603.96 -> the ID for something that doesn't exist, send, you
can see that actually, it's a bad request. Because
2612.93 -> the you with it's completely wrong. So it's not
valid you with that I've just sent to the server,
2618.78 -> but you get the idea. So if I go ahead, and
let's go ahead and add Anna Jones. So send,
2627.15 -> and then let's get everyone, and let's grab
Anna Jones ID right there. And then Ford slash,
2636.84 -> send that. In, see that works. Right. And in fact,
let me go ahead and say you wait generator, and
2647.04 -> let me generate the unit. So I want one unit here.
And if I grab this, and then go back to postman,
2656.79 -> and then perform a get request with that up there.
So if I delete everything, and then paste that
2666.06 -> yo ID and then send you see that it's a 200. But
we have nothing in our database without your ID.
2678.84 -> Right? So as I said, so you shouldn't be throwing
a 404 with a custom exception. But in this video,
2687.03 -> you can see exactly that we are returning nothing
because there is no one without you it against our
2692.82 -> database. So let's go ahead and implement the
remaining methods. So if I collapse this, so
2699.87 -> Let's go ahead and implement everything in one go
now. So open up the actual fake person data access
2706.44 -> service. And to delete a person, what we need
to do is simply get the person if the person is
2715.62 -> there, delete it, otherwise, return zero. So let's
go ahead and simply say select. And then person by
2725.16 -> ID, we're going to grab the ID. And remember, this
returns an optional, so person, and then maybe,
2732.57 -> and right here, I'll simply say, if, and then
person may be dot, and then is empty. So if it is
2742.92 -> empty, we want to return zero. Otherwise, what we
want to do is actually remove from the database,
2753.63 -> right, so if it's there, we want to remove so DB
dot and then remove, and then person, maybe dot
2763.98 -> and then get just like that. And then return one,
right, so we know that we did delete this person.
2772.14 -> Now let's go ahead and implement update person.
So what we're going to do is simply say return and
2779.67 -> then get or actually select person by ID, we're
going to pass the ID. And then what we want to
2787.74 -> do is say dot and then map. So right here, we get
a person, let's call it P. So P. And as I said,
2797.25 -> if you want to learn about, you know Java streams,
maps, operations filtering, go ahead and check my
2804.3 -> YouTube video where I teach all of this. So now
let's go ahead and try and get the actual index
2810.45 -> of this person. So int, and then index of and
then person to delete equals to and then DB dot,
2821.22 -> and then index of and then pass the actual
person, right. So now we have an index. So I'm
2828.81 -> going to say if the actual index to the leat and
I'm missing a knee right there. So if the index
2836.4 -> to delete is greater or equal to zero, what we
want to do is actually set the actual person, so
2846.96 -> we means that we found someone, right, so let's go
ahead and say DB dot and then set and then index
2853.98 -> of the person to delete, and then simply pass the
actual person, right. And now what we can do is
2861.69 -> simply return one right here. So otherwise, so
else, or we can simply say return and then zero.
2871.14 -> And then right here, so because we need to return
an integer, simply say don't, and then Or else,
2879.3 -> and then zero. Right. So basically, if I format
this, so basically, I'm saying select the person,
2887.55 -> and then map that person to something else,
right. And if the actual index of that person
2897.48 -> is greater or equal to zero, we know that we
found that person, and then set the contents
2904.19 -> of that person to the new person that we've just
received from the client, and then return one,
2909.83 -> everything is fine. Otherwise, return zero. Or
if select person by the is not present, we don't
2918.23 -> do anything and simply return zero. So this is
what it means. So now let's go ahead and simply,
2925.1 -> you know, feed this through the service, as well
as controller and then test it. So let's go ahead
2931.76 -> and simply open up pass and service. So let's
go ahead and save public and then ain't delete,
2938.54 -> and then person. And then right here, we're going
to take the actual ID, so Id, and then this will
2948.44 -> be very simple. All we need to do is simply say
return, and then person do that. And then the lead
2955.7 -> by ID pass the actual ID, and we're done. To
update a person, sorry. So if I close that, so to
2964.01 -> update a person, what we need to do is simply say,
public, and then ENT, update, and then person. So
2972.5 -> we're going to take the actual ID, so you ID and
then ID and then this person is the new person,
2981.89 -> so new and then person. Right? And then what
we need to do, I simply say return personally,
2990.26 -> oh Don and an update, and then Id right. So you
can see that I don't have much logic going on
2999.8 -> here. Here. And this is because, you know,
I'm just teaching you exactly how to use,
3003.88 -> you know, Spring Boot to to create an API. But
essentially, you know, inside of this service,
3010.36 -> you will have whatever logic your application
requires, right. So I'm just making this simple
3017.29 -> for you to understand exactly. So you don't
have a lot of code going on. Basically, you see
3022.6 -> the entire process of what we're doing. So now,
let me go back to the actual person controller,
3029.17 -> and simply expose those to a REST API. So the
first one is update, or actually delete. So let's
3039.43 -> go ahead and say delete, so public, and then
void, and then delete, and then person by ID,
3048.01 -> we simply pass the ID, and pretty much the same
thing at and then PATH variable. And then this
3057.64 -> will be the ID. And then we want that to be
a u ID, ID. And then return, Oh, actually,
3069.49 -> there's no need to return here, because this is
void. So simply say person service dot, and then
3076.3 -> the lead person passed the ID. Now this has to be
guess what at, and then remember, we are deleting
3084.7 -> a resource from the server. So delete, and then
mapping. So now inside, we have to have the path,
3093.73 -> and then the actual ID, right, so this ID is this
one, right here, which we can pass down to the
3103.42 -> service. So let's also go ahead and implement
the actual update. So the update will be act,
3110.17 -> and then put, and then mapping, and public. And
then Floyd, and then update person. And then this
3126.43 -> will take a person from the request body. So
art, and then request body will take a person
3135.04 -> to update. There we go. And now simply say person
service dot and then update. And in fact, we also
3147.19 -> need the actual path. So the path will include the
actual ID. So Id, and let's go ahead and say that,
3157.03 -> and then path and then variable. And that will
contain the ID. And this will be a user ID,
3171.29 -> and then call it ID. And there we go. So now we
can pass ID and then person to update. Right.
3181.73 -> And to be honest, this is all we have to do to
implement our REST API. So let's go ahead and test
3190.1 -> it. So I'm going to pretty much just restart the
server. So let me go ahead and open up postman. So
3199.58 -> let's start with one person. So let me send this
request. And then let me get everyone. So send,
3209.87 -> you see that we have Nelson Mandela. So let's go
ahead and change Nelson's name to something else.
3217.97 -> So go ahead and simply create a new request. So
go ahead and change this to a put and the body.
3230.45 -> So instead of saying, Nelson Mandela, let's simply
say that this is Alex. And inquiring, right. And
3239.9 -> now let's go ahead and simply pass Nelson Mandela
right here. And basically, we're just changing his
3248.48 -> name. So if I go ahead and send, you can see that
the request did work. And now if I get everyone
3257.33 -> from our database, so if I perform a get request,
and then send, you can see that that didn't work.
3266.21 -> And let's go ahead and debug this in a second.
But for now, let's go ahead and pretty much try
3273.11 -> and delete this person. So Nelson Mandela. So
if I put forward slash and then the actual ID,
3280.22 -> and then delete, and then send, we got 200. If we
get everyone, you see that Nelson is not there,
3292.16 -> and it's been deleted. So we had a bag. So if
I post let's go ahead and Post, Anna Jones. And
3302.48 -> basically, we couldn't update the actual name. So
if I get an A Jones, get the actual ID, and then
3312.83 -> perform a put. And this will be Anna Jones Id just
like that. And then body. And then let's, let's
3330.2 -> simply say update and then update, send. Still a
200. But if we get everyone, you see that still
3341.99 -> Annie Jones, so we have a bank. So let's go
ahead and try and find out why exactly this is
3347.21 -> happening. So, let me go back to IntelliJ and then
close this and then open up the actual data access
3357.5 -> service. And now this is the actual method. So
we are selecting the person mapping. And then so
3368.33 -> basically, so right here we have index of person
to delete. So this would be actual update. So to
3377.15 -> update and DB index of person. So basically,
yes, so this is the error. So so right here,
3390.83 -> so you see that we have, so this person right
here. So this is what we want to update. So right,
3396.83 -> so let me rename this to update, right, so the
index of person to update, you know, it's minus
3405.38 -> one. So this will always return zero. So what
we want is actual p, so P is the actual person,
3413.09 -> in fact, just I mean, rename this to person. So
we want this person that we found. So person,
3420.56 -> and then this should return the actual index. And
then if it's bigger or equal to zero, what we want
3428.42 -> is Neal, so because this update doesn't contain
the actual ID, so we need new person, and then
3437.75 -> pass the ID, and then simply update dot and then
get name. So yeah, I think this makes more sense.
3447.11 -> So let's go ahead and restart the server. There
we go. So now if I open up postman and then pretty
3463.25 -> much just get so let's start from scratch. So no
one in the database. Let's go ahead and post Anna
3470.84 -> Jones. And now we have to get let's go ahead and
get so we get the actual uod of Anna Jones. And
3480.95 -> then right here, let's go ahead and perform a put
to this endpoint and simply pass Anna Jones you
3492.32 -> ID or ID. And then in the actual body. Let's also
change this to Anna, and then Montana, just like
3501.08 -> that. And then if we send, so we got 200. And now
this should work. So go ahead and send. And that.
3510.74 -> So you can see that this now works. So you see
that we pretty much just managed to implement
3518.3 -> all of the methods defined in our API. So it is
nice to have like tests for everything right here.
3527.42 -> And basically I wrote tests for this class. So
go ahead and download the repo down below. And
3534.83 -> you can run all the tests and see that this works
as expected. So I've got unit tests around all of
3541.55 -> this. So one last thing that I want to mention
is that you see inside of this person controller
3549.74 -> right here. So this person controller, we
can have, you know, some annotations that
3556.84 -> enforce null ability in our application. So we can
pretty much just use one, attach some annotations,
3565.9 -> and enforce that this object right here is always
present, and also valid. So go ahead and open up
3574.27 -> person model. And then right here, what we can
do is actually say that this field right here,
3582.4 -> so the actual name, this cannot be blank, so act
and then not and then blank, right? So the reason
3591.73 -> why I'm not saying that no, is because if you have
an empty string, it's not No, but if you have an
3598.12 -> empty string, it's actually blank. So basically,
we don't want that to be blank, right? So then go
3604.54 -> back to the actual controller. And now you can say
that the request body. So this has to be. So add
3612.7 -> and then valid, right? And then we can also say
that this must not be no. So we could also do the
3620.83 -> same here. So we can enforce that this is valid,
and also add and then not know, right? So now if I
3629.68 -> pretty much restart the server, so you see exactly
what we're doing. So basically, we want to fail as
3636.7 -> soon as possible instead of having to deal with
nullability inside of our code. So now let's go
3643.57 -> ahead and open up postman. And then let's go
ahead and pretty much try and post someone
3652.03 -> to our database. So right here, so you see that
we have Anna Jones, but if we send for example,
3658.39 -> blank, so send, you'll see that we get an error.
And basically, this is working as expected,
3666.16 -> as expected. So you see that it's a bad request.
And it's pretty much saying that validation fail
3673.15 -> for object person right here. So this is awesome.
And you can see the actual default message,
3679.06 -> which says that the field name must not be blank.
So now if I go ahead and add the correct payload,
3688.64 -> so right here, so let's go ahead and say, john,
and then Jones. And then send, you can see that
3699.53 -> we have a 200 status code. And if I go ahead and
get everyone in, see that we have john Jones. So
3708.17 -> this is it to be honest. So now you should be
familiar with Spring Boot and writing API's for
3717.95 -> your application. So now the cool thing about it
is that you can have a client API, and then do
3723.8 -> all kinds of things depending on your application.
So one more thing, actually, that I forgot to show
3729.71 -> you is dependency injection, right? So let's say
that we want to have a Postgres implementation,
3737.24 -> right, another database or connecting to a
different database. So how do we change that
3743.18 -> without changing pretty much anything apart
from one line of code. So to do that, what we
3750.05 -> need to do is open up the actual do package, and
then simply create a new class. And right here,
3757.58 -> simply call it for example. person listening
was call it person. And then data, access,
3769.34 -> and then service, right, let's simply call it
that. So this is a real thing now. So okay,
3775.55 -> implement and then person, da, oh. And then
implement all the methods. And then right here,
3786.89 -> so when we select everyone, I simply return
list, dot, and then all of and then new,
3797.24 -> and then person, your weird dot random mu ID. And
then, let's simply say from Postgres dB, right? So
3808.64 -> now what we need to do is annotate this with ADD,
and then repository, and then simply say that this
3816.77 -> is a Postgres implementation, right? And then to
change our implementation, right, all we have to
3826.41 -> do is grab this name right here, and then go where
you inject the actual interface, which is inside
3833.1 -> of the service, right? So person service. So now
instead of fake do simply use Postgres, like that.
3841.68 -> Now, if I restart the actual server, and then
open up postman now if I perform a get, you can
3856.59 -> see that we pretty much switched our database,
we just one line of code. And you can see that
3862.35 -> this now is coming from a Postgres database. So
I'm actually planning to do a video on you know,
3870.24 -> connecting to a real Postgres database. But go
ahead and check my channel because I've got a
3876.36 -> video on how to spin up a Postgres container using
Docker, and also an example with Spring Boot. So
3882.33 -> go ahead and check my video on that. But I'm gonna
make a second part on this video connecting to a
3888.96 -> real database. So the last thing that I'm going
to show you exactly how to spin up everything.
3896.76 -> So go ahead and stop your server. Now what I want
to show you is actually how to spin up the actual
3903.36 -> jar. So this is the, the the jar that you can
deploy to a server and have your application up
3910.29 -> and running. So I'm going to simply collapse that.
And then inside of Maven, so go ahead and open up
3918.15 -> this tab right here. So Maven, and you should
see a target folder. So if you don't see that,
3924.18 -> so if I delete that, simply go ahead and open up
lifecycle, and then simply say, install. There we
3939.09 -> go. So this is actually running some tests. And
there we go, you see that we have a target folder
3944.82 -> right here. So inside of the target folder, go
ahead and simply right click, and then open in
3952.14 -> Terminal. So you should see that we have this demo
001 snapshot dot jar, the jar, and this is what
3962.91 -> we can pretty much just run on the actual server,
right? So when you deploy this to any environment,
3969.54 -> so just to show you, so if I open a post, man,
so if I send a request, you see that the server
3977.46 -> is not up and running. So to kick off this jar,
all you have to do is simply say Java, and right
3985.35 -> here. So if I scroll up, you can see that so if
I scroll up, you'll see that we can say Java,
3992.07 -> and then minus jar, and then pass the actual
jar. So go ahead and simply say Java, minus,
3998.58 -> and then jar, and then the actual job. So the job
name is demo. And then if I press tab, you'll see
4007.13 -> that we get auto completion. And then all I have
to do is press Enter. And you can see that now we
4015.05 -> are starting our server. So right here, Tomcat
started on port 8080. And what I'm going to do
4022.67 -> now is simply open up my web browser. And this is
the actual API that you would use, so localhost,
4029.6 -> and then 8080, and an API, and then v one, and
then person, and then enter. There we go. So
4040.13 -> you see that we have a JSON that we can consume.
And basically, you can distribute this and have
4047.06 -> 1000s of clients consume your API. So I hope you
had fun implementing this application or this API
4057.14 -> with Spring Boot. And basically, we achieved
everything. So we have, you know, a client.
4063.23 -> So in our case was postman, we implemented get
post, delete, and pull requests. Through the
4070.02 -> actual controller layer, we had a service layer
data access layer for a database, you saw how
4077.67 -> to implement an in memory database. And then you
saw how easy it was to switch from databases. And
4084.72 -> then we return the actual response to the actual
client, which is what you see right here. So as
4092.16 -> I said, I'm going to do a second part on this
video, I really want to add value by connecting
4098.67 -> to a real Postgres instance, which to be honest,
is not going to be a lot of work, as you saw,
4104.61 -> it's just switching one line of code and then
implementing the actual connectivity to Postgres.
4119.07 -> So now, let's actually go ahead and pretty much
just use, we're actually connected to a real
4129.93 -> database. The database engine that we're going
to be using for this second part of this crash
4135 -> course, is this amazing database engine called
Postgres. So if you want to learn more about
4142.23 -> Postgres, you can go ahead and check my website,
where I've got this PostgreSQL Crash Course,
4148.47 -> or actually, it's a course about four hours long,
and it's absolutely free. So you can see the
4154.29 -> roadmap right here, and a cover the essentials
that you need to know, in order to be familiar
4160.23 -> with Postgres. And by far, actually, by far, both
price is the best database out there. So go ahead
4168.3 -> and check it out, go ahead and enroll. And it's
absolutely free. So if I go back to the diagram,
4173.49 -> so you can see that right here, we have this
data access layer, and instead of talking to a
4179.91 -> fake database, we're going to connect to a real
database, which will be Postgres. So for this
4187.08 -> database, what I'm going to be using his Docker,
so I'm going to use Docker and simply say Postgres
4193.26 -> and go ahead and click on this very first link
and Basically, if you don't know about Postgres
4202.53 -> and Docker or Docker, go ahead and check my video.
So I've got this video right here, where I cover
4209.61 -> about where I covered Docker and Postgres in about
10 minutes. So everything I'm going to teach you,
4216.66 -> you can find on this video. So if I close that,
and basically is really straightforward. So you
4224.85 -> can see that they have some description on what
is Postgres. And basically, this is how you start
4231.66 -> an instance, once you have Postgres installed.
So let me go ahead and simply grab this command
4239.73 -> right here. So let me just grab this command, and
then go ahead and open up your terminal iterm, or
4247.44 -> command line. So go ahead and simply open that up.
And what I'm going to do is simply paste that in.
4255.52 -> So actually, I didn't copy that. So let me just
grab this. And then paste that in, basically what
4264.43 -> we're doing here, so just let me explain so so
let's start from the very first command. So Docker
4270.94 -> run, so this is the Docker command. And then I'm
going to name this container as so let me simply
4278.77 -> rename this to, let's say, post grass, and then
dash, sprink. And then right here, so dash E is
4287.89 -> the environment variable. So we need that to set
the actual password. So let's simply keep things
4294.25 -> simple and simply have a very weak password. So
password, and then minus d, Postgres. And this is
4302.47 -> the actual image and go ahead and simply change
this to Postgres, and then Alpine. So this is
4309.76 -> the smallest version. And then what we need to do
is also expose a port. So we're going to take the
4316.66 -> port that is running inside of that Docker
container. So 5432, and then expose that
4322.84 -> to the outside world, as 5432. So basically, what
we're doing, and so basically, this minus D is in
4334.33 -> detached mode. So as soon as I press enter, then
we can carry on doing all the things. So go ahead
4340.51 -> and check my video if you don't quite understand
this, because I teach you all of this. So go ahead
4345.94 -> and simply press Enter. And there we go. So now
if I go ahead and simply say Docker, and then PS,
4352.66 -> you can see that we have this container right
here. And the name is Postgres spring. So go
4359.95 -> ahead and simply say Docker, and then port, and
then post, an Ingress dash spring. And you can
4369.04 -> see that we are exposing the container port to
the outside world, on localhost, and then 5432.
4377.83 -> And by the way, 5432. This is a default port for
Postgres. So now that we have a database, so we
4386.82 -> can log into it, and then perform various things.
So I want to show you exactly how to SSH into this
4393.18 -> box in a second. But for now, you have a container
running and exposing a port 15432. So now what we
4402.36 -> need to do is actually go and open up IntelliJ.
And let's actually connect to this database. So
4408.45 -> what I want to do is first revert this because I
want to connect to Postgres. And what we need to
4415.86 -> do is if I actually stopped that, and then close
that, so open up the resources. And by the way,
4423.78 -> I didn't mention that we have some tests right
here. So if I open up tests, so you can see that
4430.11 -> I even took some time to write some tests to
make sure that everything works. So let me go
4436.14 -> ahead and close this. And then what we're going to
do is open up this application dot properties. So
4443.91 -> application application dot properties, this is
where you define the connection details to your
4451.29 -> database. So go ahead and pretty much just delete
the application properties. So go ahead and delete
4459.87 -> application dot properties. And inside what I want
really is a file and simply call it application
4468.99 -> dot and then yamo wash that could have simply
renamed that to yamo. But it's fine. So okay,
4476.97 -> and not for now, I'm going to commit this in a
second so you can have everything on a branch.
4483.51 -> So now inside of this application dot yamo. This
is where we define all the connection details to
4491.1 -> our database. But before we do that, what we
need to do is actually add some dependencies
4496.95 -> that will allow us to connect To databases have
the drivers and then also give us the ability to
4506.7 -> write SQL commands against our database. So for
that, what we need to do is open up palm dot XML.
4514.26 -> And just let me collapse this for a second. And
you can see some information about this project,
4524.1 -> I'm using Java 11. The version of Spring Boot
is 2.13 release. And then you can see that we
4532.5 -> have some dependencies right here. So this is the
actual dependency for Spring Boot starter web. So
4539.04 -> this gives you the ability to have a Tomcat server
up and running and write some API's using Spring
4545.85 -> Boot MVC, so on and so forth. And then we have
this one right here. So Spring Boot starter test
4552 -> for writing tests. And then we have this set for
j, which is pretty much an assertion library for
4559.2 -> writing unit tests. So the dependencies that we
need are the following. So go ahead and simply add
4565.56 -> a new dependency. So I'm going to press Enter. And
you can see that now we can add a new dependency.
4571.74 -> So the first one that I need is from all dot
spring framework dot boot, so it's me grab that,
4579.51 -> and then paste that in. And then the artifact ID
that we need will be spring, and then dash and
4588.66 -> then boot, dash, and then Stata and then dash
JDBC. So let's actually duplicate this. And then
4602.22 -> what we need is org dot and then post and then
dress keywell. And then the artifact ID is simply
4612.18 -> PostgreSQL. So just let me grab that. So that will
be that. And then what we need is simply have a
4621.21 -> scope. And then this will be only at runtime.
And finally, we need a net of dependency. And
4628.53 -> this time, this will be org, dot and then fly way.
And I'm gonna explain this in a second. And then
4639.33 -> dB. And then what we need is simply fly. And then
way, and then core, wash the dash, and then core,
4650.4 -> just like that. So all of these dependencies will
allow us to connect to a database. And basically,
4658.98 -> this is what allows us to write, or actually it
gives us an awesome API that we can execute some
4666.81 -> statements against our database. This is for the
actual driver, so PostgreSQL. And in this flyway,
4674.7 -> db is for database migrations, and you will see
this in a second. So now let me simply go ahead
4680.94 -> and open up the project tab. And then if I open
up the demo application, and then press play, you
4696.25 -> should see that we have an error. And the error
simply says that failed to configure data source
4703.359 -> URL attribute is not specified, and no embedded
data source could be configured. So basically,
4710.62 -> what we need to do now is simply to create a data
source, and inject the URL, username and password.
4718.479 -> And then pretty much allow us to add an image that
will allow us to connect to our database. So let
4726.01 -> me go ahead and simply collapse this. And what we
need to do first is simply go ahead and create a
4732.609 -> new package. So package, and then simply call
it as data and then source. And then go ahead
4744.31 -> and press OK. And inside of this package, let's
go ahead and simply create a new class, and then
4753.04 -> simply say, post, grass, and then data and then
source. Press OK. And don't ask me again, I will
4764.53 -> add all of this later on. So this class will be
served as a configuration. So add configuration.
4771.91 -> And what we're going to do inside of this class
is simply return a data source, so public,
4780.189 -> and then it carry data source. And let's simply
go ahead and name it as the current data source.
4787.959 -> And what we're going to do here is simply return a
data source of builder. And inside let's simply go
4797.05 -> ahead and say dot and then create Then right here,
simply go ahead and say, and then simply go ahead
4803.8 -> and say dot and then type. And the type for this
will be e curry data source dot and then class,
4813.37 -> and then dot and then build. So you can see that
now we have this data source. And what we need
4821.859 -> to do is simply say add and then being. And what
we need to do also is say configuration and end
4828.76 -> our properties. And then what we're going to do
is simply have app dot and then data, and then
4838.33 -> source. So, basically, we are instantiating, this
Ikari data source as a bean. And then what we're
4846.73 -> going to do is create some configuration file
that will contain the username, URL, password,
4852.07 -> so on and so forth. So let's go ahead and simply
open up the application dot properties. And inside
4859.12 -> of application properties, what we need to do
is simply do the following. So let's go ahead
4865.06 -> and have an app and property. And inside of this
app property, we have data and then source. And
4873.52 -> then inside of data source, what we need is JDBC
dash URL. And this will be JDBC. Colon post grass
4885.46 -> qL colon forward slash localhost. And then the
port will be 5432. And then Ford slash, and then
4900.97 -> we need to connect to a database. So we're going
to create a database in a second. But for now,
4906.58 -> let's simply name this as spring and then boo. And
then dash and then post grass dashDB. long name,
4919.54 -> but you get the point. So now we need a username.
So the default username that ships with Postgres,
4926.11 -> it's called post press, we need the password.
So password. And remember, the password that
4932.98 -> we chose was password. And we can also define a
pool size. So basically, this is the number of
4942.94 -> connections that we can have actively, instead of
having to create a new connection to the database,
4950.02 -> every time that we want to execute a query,
every time we want to execute a query against our
4956.56 -> database. So simply go ahead and add 30. And that
should be it. So you can see that we have App Data
4964.439 -> Source. And inside of this data source package,
we have this Postgres data source. And basically,
4972.09 -> it's just grabbing all the dependencies from
this app, and then data source. So just let me
4980.1 -> go ahead and start this because I want you to see
the errors, and you see exactly what is going on.
4984.93 -> So if I start the application. There we go. So now
it says that cannot find migrations in location,
4998.07 -> class, path, db, and then migration. So what
we need to do is to create a folder called dB,
5007.37 -> and then another one called migrations and then
have our database migrations inside it. So what
5015.379 -> flow and this is basically flyweight, right? So
flyway allows us to manage our database schema
5022.28 -> as we evolve. So go ahead and pretty much
just learn about flyweight, right here. So
5028.97 -> flyway. And basically, you can read through the
documentation, but it is straightforward. So just
5037.459 -> follow along and you will understand exactly
what I'm doing. So if I close Oh, actually, I
5043.28 -> just have to keep that open. So if I go ahead and
simply close this or actually clear that and then
5052.49 -> I've got this terminal right here. So I'm going
to open this terminal. And if I do pwd, you can
5059.45 -> see that I'm inside of desktop Spring Boot YouTube
Crash Course. So what I'm going to do is simply CD
5066.86 -> into SRC, and then main and then inside of core
resources. So inside let me go ahead and make a
5076.25 -> folder. So make and then there. And what I need
is dB, CD into dB, and then make another folder.
5087.47 -> So make dir and then migrations. There we go.
Enter. And basically, you could simply come here
5104.75 -> and pretty much just right click, and a new, and
then directory, and then DB dot migrations. But
5111.59 -> I prefer to use terminal. And you should also get
used to it. So inside of dB migrations, let's go
5118.31 -> ahead and create a file. And this file will be as
follows. So version one, underscore, underscore,
5127.07 -> and then person, table dot SQL. So make sure
that this is as follows. Because otherwise he
5137.6 -> won't work. So version one, underscore, underscore
person, table dot SQL, enter. Now, let me ignore
5146.21 -> this extension. So this is where we can write
some SQL. So let's go ahead and simply create
5151.76 -> table person. And then inside of this person, we
have an ID. So Id, and this is of type EU ID. And
5162.02 -> then this will be not and then no, primary, and
then key. And then we also have, so if I open
5170.63 -> up the person class, we have the actual name. So
if I go back, so this will be name. And then this
5180.2 -> is a VAR, char. So let's go ahead and simply
add 100. Not Know. So this is enough. So now
5191.45 -> we have a table called person with the following
columns ID and Name. So what I need to do now is
5199.49 -> actually go ahead and create our database. So if
I open up the applications, or yamo, so remember,
5207.32 -> so right here, we have this database called Spring
Boot, Postgres dash dB. So you can get a better
5214.49 -> name. But you know, I'm just giving this name so
that you know exactly what we're doing. So what we
5220.7 -> need to do now is actually, so if I open up the
terminal, and this is where I've got, you know,
5228.86 -> I've typed some Docker commands. And you saw that
how we pretty much just launched this container,
5234.47 -> which is running and also exposing this port. So
5432. So this is the port that we are connecting,
5241.43 -> right, so localhost 5432. So let's actually go
ahead and bash into this container and create
5250.13 -> a database. So simply go ahead and say Docker.
And then exec, and then dash, and then it for
5259.82 -> interactive. And then what we want is actually,
if I grab this container ID, and then simply say,
5268.67 -> bin, forward slash, and then bash, Enter. And you
can see that now we are inside of that box, or
5276.71 -> actually. And you can see that now we are inside
of this container right here, which we can simply
5284.9 -> now run some commands. So go ahead and simply
say p SQL. And you can see that we have p SQL
5293.48 -> working. And now we can write some p SQL commands.
Again, if you have missed my course on p SQL,
5302.24 -> or actually Postgres, so on my website, so this is
where I teach everything that you see me typing.
5309.71 -> And to be honest, you should learn Postgres. It's
amazing. And a lot of companies are using it. And
5316.67 -> you can also use it for your personal project. So
let me go ahead and simply go back to my terminal,
5322.04 -> and simply go ahead and say p SQL, dash, and then
capital U, for post grass. So remember, so this is
5333.44 -> the actual username, and then enter. And there we
go. So you can see that now we are inside. So if I
5344.81 -> pretty much type backslash, and then l. So if I
clear the screen, backslash L, you can see that
5353.96 -> I do have three databases. So you can see a list
of databases, we have Postgres template zero and
5361.66 -> template one. So let's go ahead and create a new
database with this main. So let me grab this. And
5368.53 -> then if I tried to create So, caps, so create
table or actually database and then paste the
5380.95 -> name and that with semicolon, enter. Oh, okay. So
we cannot have dash. So let me simply pretty much
5394.12 -> rename this to demo dB. Right? So demo And then
dB, Enter. And you can see that worked backslash
5406.9 -> L. And we have demo DB right here. So let's go
back to IntelliJ. And let's simply rename this
5416.29 -> to demo dB. And now all we need to do is actually
connect to this database. So let me connect
5426.52 -> inside. So backslash, C, and then demo dB, Enter.
And you can see that I'm connected to demo dB. So
5435.31 -> if I do backslash, and then D, so to describe what
we have in here, so backslash, D, we haven't got
5443.74 -> anything backslash dt, you can see that there is
no relations. So these are everything. And then
5450.91 -> dt is for tables only. So now what I'm going to
do is go back to IntelliJ. And let's go ahead and
5460.33 -> simply start our application. So let's actually
go ahead and click on this icon right here,
5466.42 -> edit configurations. And let's go ahead and simply
say that we only want one single single instance
5473.77 -> running at a time. So if you press play multiple
times, it means that you will only get the same
5482.68 -> process and not get two processes. And then you
get like one error saying that the port is already
5489.16 -> in use, so we don't wander. So go ahead and simply
now play. And we have an error, and it says cannot
5499.65 -> find migrations in classpath DB migration. And
thus, because I've misspelled migration, I've
5509.34 -> typed migrations instead of migration. So just let
me rename that. So migration, and then refactor,
5516.63 -> there we go, and install the application. And
there we go. So you can see that everything
5525.39 -> started fine. And basically right here, you can
see that we have a curry pool started and then
5533.79 -> completed. And then we have database demo database
right here. And you can see that we have migrating
5544.32 -> schema. So person tables. So basically, that was
executed. So our command was executed correctly.
5550.32 -> And you can see that the migration was applied.
And now we have the server running on port 8080,
5558.78 -> which is awesome. So now, so if I pretty much go
back to my terminal. So I just want to show you
5567.15 -> something first. So remember, previously, we
type backslash, D, and backslash dt. And that
5573.45 -> wasn't working. Well actually, it was working,
but we didn't have any relations. So if I type
5579.06 -> the same command, so backslash d t, you can see
that now we have two tables. So this one is for
5588.81 -> flyway specific, which you shouldn't care at
this point. But you can see that we have this
5594.39 -> table right here called person. And if I go ahead
and describe a person, so backslash d ln person,
5602.37 -> you can see that we have this table called person
inside of the public schema. And then the ID is
5611.37 -> of type you will not know. And then we also have
name, and then it's a character varying. And then
5618.75 -> this is 100. So this is the length. And it's
also not no, and this is awesome. So if I go
5626.39 -> ahead and simply say select, and then start from
and then person and with semicolon. And you can
5635.63 -> see that we have zero rows. So let's go ahead and
simply insert someone. So because I know I need to
5644.24 -> install an extension to be able to to generate the
Jew IDs and also I cover this on my free course,
5653.39 -> I need to install an extension. So what I want to
do basically is this, so select and then you ID
5660.95 -> and then generate and then v4 as a function. And
you can see that no function matches. So what we
5672.771 -> need to do is create extension. And then this will
be u ID. Dash o s SP. So w ID dash o s SP and now
5688.85 -> it's semicolon. And you can see that worked. Now
if I pretty much select you with generate vision
5696.021 -> for and you can see that now we can generate you
It's so If I go ahead and simply say insert, into,
5702.71 -> and then person. And then we have ID, and a name,
and then values. And then we want to generate a u
5718.16 -> ID. So generate, and then v4, and then the actual
name. So let's simply say that this is Maria,
5728.12 -> and then Jones. And then end with parentheses,
and enter. You can see that Maria Jones does not
5742.64 -> exist. And last, because that has to be single
quotes, and not double quotes. So Marie Jones,
5749.57 -> like that, enter, and you can see that worked.
So let me go ahead and add Tom, and then Smith,
5758.371 -> enter. Now if I select star from person, you
can see that we have two people inside of our
5768.21 -> database. And this is awesome. So we've managed to
connect to our database using Spring Boot. And we
5777.51 -> have a database up and running with a table. And
now what we need to do really is just write some
5786.18 -> code to retrieve data from our database using
Spring Boot. Oh, actually, we're actually using
5793.41 -> Java and Spring Boot and their API basically. So
let me go ahead and collapse this, and inside of
5801.21 -> the person data access service. So let's go ahead
and write some code that will retrieve as everyone
5807.99 -> from our database. So what we need to do is to
use this class right here, so private, final,
5816.69 -> and then JDBC template. So what we're going to do
is add this to constructor, and then auto wire. So
5826.71 -> this is dependency injection, we don't have to
do anything spring, no Spring Boot knows how to
5832.95 -> initialize correctly this class for us, so that we
can write some code against our database. So now,
5841.5 -> instead of saying select all people, and then
return a list, so basically this fake list,
5849.391 -> what we're going to do is use JDBC template API.
So simply go ahead and say JDBC template dot,
5857.641 -> and you can see that they have various methods. So
the one that we want is query. So we want to query
5865.32 -> and the query takes two things. So we can pass
s, so we can pass an SQL statement. And then we
5875.371 -> have to pass a row mapper. So basically, the row
mapper is how we take the values that we retrieve
5882.601 -> from our database, and then transform that into a
Java object. So let me go ahead and simply simply
5892.52 -> define a string. So string, and let's go ahead
and make this final, not needed, but why not.
5899.51 -> So SQL equals to select. So basically, the same
thing that we had, so select name, or actually,
5908.93 -> this is ID, name. And then from and then person,
not the same. So previously, we simply said select
5918.44 -> star. So right here, so select star, so select,
and then Id, name from person. And this is the
5930.89 -> same thing. So I prefer to use the actual column
names instead of the instead of star. So now let's
5939.08 -> go ahead and simply pass the SQL query. So SQL,
and now we have to pass this row mapper. So this
5949.1 -> row mapper is a lambda that has access to the
result set to the result set, as well as the
5955.43 -> index. So what we're going to do inside here, so
let's simply go ahead and say return. And then
5965.33 -> we need to return a person object. So person,
so this is our model, and inside of our person,
5972.95 -> we need to pass what we need to pass the uod.
So they should be new person, just like that.
5981.53 -> So inside you can see that we have to pass a u
ID and also a name. So inside of the result set,
5988.67 -> we can get the string so get string, and then you
can see that we can pass the column label. So I
5996.95 -> know the column label is so basically ad. And also
we can get the actual name. So resolve set dot,
6006.91 -> and then get string. And then this will be
named. So if I put this this on a new line,
6012.97 -> and you can see exactly what we're doing. So right
here, you can see that we have an error. And this
6019.48 -> is because this result set Daguerre string
is a string basically. And what we need is
6025.87 -> a unit type. So let's simply go ahead you ID and
then from string. And then we need that. So that
6035.11 -> will work. So you can see that now this works,
let's end with semicolon. And what I like to do
6043.24 -> is actually to extract this way variable. So all
of that actually. And then this will be ID. And
6050.2 -> then this will be name, so you see exactly what's
happening. And we can remove that. And also that,
6060.43 -> and then that and then format everything. So
now I don't need to return the list of pretty
6069.94 -> much just fake people. So this from Postgres
DB is not named, and we are retrieving from
6076.48 -> a database. So you can see that I still have an
error. And this returns a list of people, right.
6085.75 -> So you can see that it's a list of people. So I
can either say return, and then people whoops,
6095.14 -> so people just like that. Or I can simply return
that in line, so refactor. And there you go. So
6105.7 -> you can see that I'm returning that in line,
which is awesome. So this is it. So if I now
6113.74 -> restart the application. So let me go ahead and
simply say stop, and we run. There we go. It's
6124.06 -> up and running. And if I open up Chrome, and
then remember, so right here, we previously add
6133.27 -> an empty list. And this is because he was coming
from our fake database. So now, if I refresh this,
6142.27 -> there we go. So you can see that now, this is
coming from our database. And this is awesome,
6150.1 -> guys. So you can see the power of Spring Boot.
Basically, you can just write code, you know very
6157.27 -> fast, and they give you these tools so that you
can focus on writing what matters instead of you
6163.03 -> having to configure things. So there we go. So let
me go ahead and simply add someone else into this
6170.59 -> table. So let's go ahead and simply, say, Tomcat,
we're actually not Tomcat. Let's simply say, Anna,
6181.3 -> and then Jamil and an editor. So if I refresh
this, you can see that now we have Anna Jamil.
6194.35 -> So there we go. If I collapse this, you can see
that this is only one implementation of our API,
6205.9 -> right? So we have a bunch of other methods. So
select person by ID, you actually let's go ahead
6211.69 -> and implement this one quickly. So you see exactly
how this is done. So what you want is to pass,
6218.89 -> or actually select ID name from person. And then
here you can see where in an ID equals to question
6229.27 -> mark. And then what you want is to take the exact
same thing here, and then simply return that back
6238.06 -> instead of query because query it returns you a
list, what you want is query for and an object.
6245.83 -> And query for object takes the SQL. But we also
need to pass the arguments. So new object, so this
6256.48 -> must be an array. So just like that, and you can
see that So currently, this is empty, right? But I
6266.56 -> need to pass this ID that comes from the client.
So Id and then this, let's rename this because
6274.75 -> it's already in scope. So person, and then ID. And
let's rename that to person ID as well. Just like
6282.79 -> that, and let's actually change this. So this will
be a person, right? But what we want really is to
6293.03 -> return so return optional.of nullable, because
that can be no Then pass person, just like that.
6302.54 -> So basically, it's the same thing. But we have
this question mark, and then query for object,
6310.04 -> we pass the ID right here. So if I put this on a
new line, so you can see exactly what we're doing.
6316.16 -> So in that, that, so we passed the SQL, the ID.
And basically, if you had more item arguments,
6325.76 -> you'd simply pass him right here. So we have the
ID, and then this is the row mapper, as you saw
6331.43 -> before, so right here. And then we simply return
the optional of nullable of person, because,
6337.55 -> you know, we might not find a student with a
given ID. So if I restart this, and then open
6349.31 -> up Chrome. So if I refresh, you can see that this
still works. But you find out take this ID, and
6358.34 -> then pretty much to Ford slash and then Id remove
the quotes there, Enter. And you can see that now
6368.27 -> we only get one person. And this is awesome. So
there we go. So you can see that we managed to
6376.73 -> connect to a database using Docker, Postgres,
flyway, spring JDBC. And you can see that this
6386.66 -> was pretty much, you know, very straightforward.
And we even managed to use a carry. So I didn't
6391.46 -> even explain what you carry was, but you carry is
the actual recommended data. So if I you should be
6398.51 -> using for a Spring Boot, because it's very fast,
reliable. And yeah, it's just one of the best
6405.71 -> ones out there. So there we go, guys, if you have
any questions on this, and basically, I'm gonna
6413.51 -> let you implement the rest of the logic. So insert
person, the same with delete, and then update. But
6423.27 -> basically, it is straightforward using some SQL.
So go ahead and pretty much just subscribe to my
6431.1 -> channel to get more videos like this, if you want
to learn more about all of this and more. So I've
6437.52 -> got a Spring Boot full stack, web development
course where I teach all of this and a lot
6444.36 -> more. And also creating user interface for this
using react, reacts and hooks. And pretty much,
6452.22 -> it's a lot of content packed into one course where
I'm pretty sure that once you learn that you will
6460.08 -> be able to build applications on your own. And
also apply for jobs because a lot of companies
6466.56 -> are looking for a Spring Boot developers or, you
know, Java developers. But Spring Boot is used
6473.49 -> quite a lot in the industry. And once you know
all of these skills, so dependency injection,
6479.22 -> connecting to databases, writing tests, writing
integration tests, writing user interface for it,
6486.3 -> I'm pretty sure that you will be able to get a
job. Okie dokie. As you saw throughout this crash
6494.31 -> course, springboard is amazing. So we really just
scratched the surface. And the next step for you
6502.05 -> is to check out my full stack web development,
Spring Boot and react. And in that course,
6509.91 -> I'm going to teach you everything you need
to know about Spring Boot. So we're going to
6514.44 -> cover essential topics that you need to know as
a software developer. And the cool thing about
6520.38 -> this course, is that whatever you learn, you
will be deploying it into a real server using
6527.49 -> Amazon Elastic Beanstalk. And upon completion,
the skills acquired in this course, it will help
6535.29 -> you apply for jobs because it will help you with
a lot of great important concepts that a software
6541.74 -> engineer should know. Also, you can start your
own projects, university projects, and pretty
6547.98 -> much if you are a developer, you should be aware
of this awesome framework and how to use it inside
6555.12 -> out. This is all for now. Go ahead and check out
my YouTube channel where I've got more content
6562.29 -> similar to this. It was a pleasure teaching
you and I'll catch you in the next one. See ya
Source: https://www.youtube.com/watch?v=vtPkZShrvXQ