This content is an introductory course on Spring Boot, a popular Java framework for building web applications and APIs. It guides learners through setting up a Spring Boot project, building a multi-layered API (controller, service, repository), connecting to a PostgreSQL database, and implementing CRUD operations (Create, Read, Update, Delete).
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
what's going on guys assalamualaikum
welcome to amigos code
in this course i'm going to teach you
everything you need to know about springboom
springboom
springbo is by far one of the most
popular frameworks
and i've asked what people want to see
and they said that they wanted to see
a course specific on spring boot and
also some
some other tutorials so this
first course is basically the introduction
introduction
on how this framework does work and
we're going to build an
api where we're going to have three layers
layers
and also we're going to connect to a
real database and you'll see
um you know the power of this amazing framework
framework
if you're new to my channel go ahead and
subscribe also give me a thumbs up so i
can keep on recording these courses
and um yeah so uh if you're not part of
the amigo squad community go ahead and
join because the community is growing
and i would love you to have you there
without further ado let's kick off
right before we proceed and i just want
to say that
if you want to take this course at your
own pace
and receive a certificate this course is available
available
on my website for free yes for
free you can enroll and you will get a certificate
certificate
also practice as i teach because that's
the best way of you
learning any new framework or
programming language
so i don't want i don't want to waste
your time i want to add value so
practice as i teach
also join both the private facebook
group as well as discord
to engage with other students if you
have any questions you can ask questions there
there
and engage uh yeah basically we're just
building an
awesome community where people are
learning from each other
and yeah you should be part of it
and finally if you need to have access
to the entire source code at the end of
this course i'm going to provide you
with the github link
where you can find all of the examples
without further ado let's kick off this course
springboo is an amazing framework for
building applications
whether you want to build back-end
applications or full-stack applications
using java
or kotlin spring boot is the framework
to go
springboard is by far the most popular
framework right now
and you by learning springbook you will
be in demand for jobs
everybody uses spring boot and you
should also
if you need to my channel go ahead and
subscribe give me a thumbs up and also
go ahead and join the both
private facebook group as well as discord
discord
so as i said springboard is an amazing
framework that
gives you everything you need in order
to build applications
if you need security you can use
security modules available
if you need logging you can use the
login integration connecting to databases
databases
whether you want to connect to mongodb
postgres mysql
they made it super easy for you as a
developer to connect to any
database metrics so checking how
your application is behaving in
production and
the cool thing is is that it's very easy
for you to learn as a beginner as you'll
see in this course
also it's production ready you can build microservices
microservices
has dependence injection built-in configuration
configuration
great community and a bunch more
so i'm super excited for this course
next let's go ahead and learn how to
so for this course we're going to build
this entire application
excluding the front-end part where we're
going to have
the api right here so the api will receive
receive
get request post put and delete
and then we'll have a service layer so
this is mainly for
business logic and then we'll have a
data access layer
and this layer right here is responsible
for connecting to
any database so in this course we're
going to use a
real database and you'll see how easy
it is for you to implement all of this
now in order for us to get started with
spring boot let's go ahead and navigate to
to
spring initializer so i'm going to leave
the link in the description
of this video where you can follow along
so in this website right here this is
where we can bootstrap
any given springboard application so
here you can see that we can pick from
maven or gradle so i'm going to leave
maven and in the languages
java kotlin or groovy i'm going to stick
with java
and then pick any version so literally
any version
i'm going to leave the 2.4.1 this might
be different by the time you watch this video
video
but everything i'm going to show you
today will work in later versions
then right here you can see that we can
customize the
project metadata so i'm just going to
leave everything
as default and for packaging
go ahead and select jar so this is the
most common
common packaging type for java
application these days
and then for java version i've got the
15 installed
so i'm just going to select that and now
let me scroll up
and right here let's go ahead and select dependencies
dependencies
in here we can pick dependencies that
our project
needs so this list is quite
huge and by all means feel free to explore
explore
this list but for this course let's go
ahead and
select the spring web so build web
including restful applications
using spring mvc so right here i'm going
to press
command and then b because i want to
select multiple
dependencies right here so i've selected that
that
so they've got a bunch of things spring
security if you want
so we're gonna leave that out for this video
video
and right here go ahead and select
spring data
jpa so we're going to connect to a
database by using
jpa and hibernate and you'll see this in
a second
let's also scroll down and right here
we can use an in memory database if we
want to
or if you want to connect to a real database
database
such as postgres you can also do it for
this course let's connect to a postgres database
database
so here let's select the postgresql driver
driver
so i've just pressed command there again and
and
you can see that you can connect to
or actually you can
work with spring data reactive
elastic search and basically the list
goes on and on and on rabbit mq web sockets
sockets
kafka and spring batch
java mail and you can see that the list
is just crazy including graphite and prometheus
prometheus
so now that i have selected
everything what i'm going to do is i'm
going to escape out of this so press
escape there we go and also what i'm
going to do is actually
share this with you so you can pretty much
much
just download the exact same
dependencies from the link
of this video now if i open up my desktop
and there we go so you can see that we
have the demo.zip
and this already has been extracted
next let's go ahead and open this
for this course i'm going to be using
intellij which is by far
my favorite ide and the one that i
recommend for
most developers so i've got the ultimate
edition right here but if you have the
community edition
this will also work the reason why i
love the
ultimate edition is because it has lots
of great features
when working with frameworks such as
springbook and also when working with databases
databases
so i highly recommend you to give it a try
try
so i'm going to open up the ultimate
edition and by the way i'm using the
jetbrains toolbox so this tool right here
here
allows you to download and manage all of
the ides
and there we go so now i'm going to
and then navigate to desktop
and then in here open up demo and
open this folder so from this point it
should pick up
everything needed for this project
including downloading all of the dependencies
dependencies
just give it a second and there we go so
you can see that that was super quick
now what i'm going to do is to open up
the demo folder
and then open up the palm.xml so in here
you can see that we have the project
metadata here
and then the dependencies so we selected spring
spring
boot starter data jpa and then
start a web as well right here and
the dependency for postgres is right here
here
so this is the so this is what gives us
the ability to connect and work with
postgres databases and right here we have
have
this dependency given to us by default
and this is for
testing and to be honest this
is all right here you can see the java
version 15 that i've selected
and this is pretty much the palm.xml
and also you can see that we have the parent
parent
dependency right here which is springbootstarterparent
springbootstarterparent
there you have it you've successfully
created an empty
springboard application next let's go
ahead and start the application
to start the application let's go ahead
and close
this palm.xml and in here
in this demo folder right here
so we have source and inside of source
we have
main and then
test so this is where all of the test code
code
goes so let's open up dem application tests
tests
so you see that we have one empty test
right here
so this is where you put all of your
testing code
if you want to learn more about testing
go ahead and check my website where i've
got a full course
covering unit testing integration testing
testing
mocking and tdd now let me close
this and if i open up the java
folder right here so here this is where
we put our code
but more importantly we have this demo application
application
so if i open that up so this is a
bare bone springboard application and
you can see the annotation right here
now inside of java
so inside of actually main we have resources
resources
and static template and application.properties
application.properties
so this application.properties this is
where we configure
all the properties for our application
as well as
environment specific properties and
you'll see this in a second when we
connect to a real
database then we have static so
static and templates this is when you
are doing web development
such as working with html css javascript
and all of that good stuff
now open up the demo application so
let's actually run
the application and right here you
should see
this play button or you can right click
and then run them application or you can
run the application from here
so let me just run the application here
and just give a second so just to let
you know that this
should fail because it will try to
connect to a database
right here you see that fail to
determine suitable driver
right here so what i'm going to do here
is the following i'm going to open up
the pom.xml
and where we have the springboot starter
data jpa
let's go ahead and comment this
dependency for now
there we go and what i'm going to do is
right click on the palm.xml
and then maven and then reload project
so sometimes it might not reload automatically
automatically
but this makes sure that it's actually removing
removing
this dependency from here now let's go
ahead and start the application
and there we go you can see that no
errors so bear in mind that we will put
this dependency bank once we are ready
to connect to our
database so here you can see that
we have the logs tomcat started on
48080 right here which means that we
have a web server up and running
and if we try to hit our web server on this
this
port we will get nothing because we
haven't implemented any endpoints
next let me go ahead and show you how
easy it is for you to
so far we have the server up and running on
on
48080 so what i want you to do is to
navigate to your web browser and then type
type
localhost column and then
8080 enter and
you can see that we do get a white label
error page
and right here the status is 404 which
means that it's not found
so let's go ahead and quickly implement
an endpoint that will give us a
json back so here let me go back to intellij
intellij
let me stop the server and here i'm
going to hide
this i'm going to close palm.xml
and remember this dependency right here
we will add it back
once we are ready to connect to our database
database
so close that and application.properties
as well
so here what i'm going to do is right within
within
this demo application class go ahead and
type add
and then rest and then controller
now let's go ahead and have a class so here
here
public or actually a method so public
and then say string and then hello
and right here what we're going to do is
simply return
hello and then world just like this
now in order for this method to be
served as a
restful endpoint we have to annotate it
with at and then we have
get mapping we have put mapping post
mapping so on and so forth
and you will learn all of this in a
second for now we want to
get something out from our server so here
here
at get and then mapping
and this annotation makes this class
as a restful
makes this class to serve rest endpoints
and the only rest endpoint that we have
in here is
this one string hello and
returns hello world now
there we go server up and running let me
open up my web browser
and before we had a 404 now if i refresh
you can see that we have hello world in here
here
and what's really interesting in here is
that if i change this to
list so i'm going to change this to a list
list
of and then string in here
and here i'm going to say list dot and
then of
as one element and then world as another element
element
just like that and if i restart the server
so stop and rerun there we go now if i
open up my web browser again and then
reload the page
you can see that we have a json back so hello
hello
world so this is actually really nice
there we go so you can see that we have
hello world
and basically we didn't do anything
and we get a json array out of the box so
so
this is really awesome so obviously
we don't want this simple api
where we have hello world but we want to
build all of this
so what we're going to do next is the
following we're going to create a class
right here we're going to model it as a student
student
and then we're going to give it some
attributes and behaviors
and then ultimately students will end up in
in
a database but for now let's just begin
with a simple class that will represent
a student
and then implement the student
so let's take this student right here
represent it in a class so that we can
start building
this entire api so let me go ahead and
open up intellij
and the first thing i'm going to do here
is to create a package
so here i'm going to create package
and i'm going to name this as student
so you will also learn how to structure
your applications
using springbook so inside of this
package now
we have to pull every code which is
student related
so here we need to have the student class
class
so so so this will be our model
right here so public class student
and now let's go ahead and define private
private
and then we want long for id we also want
want
private and then string
name so here name
age date of birth and h so here
private and then integer
h and we actually need
private and then local
and then date and this will be
dob for date of birth
and finally i've almost forgot so let's have
have
an email so string and then email
just like that and looking into
this diagram this should be email
actually let's change this to email
right here
and we are good to go so
now let's generate the getters and
setters and constructors
so here let's go ahead and generate the constructor
constructor
so constructor and i'm going to have
three constructors
so i'm gonna have the no r constructor
i'm also going to have another one with
everything there we go
and let me put this on a new line just
like that
so the constructor with everything
let me also have another one without the id
id
and you'll see why in a second because
the database will generate the id for us
there we go and new line
just like that and finally let's
generate the
getters and setters and obviously we
could use something like
lombok to remove all of this boilerplate code
code
there we go and finally
tostring so to string
and then ok and there you have it
so now we have a class called student
so what i'm going to do now is open up
demo application
and instead of a list of string i'm
going to say list
and then in here what i'm going to do is
on a new line i'm going to delete that
and let's pass the id of 1
and then name mariam
and this has to be one l because it's long
there we go and here let's have
mariam dot and then jamal at
gmail.com comma
and we also need to pass the date of birth
birth
so local and then date
and then dot and then of and if i put
this full screen there we go and here
let's pass the first wall here so let's
say that
she was born in 2000 and then
the month so let's say that
she's from january
and then the day let's say the 5th of january
january
just like that and then comma and then
here let's just simply say 21.
we could actually subtract this so subtract
subtract
now minus the date of birth but we'll do
that in a second
so now let's go ahead and start the server
server
and by the way you see that we have id column
column
name column email column don't add these
because these are added by intellij
again don't add these so if i open up my
web browser now
and then here remember before we had
this list
if i reload the page you can see that now
now
we have an array of and then
objects so our class was converted
into a json object right here and we
have an array
surrounded by it so this
it's really awesome you can see how easy
it is for us to start building
right so we have this class right here
and we also expose an endpoint that
returns an array of students
now is the perfect time for us to get things
things
done in the proper way so we want to
structure our application
into this entire architecture where we
have the api layer
service layer and then data access
so we already have the class student
let's go ahead and create a class
that will serve as the api layer
so open up intellij and right here i'm
going to collapse this
expand project and inside of student
so everything will be inside of student
so create a new class
and here call it student and then controller
controller
there we go so now we have this class
which will have all of the
resources for our api so
here remember before if i open up demo application
application
we had this annotation right here so
rest controller so
so what we're going to do is remove it
from here
and let's also take this
get mapping so i'm going to cut this
so this class right here now
should be empty right here so empty class
class
and let me remove all of those imports
just like that
and you can see the keyboard shortcut
now let's go
back to our student controller right here
here
and here we're going to annotate with
add and then
rest controller and then what we also want
want
is to say that this will have a request
and then mapping in here and
we can pass a couple things so one is
the path
here and the path will be api
and then v1 so version one of our api
forward slash and then student so
instead of going to
localhost 8080 we now want localhost 8080
8080
api for slash v1 and then student
let's also paste the get mapping that we
had before
here so put this full screen
but now instead of hello i'm going to say
say
get and then students so now we have a
get mapping for our student controller
so let's start the application there we go
go
if i open up my web browser and right
here remember before we had
localhost 8080 so if i try and hit this
endpoint this should not work
but now we've changed it to forward slash
slash
api forward slash
v1 and then forward slash student
enter and check this out so you see that
we have
the api working but now things are much more
more
organized next let's go ahead and create
a class that will serve
as the business logic for managing students
right so we have our api layer right here
here
now the api layer should talk to the
service layer
to get some data and that service layer
should also
talk to the data access layer to get the data
data
so that it does a round trip like this
right from the client api
service data and then all the way back
so now let's move
this so this method here to the
service layer so service layer is mainly
responsible for
business logic so here
open up the project tab and then here
we're going to create a new class
and then call it student
and then service there we go and
what we're gonna do is so here we're
going to
take this method
so let's take this method
and then put it inside of this service
so just like that and now we can go back
to controller
and here so this will no longer be
like that and instead we have to use
the method inside of our student service
so here let's have a reference so private
private
and then final student
and then service student service
just like that and let's add it to the constructor
so let me put this full screen there we go
go
and now i can say return and then
student service
dot get students so
this now is a much better approach and
we are using
the anterior design pattern
so if i go back to project
or actually open a project and then
student service
you can see that now it's being used
but ideally so this currently is a static
static
list we also want this to come from a database
database
but we're going to worry about that in a second
second
next let me go ahead and teach you about
some annotations
and dependency injection within spring
and springbook
all right so far we have three classes
and within the student controller in here
here
so let me just put this like this you
can see that
we are having a reference to student service
service
and then here we pass it inside of the controller
controller
now this is not going to work because
we don't have an instance of student service
service
now the way that this would work is if i
was to say
equals to new and then student service
just like that and now this will work
right but when writing code we should
avoid stuff like this
and use dependency injection as much as possible
possible
so how do we tell that this
student service should be injected into this
this
constructor right here or anything that
we pass into this constructor
should be injected well we have this
annotation called
at and then auto and then wired
now we are saying that this student service
service
should be auto wired for us so
this will be magically instantiated for us
us
and then injected into this constructor
and then all of this will work but now
we have to also tell that
this student service is a class
that has to be instantiated i.e
it has to be a spring bean
so to tell that this
is a bean we can use this class or actually
actually
we can use this annotation called add
and then component
now if i go back to student controller
you can see that this red error went away
away
because now it knows where to find this
being right here so if i go back once more
more
you see that i'm using add component but
with spring
we have annotations that allows us to be
more specific
so here we don't want this to be just a
regular component but we want this to be a
a
service so at service at component they
are the exact same thing
but this is more for semantics
right this is more for readability
looking at this you know that
this class is meant to be a service class
class
so the same way that you saw in here for
this controller we use the
add and then rest controller for the
service we use
at and then service
and then go back to my web browser
and here i'm going to refresh this and
you can see that
still works so
now we are using dependency injection
and we've actually split
things into layers so we have the api
layer talking successfully to the
service layer
and the service layer is giving back some
some
data back to the api layer
next let's focus on the data access
by connecting to a real database
and then store the student inside of the database
database
and then get it back out if you have any questions
questions
go ahead and let me know otherwise let's
all right now let's go ahead and connect
to a
real database so we've got the api
layer service layer now let's focus on
this data access layer
for this course
let me go ahead and steal some code from
my spring data jpa course
and in this course you'll learn
everything that you need to know
about spring data jpa so we cover
uh what is bring data to apa how to how
to connect to real databases how to map
classes to tables
the entity life cycle queries paging salty
salty
one-to-one relationships one-to-many
many-to-many transactions and a lot more
so in here what i'm going to do is i'm
going to go inside of
src and then resources
and then application.properties and i'm
going to grab
all of this so right here again i'm
going to leave
this in the description of this video so
you can follow along
now i'm going to go back to intellij and
i'm going to open up the applications.property
applications.property
or properties and then paste that in
there we go
so this is the configuration that we need
need
in order to connect to a database
so this is the url this is the port
and in here that and then in here this
is the
database so here let's call it student
instead of amigos code and then we have the
the
username and password so because i'm on
a mac
and i'm using postgres app so this for
local development
is empty and then have the
jpa create drop so this means that we
have a clean state every time we run the application
application
and then show the sql we also want the
dialet to be
postgres and then format the sql so
you'll see all of this in action in a second
second
now the easiest way for you to download
and install
postgres on a mac is by installing
this application called postgres app
so it's really straightforward and
allows you to connect
and experiment with different various
versions of postgres the default port
is so in here is 5432
if you are on windows go ahead and download
download
from enterprise db.com and i'll leave
the link in description of this video
and download for windows version right here
here
and you can download the latest version
13.1 as i speak
also i'm going to leave a link for videos
videos
that i've done in the past on how to
install the database
if you have any troubles but if you want
to learn more about databases
go ahead and check amigosco.com and if i
go to courses
so in here spring data jpa so here
this course covers everything you need
to know in order to work
with databases so i'm going to leave a
link for
this course as well so once you have
postgres installed
we have to create a database and that's
to connect from our application to our
database we have to do the following
so in here remember that we've changed this
this
to students so this is the database name
right here so i already have a database
up and running
and again if you want to learn how to
work with databases
go ahead and check my website so this is
the spring data jpa
but i also have a free course just on postgres
postgres
including the advanced databases and
database design and implementation where
you will
master everything there is to know about databases
databases
so in here what i'm going to do is
i'm going to open up my shell and i'm
going to type
psql and if i do a backslash
l you can see that we have a couple of
databases in here
but what i want really is if i press
ctrl l
is to create a brand new database so create
create
a database and then call it
student and then semicolon
and there we go so now you can see that
we have a database that we can use
now if i type backslash ndu
you can see that i do have two roles so
amigos code and postgres
so in here what i'm going to do is say grant
grant
and then all privileges
on and then the database that i've just created
created
student and then two
amigos code there we go
semicolon and this should be
database so on and then database
there we go enter and let's also
just like that now if i clear the screen
you can see that we have this database
right here
called student so if i connect to it
and then student
and then backslash d and no relations
so now let's open up intellij and
before remember we commented out this
dependency so i'm just going to uncomment
uncomment
just like that and then i'm going to
reload the project
so click on palm right click
and then maven and then reload project
there we go and now i can start the application
and there we go so we actually connected
to our database right here you can see
the logs
so we have ikari started and then completed
completed
using postgres right here and
everything is looking good but we don't
have any tables yet in our database
and that's what i'm going to show you
alright so we successfully connected to
our database
now what we want to do is to take this
student right here and use spring data
jpa to create a table
inside of our database that we can then
add delete and basically perform all of
the cred
operations against our database
so to do that is very straightforward
with spring
boot and spring data jpa so in here
so this dependency right here that we've
just uncommented
gives us this ability and if you want to
learn about spring data jpa
go ahead and check my course on spring
daily jpa where i teach
all of this so now let me go back
in here and open up student class
and to map this student to our database
simply type at and then entity
right here and then say at
and then table so this one is for
hibernate and this one is for the
table in our database and we also need
to say add
and then id and then here we're going to
generate a sequence so sequence
and then generator and here i'm going to
say name equals to
and then student and then sequence
and you'll see this in a second we also
need to have the sequence name
which will be the same and also the
allocation size
so our location size
this will be one and then here
this will be sequence and then
name just like that and
now that we have a sequence generator in
here i'm also
going to now say add and then generated value
value
and then the default or the recommended
for postgres is a sequence so i'm going
to say strategy
equals two and then sequence
there we go and the generator
is the sequence that we've just created student
student
underscore and then sequence and
there you have it so literally this is
everything that we have to do
so now that we've mapped this
student class to a table in our database
next let's go ahead and start the
application and see
right let's go ahead and test these
changes that we've done
using spring data jpa
using these annotations at entity at table
table
and then the id with a sequence as well
so let's go ahead now and restart the application
application
and right here let me put this a little
so you can see that we have some logging
going on and there we go
so have a look so in here we have some logging
logging
and we have create sequence student
starting at one increment one
so basically this sequence is this one
so sequence generator and allocation size
size
right here so increment of one
so that's what it means and then we have a
a
create table statement
in here have a look create table and then
then
student and then all of these fields
and then the primary key is id
so this id right here and you can see that
that
this was super easy right we've just
added some annotations and now we have
a table in our database so if i
collapse this and in here what i'm going
to do
is i'm going to connect from intellij
to my database so new data source
so i've just pressed here database
and a new data source
and then postgresql
and localhost fine database
this will be student test the connection
you can see that it worked apply okay
and now we have one database
inside we have the public schema and
inside we have
the table right here student and you can
see the columns
and then we have the sequence student sequence
sequence
so if i open up my shell so
in here remember before when i type backslash
backslash
d we had no relations now if i type
backslash d again
there we go so you can see that we have
this table
student and the sequence so let me now
describe the student and you can see
that we get the exact same thing
in here so we have the index for the
primary key
so this is pretty nice
so what we are missing now is
this part right here so the data access layer
layer
we have the database configured up and
running with
one table called student next
let's go ahead and implement this
layer right here that will interact
right so you saw the power of how we
took a class
and then we mapped that class to a table
in our
database so if you want to learn more about
about
databases and spring daily jpa i've got
a series of courses focusing
just on databases starting from the
ground up so
um sql and postgresql so
um those are the funday those those are
the foundations that
you should know and then i'm going to
take you through
how to design databases how to use erd diagram
diagram
relationships and constraints
transactions and all of that stuff
and then i've got the advanced
database course and that basically
you know brings everything together and
then the fourth course
is about spring data jpa so you can see that
that
basically it's a it's a series of
courses focusing
just on databases and if you know spring
data jpa well enough
then you can build you know complex
applications with complex relationships
um you know understanding for example
indexes uh unique constraints
uh relationships one too many many to one
one
so basically the whole thing it's
covered uh in my spring daily jpa course
so i'm gonna leave a link in the
description of this video so you can go
and check the course for yourself
and yeah and i would love to have you
there so
um yeah i just wanted to say this and
all right let's go ahead and implement
the data access layer
so open up intellij and right here
i'm going to create a class inside of the
the
student package and this class i'm going to
to
call it student
and then repository
so this is the naming convention for
anything that access
your database and specifically when you
are working with
jpa which we are so now
this has to be an interface and you'll
see the power of this in a second
enter there we go so we have the
interface there
and let me collapse that now also let me
press command n1
just like that now this
interface right here i'm going to say extends
extends
and then jpa and then repository right there
there
now you saw that this actually is using
some generics and we have to specify a
couple things
so first we have to specify t
so the type of object that we want this
repository to work
with and also the id for
the type that we want so here let's go
ahead and say
student so this is the type that we want
to work with
comma and let me put this just like that
so we can see everything
and here remember the
id for our student so the type is
long so if i open up student
you can see that the type is long in here
here
that's why this is long and
this is the type that we want this repository
repository
to work upon now let's annotate
this with add oops not backslash two but add
add
and then repository so the same way that
we have add service
at rest controller this is at repository
because this interface is responsible for
for
data access and to be honest
this is everything for this layer
now let's connect things using
dependency injection
so in here open a project and we want to use
use
this interface inside of our service
so in here let me collapse this
so instead of having a list like
this so static list we want to say
private final and then student
repository there we go
then we want to add this to constructor
and then let's also annotate
the constructor with add and then auto wired
wired
just like that and now check this out
instead of saying that we want to return
this list what we're going to say is so
let me actually
um just copy this student because i'm
going to need it in a second
but now i'm going to say in here
repository or actually return and then repository
repository
dot and then check this out so we have a
bunch of methods here we have find all
we can pass the sorting if you want by id
id
we can save and we can flash we can count
count
exists find one save all
delete all and you can see that we have
a bunch of methods
and to be fair we haven't implemented
none of this
right and this is the magic of spring
data jpa
so here i'm going to say find and then all
all
so this returns a list to us
and to be honest this is it so
this interface that we've just
implemented so student repository
this so if i navigate to this file
you can see that it's an interface that
has all of these methods here
available to us we haven't written a
single line of sql
or code and we can fetch
students from our database
next let's go ahead and add some
students into our database
using this repository and then see that
everything will be working so
api to service data access
fetching from the database and then
returning back the response to our client
alright so let's go ahead and start our server
server
there we go so server up and running and
if i open up my web browser
so remember here before we had one student
student
miriam and now if i refresh this
you can see that we have an empty
array and this is because there is no
one in our
database and in fact to prove that to you
you
so in here what i'm going to do is i'm
going to say select
oops select star
semicolon enter and have a look
0 rows so let's go ahead and add some students
students
into our database so let's go back to
intellij and right here
what i'm going to do is i'm going to
create a new class and i'm going to name
it as student and then
config and
let's annotate this with add and then configuration
configuration
just like that so this will be a
configuration file
and in here we want to have a bean
so we want to have this bean right here so
so
line runner and
this guy will take nothing inside there
but here we want to return args
and for now we're not going to do
anything with arguments
and here let's annotate this with add
and then bean so that this runs
and what we're going to do is to have
access to
our repository so here we can inject
student repository just like that and
i'm going to name it repository
and from this point onwards what we can do
do
is remember we had a student before so
if i press command v
so this is the student that we had
before and
let me add semicolon there and then
import this and local data as well
and in here what i'm going to do is
add a static import just like that
so now this is our student i'm going to
press command v
or actually command shift v and this is mariam
mariam
let's also create another
student so in here
and this time what i want to do is i
want to remove the id
right because this will be generated for
us by the database
and this will be alex
so alex and this will be
alex gmail.com for example
and alex was born in 2004 for example
right and don't worry about the age
we're going to
sort this in a second and now we have
two students
and we want to save it into our database
so here
the way we save to our database is as follows
follows
we invoke our repository dot
and then we can say save and have a look
we can save one student or we can pass a
list of students
so here we want to save all and this
takes a list
just like that and i'm going to pass miriam
miriam
and alex just like that
and then here and this with semicolon and
and
and check this out so you can see that
now we have
a bit more logging and here we have
insert into student
these columns and then these values
and then we have another insert so here
into student and then these values so basically
basically
we can see the sql that hibernate is
running when we invoke save all
so now let me go back to my web browser
and remember before we had an empty list
so if i refresh this check this out
so we have two students coming from our database
database
and here if i select start from student
there we go this is from our database
and even within intellij
so in here i can open the console
say select basically the exact same
thing so here let me just
be a little bit lazy and copy this
command here
there we go go back paste that in
and you can see it's a bit more slow
but you can see that the results are
here as well and
cool thing here is i can view as and
then transpose
and check this out so we have the
results here
so this is mariam and this is alex
so to see the power of sprinkle and
spring data to apa
already it allows you to build software
really fast and so far
we have everything working so we have
the get request
that gives us a list of students and
upon a client sending a request
we have the service layer for business
logic the data access
we have the database i've been running
and everything is configured
beautifully now all we have to do is to implement
implement
these three other http methods so post
for saving
new resources put for updating the lead for
for
the leading so if you have any questions
drop me a message
right before we implement the post
method in here that will allows us to
save new students into our
system what i want to do is take care of h
h
basically i don't want age to be stored
in our database
because we can calculate that based of
the date of birth
so to do that let's go ahead and open up intellij
intellij
and in here what i'm going to do is the
following let me collapse this
and this and then open up the
student class and
this field right here so private and an
integer age
i'm going to annotate this with at
and then transient
so one thing also
so make sure that whatever package that
you import
you always use the java.persistence
because that way if we change from hibernate
hibernate
to another provider behind the scenes
everything will still work because it
will follow
this implementation right here so from persistence
persistence so what this transient does
so what this transient does basically it just says right this field
basically it just says right this field right here
right here there is no need for you to be a column
there is no need for you to be a column in
in our database so which means that
our database so which means that age will be calculated for us so
age will be calculated for us so let's go ahead also and remove
let's go ahead also and remove age from the constructors so here so
age from the constructors so here so age is gone let's remove the age
age is gone let's remove the age from here so that's gone and from here
from here so that's gone and from here as well
as well and also we actually forgot from here so
and also we actually forgot from here so there we go
there we go and in here h goes away as well
and in here h goes away as well and now if i go back to
and now if i go back to our student config so in here
our student config so in here so this no longer works
so this no longer works there we go so that's gone and also
there we go so that's gone and also in here that goes away
in here that goes away and now we want to basically calculate
and now we want to basically calculate this right so we want to basically
this right so we want to basically calculate the h for us and the way we do
calculate the h for us and the way we do it is inside of
it is inside of student so in here
student so in here where we say get and then age
where we say get and then age so what we're going to do is say
so what we're going to do is say period from java time
period from java time dot and then between
dot and then between and i want to get the date so
and i want to get the date so the start date so this will be
the start date so this will be date of birth or dob
and then local date and then dot now and then here
and then dot now and then here i want to get and then years
i want to get and then years there we go and now we have the number
there we go and now we have the number of years and also let me say
of years and also let me say this.dob oops
this.dob oops this.dob and there you go so
this.dob and there you go so now let's go ahead and start the server
so there we go server up and running and in here remember
in here remember before we had two students and then age
before we had two students and then age was 2121 because this was actually art
was 2121 because this was actually art coded
coded so now this should actually be real so
so now this should actually be real so if i refresh
if i refresh and you can see that now age is 21 and
and you can see that now age is 21 and this
this fair enough is 17 and also
fair enough is 17 and also in our database so if i describe the
in our database so if i describe the database
you can see that age is no longer a column in our database
column in our database and this is because in here so if i
and this is because in here so if i collapse this so remember
collapse this so remember we added add and then transient
we added add and then transient next let's go ahead and implement the
next let's go ahead and implement the ability
ability for our api to save students from
for our api to save students from our client
for our post request we want to send this payload right here to our server
this payload right here to our server and we want to check whether the email
and we want to check whether the email exists
exists if it doesn't then we save the student
if it doesn't then we save the student to our database if the email is taken
to our database if the email is taken we want to throw an exception so let's
we want to throw an exception so let's go ahead and
go ahead and open up intellij and in here
open up intellij and in here inside of student controller you've seen
inside of student controller you've seen this class and remember we have
this class and remember we have implemented get mapping
implemented get mapping now let's implement the api that will
now let's implement the api that will take
take a payload and then store that in our
a payload and then store that in our system
system so post is used
so post is used when you want to add new resources
when you want to add new resources to your system in our case we want to
to your system in our case we want to add a new student
add a new student so here let's go ahead and say public
so here let's go ahead and say public and then void and then i'm going to say
and then void and then i'm going to say register
register and a new and then student and this will
and a new and then student and this will take
take a student just like that
a student just like that and now what we want to do is invoke
and now what we want to do is invoke the service dot
the service dot and then add new and then shouldn't
and then add new and then shouldn't and then pass student inside
and then pass student inside now from here what i'm going to do is
now from here what i'm going to do is press
press option and then enter and i'm going to
option and then enter and i'm going to create this method
create this method inside of the service class
inside of the service class and there we go so at this point i'm not
and there we go so at this point i'm not going to do anything
going to do anything because i want to go back in here to the
because i want to go back in here to the controller
controller and in order for this to work we need to
and in order for this to work we need to do the following
do the following so here this will be at and then post
so here this will be at and then post and then
and then mapping and inside we're not going to
mapping and inside we're not going to configure anything
configure anything because this is more than enough and we
because this is more than enough and we want to
want to take the student that comes from the
take the student that comes from the client
client so this json blob right here
so this json blob right here we want to map that into this student
we want to map that into this student by taking it from the response
by taking it from the response or actually request so request
or actually request so request and then body so this is what this means
and then body so this is what this means right
right so we take the request body and then we
so we take the request body and then we map it into a
map it into a student right here so now
student right here so now we're going to add the student
we're going to add the student so let's open up the add new student and
so let's open up the add new student and for now
for now let's just say south and then student
rerun the application and within intellij
intellij right here you can see that we can test
right here you can see that we can test so in here
so in here we can test our api
we can test our api so i'm going to say open in client
so i'm going to say open in client there we go so this will be opposed to
there we go so this will be opposed to localhost
localhost and then 8080 api view on students
and then 8080 api view on students oops there we go and i actually need an
oops there we go and i actually need an example
example oh actually yeah so there we go so this
oh actually yeah so there we go so this is an example
is an example with the payload
with the payload so here we want that
so here we want that but instead what i want is
but instead what i want is this payload right here just like that
this payload right here just like that and this will do this
and this will do this request in here but for now we're not
request in here but for now we're not implemented
implemented we haven't implemented this logic nor
we haven't implemented this logic nor this one we're just
this one we're just logging to see whether we can send a
logging to see whether we can send a request to our server
request to our server so here i'm going to run this request
so here i'm going to run this request so run there we go that seems to have
so run there we go that seems to have finished
finished and let me open up the console
and let me open up the console run and check this out so in here
run and check this out so in here we have student id null name below
we have student id null name below email bilal ahmed and then the date of
email bilal ahmed and then the date of birth and then age
birth and then age equals to no so this is awesome so we've
equals to no so this is awesome so we've managed to
managed to take this payload
take this payload and then send it across to our server
and then send it across to our server so in here you see that i'm using
so in here you see that i'm using intellij and
intellij and you can also use for example postman or
you can also use for example postman or any risk client to perform this
any risk client to perform this operation in here
operation in here but the reason why i like intellij is
but the reason why i like intellij is that everything is
that everything is integrated especially if you are using
integrated especially if you are using the ultimate edition
the ultimate edition next let's go ahead and take
next let's go ahead and take this student from our service class
this student from our service class and then check whether the email exists
and then check whether the email exists if it doesn't
if it doesn't we save if not we throw an exception
all right so now that we have the student right here
student right here we can perform some business logic and
we can perform some business logic and what we want first is to open up the
what we want first is to open up the repository so in here we have our
repository so in here we have our student repository
student repository and we want to have a custom
and we want to have a custom function here that will find a user by
function here that will find a user by email so to do that is very simple
email so to do that is very simple and here we can have this done in couple
and here we can have this done in couple ways
ways we can have an optional
we can have an optional and this will be student and then we can
and this will be student and then we can construct
construct so let me import that first we can
so let me import that first we can construct the query based
construct the query based of the method name so
of the method name so find and you can see that i'm getting
find and you can see that i'm getting auto completion
auto completion student by and then email
student by and then email and here i'm going to say string
and then email so this is one way and this here will transform to an sql
and this here will transform to an sql such as
such as select and then
select and then start from and then student
start from and then student where email equals to whatever we pass
where email equals to whatever we pass so this is one way of doing things or we
so this is one way of doing things or we can annotate this with
can annotate this with add and then query in here
add and then query in here and this we can say select and then
and this we can say select and then s so lowercase s so this is an alias
s so lowercase s so this is an alias from and then student
from and then student and then s where
and then s where and then s dot email equals to question
and then s dot email equals to question mark
mark and then one so i'm actually going to
and then one so i'm actually going to leave
leave this in here because you know what is
this in here because you know what is happening
happening with this name so again i can comment
with this name so again i can comment this
this or i can be actually more specific and
or i can be actually more specific and say that this is the query that i'm
say that this is the query that i'm actually running
actually running and bear in mind that this is j this is
and bear in mind that this is j this is jpql and not a straight sql
jpql and not a straight sql and this student right here this
and this student right here this one is our class our student class so
one is our class our student class so this name here so when we said
this name here so when we said at entity and then student
at entity and then student so this is this and then we can access
so this is this and then we can access all of the
all of the properties so email age so on and so
properties so email age so on and so forth
forth so now let's go back to the service
so now let's go back to the service class
class and in here we can do the following
and in here we can do the following we can say repository
we can say repository so our student repository dot and then
so our student repository dot and then find by
find by email and then student dot
email and then student dot get email and again we can
get email and again we can we can do lots of validations here but
we can do lots of validations here but i'm going to keep it super simple
i'm going to keep it super simple and in here i'm going to say we're going
and in here i'm going to say we're going to try and find
to try and find a student by that given email right so
a student by that given email right so student by email
student by email and let me put this on a new line just
and let me put this on a new line just like that
like that and now i can say if student
and now i can say if student by email dot and then is
by email dot and then is and then present so if this is the case
and then present so if this is the case i want to
i want to throw so for now let's just say throw
throw so for now let's just say throw new and then illegal steady exception
and i'm going to say email taken for example right so obviously um there
for example right so obviously um there are better ways of handling exceptions
are better ways of handling exceptions and you should have your custom
and you should have your custom exceptions but this will do the trick
exceptions but this will do the trick and let me also rename this too
and then student and then optional there we go if the student is not
there we go if the student is not present
present we want to save our student and again
we want to save our student and again you can check
you can check whether the email is valid or not you
whether the email is valid or not you can do
can do you know much more complex validation in
you know much more complex validation in this but i'm just keeping things simple
this but i'm just keeping things simple so here now i'm going to say repository
so here now i'm going to say repository so
so student repository dot
student repository dot and then save and then we want to save
and then save and then we want to save the
the student just like that
student just like that and there we go
and there we go so to be honest this is everything
so to be honest this is everything you can see that we've added a new
you can see that we've added a new method into our repository
method into our repository and we have some business logic going on
and we have some business logic going on next let's go ahead and test
next let's go ahead and test this and see it working
next let's go ahead and test this so let me
me stop the server oh actually um
stop the server oh actually um in here so this is the server and it's
in here so this is the server and it's not running
not running so just give it a second
so just give it a second there we go so up and running now let me
there we go so up and running now let me go back
go back to this request in here that we had
to this request in here that we had before
before and what i'm going to do is
and what i'm going to do is run this request so this one to
run this request so this one to student and then with the same payload
student and then with the same payload and then
and then run you can see that
run you can see that this post right here was a 200 status
this post right here was a 200 status code
code and in here let's go ahead and try and
and in here let's go ahead and try and fetch all the students
fetch all the students you can see that we have bilal so this
you can see that we have bilal so this is now in our database
is now in our database and id 3 in here so this is working
and id 3 in here so this is working so if i try and send the exact same
so if i try and send the exact same request
request so i'm going to send the exact same
so i'm going to send the exact same request
request you can see that we do get a status code
you can see that we do get a status code 500 in here and the message is empty
500 in here and the message is empty so let's fix this message here so
so let's fix this message here so to get the message from our exception
to get the message from our exception that we just
that we just through we open up the
through we open up the application.properties
application.properties and in here at the bottom let's go ahead
and in here at the bottom let's go ahead and say
and say server and then dot error
server and then dot error and we have include message
and we have include message so this guy right and
so this guy right and let's say always
so restart the server there we go so if i send the request
there we go so if i send the request again
again so in here so remember every time that
so in here so remember every time that we restart the application
we restart the application we lose everything um so the first time
we lose everything um so the first time around that we send this request
around that we send this request this will work there we go so
this will work there we go so 200 status code
200 status code so in here 200 and if i send the request
so in here 200 and if i send the request again
you can see that now we get a status code 500
code 500 but the message here is the one that we
but the message here is the one that we added
added in our exception and that is
in our exception and that is email taken so there you have it you can
email taken so there you have it you can see that
see that we implemented the post functionality
we implemented the post functionality nicely and right here
nicely and right here so we actually implemented the
so we actually implemented the get and post right here next let's go
get and post right here next let's go ahead and
ahead and implement the ability to delete students
implement the ability to delete students from our database
all right so let's implement the delete method in here where we want to delete
method in here where we want to delete a given student by an id
a given student by an id so open up intellij and in here
so open up intellij and in here let's open up the controller so student
let's open up the controller so student controller
controller and what we're going to do here is the
and what we're going to do here is the following we'll have another method
following we'll have another method so public and then void
so public and then void delete and then student
and right here this will be at and then delete
at and then delete mapping and for deleting
mapping and for deleting what we want is inside i'm going to say
what we want is inside i'm going to say that
that this has a path
this has a path equals and then within
equals and then within i want to pass the student and then id
i want to pass the student and then id there we go just like that now
there we go just like that now inside we can grab this student id
inside we can grab this student id by saying add and then path
by saying add and then path and then variable and refer
and then variable and refer to it so student
to it so student and then id just like that and this will
and then id just like that and this will be
be long so this is the type and oops
long so this is the type and oops and this will be id just like that
and this will be id just like that and we can enforce that it's not null
and we can enforce that it's not null and other things
and other things but for now let's just keep it nice and
but for now let's just keep it nice and simple
simple now let's go ahead and say student
now let's go ahead and say student service dot
service dot and then delete and then student
and then delete and then student and here we're going to pass the id
and here we're going to pass the id so oops id just like that
and then let's create that within our student service
student service and this will be student id in fact
and this will be student id in fact let me go back here and this will be
let me go back here and this will be student id
just like that so you can see how easy it is for us to take something
how easy it is for us to take something from the path
from the path so this is a path variable and you'll
so this is a path variable and you'll see this in a second
see this in a second so now let's go back to the service
so now let's go back to the service class
class and in here i want to say student
and in here i want to say student repository
repository dot and then check this out we have a
dot and then check this out we have a bunch of methods
bunch of methods so here i want to find
and then buy id pass through an id and then here i'm going to say dot or
and then here i'm going to say dot or let me actually see whether we have
let me actually see whether we have exists so student repository
exists so student repository let's just say here student repository
let's just say here student repository dot exists yes we have an exists that
dot exists yes we have an exists that returns a boolean
returns a boolean so let's just use this instead
so let's just use this instead so there we go student id
so there we go student id and i'm going to say exists
so if and then does not exist
i want to throw new and then illegal state exception
state exception student weight id
student weight id and then plus id
and then plus id oops i'll actually shoot an id
oops i'll actually shoot an id and let me put this just like that
and let me put this just like that and then plus does not exists
otherwise if this is the case i want to say
say student repository dot
student repository dot and then delete and then in here
and then delete and then in here i want to delete by id and then fast
i want to delete by id and then fast student id and to be honest
student id and to be honest this is everything for this
this is everything for this api call so let's restart the server
api call so let's restart the server there we go so in here
there we go so in here let's have so basically we want to have
let's have so basically we want to have a delete
so we have get put so in here we want we want to have a delete method
want to have a delete method so here let's just take all of this
so here let's just take all of this and in fact we just need that and then
and in fact we just need that and then in here i guess we need to add
in here i guess we need to add pound sign three times just like that
pound sign three times just like that paste that in change this from post to
paste that in change this from post to delete
delete and then student and here i'm going to
and then student and here i'm going to delete
delete student one so
student one so if i refresh this we should have two
if i refresh this we should have two students
students right maryam and alex so i'm going to
right maryam and alex so i'm going to delete
delete mariam and
mariam and in here if i run this and by the way
in here if i run this and by the way this is the path variable right forward
this is the path variable right forward slash and then one
slash and then one and this here is what we captured
and this here is what we captured inside of this controller so this id
inside of this controller so this id right here
right here now if i run this
you can see that we have a 200 status code which means that
code which means that the request went fine and
the request went fine and if i refresh this you can see that
if i refresh this you can see that mariam is now gone so
mariam is now gone so if i run the exact same request
there we go and we get an exception back and the message is
and the message is student with id1 does not exist
student with id1 does not exist there you have it we've implemented this
there you have it we've implemented this method right here and now we can delete
method right here and now we can delete students from our system
students from our system finally let's go ahead and implement the
finally let's go ahead and implement the put functionality
all right for put what i'm going to do is to give it a try to implement the
is to give it a try to implement the functionality
functionality to be able to update both name and
to be able to update both name and email so put is used when you want to
email so put is used when you want to update a resource in your system in our
update a resource in your system in our case we want to update
case we want to update name and email so for
name and email so for this exercise what i'm going to do is to
this exercise what i'm going to do is to have a method
have a method like this one here so update student in
like this one here so update student in your service class
your service class and then use this annotation at
and then use this annotation at transactional
transactional which you haven't learned about it but
which you haven't learned about it but by using this annotation right here
by using this annotation right here it means that you don't have to
it means that you don't have to implement any
implement any jpql query so you can use the
jpql query so you can use the setters from your entity that you get
setters from your entity that you get back to check
back to check whether you can or cannot update and
whether you can or cannot update and then you can use
then you can use the setters to automatically
the setters to automatically update the entity in your database
update the entity in your database again use the setters to update the
again use the setters to update the entity
entity when it's possible so go ahead and try
when it's possible so go ahead and try and write some business logic for
and write some business logic for this exercise and i'm going to give you
this exercise and i'm going to give you the solution in the next video
all right let me go through the solution for the put mapping
for the put mapping where we want to update students
where we want to update students so remember i asked you to update
so remember i asked you to update name and email so what i went ahead was
name and email so what i went ahead was i added this method right here
i added this method right here and in here we have a path variable so
and in here we have a path variable so student id and you've seen this but then
student id and you've seen this but then we have a request parameter
we have a request parameter in here for both name and email
in here for both name and email where they're both not required
where they're both not required and then i pass it down to the service
and then i pass it down to the service with the name update student and
with the name update student and inside of this update student i've told
inside of this update student i've told you to use
you to use this annotation at transactional
this annotation at transactional so here i'm just doing simple business
so here i'm just doing simple business logic
logic i'm checking whether the student exists
i'm checking whether the student exists with that id otherwise we throw an
with that id otherwise we throw an exception
exception and then if the name not equal to null
and then if the name not equal to null and the name
and the name length is greater than zero and
length is greater than zero and the name provided is not the same
the name provided is not the same as the current one so if that's not the
as the current one so if that's not the case we want to
case we want to set the name just like this and we do
set the name just like this and we do the same
the same for email so it's the exact same logic
for email so it's the exact same logic right here but we also check that the
right here but we also check that the email right here hasn't been taken
email right here hasn't been taken if it's taken we throw an exception
if it's taken we throw an exception otherwise
otherwise we update the student by just saying
we update the student by just saying shouldn't set
shouldn't set email so here you see that we're not
email so here you see that we're not using any queries
using any queries from our repository in here we're not
from our repository in here we're not using any queries
using any queries and this is because of this
and this is because of this annotation right here at transactional
annotation right here at transactional which
which when we have this annotation the entity
when we have this annotation the entity goes into a managed state and i cover
goes into a managed state and i cover all of this
all of this in my spring data jpa course so it's
in my spring data jpa course so it's really important that you understand
really important that you understand exactly how this annotation works
exactly how this annotation works but in a nutshell this is what's
but in a nutshell this is what's happening now let me go ahead and show
happening now let me go ahead and show you that
you that in here we have mariam and alex
in here we have mariam and alex right here and what i'm going to do is
right here and what i'm going to do is i'm going to
i'm going to have a new request and
have a new request and this will be request to v1 and then
this will be request to v1 and then student and then one we want to update
student and then one we want to update mariam
mariam and we want to assign a new name called
and we want to assign a new name called maria
maria now if i send this request
you can see that it's a 200 and now in here this should be maria instead
now in here this should be maria instead of merriam
of merriam so if i refresh you can see that
so if i refresh you can see that there we go so maria instead of
there we go so maria instead of mariam let's also update the email
mariam let's also update the email so here i'm going to just
so here i'm going to just say that the new email now so in here
say that the new email now so in here i'm going to say
i'm going to say and and then email equals to
and and then email equals to and let's just say this time is just
and let's just say this time is just maria
maria and then send
there we go 200 status code and if i refresh you can see that
if i refresh you can see that the email has been updated
the email has been updated so there you have it as i said if you
so there you have it as i said if you want to learn more about spring data jpa
want to learn more about spring data jpa go ahead and check my course where i
go ahead and check my course where i teach about all of this
teach about all of this but basically the entity life cycle
but basically the entity life cycle right here
right here where i mentioned that the entity went
where i mentioned that the entity went into a managed state
into a managed state so in here manage state it's because
so in here manage state it's because of this so it's really important that
of this so it's really important that you understand this
you understand this in order to fully grasp the concept
in order to fully grasp the concept and take full advantage of spring daily
and take full advantage of spring daily jpi
right so so far you've seen the power of spring pool right it's amazing so i just
spring pool right it's amazing so i just want to
want to talk about testing because testing is
talk about testing because testing is something that you as a software
something that you as a software engineer
engineer you should know and when it comes to
you should know and when it comes to sprinkle
sprinkle everything that you know about tdd
everything that you know about tdd mocking
mocking testing external services and
testing external services and basically junit all of that stuff
basically junit all of that stuff you can take all those skills and use
you can take all those skills and use them to
them to basically test your applications right
basically test your applications right so in this course we haven't done any
so in this course we haven't done any testing
testing but testing is something that you should
but testing is something that you should know
know inside out so i've got course for you
inside out so i've got course for you um on testing and i definitely recommend
um on testing and i definitely recommend you because
you because um that will set you apart from other
um that will set you apart from other developers
developers right so i remember you know back in the
right so i remember you know back in the days i used to work with people that
days i used to work with people that wouldn't test their code and then
wouldn't test their code and then deploy the production without any tests
deploy the production without any tests but you know
but you know if you want if you are serious about it
if you want if you are serious about it then you should know
then you should know testing so in my testing course you
testing so in my testing course you you're going to learn about
you're going to learn about uh what is testing you're going to learn
uh what is testing you're going to learn about the junior framework
about the junior framework assertions uh mocking
assertions uh mocking you know why should you use
you know why should you use dependency injection and you'll see you
dependency injection and you'll see you know
know when it comes to testing why it's so
when it comes to testing why it's so important um
important um so you're going to learn about mocking
so you're going to learn about mocking as i said and also
as i said and also i'm going to show you um integration
i'm going to show you um integration tests right so this is when you spin up
tests right so this is when you spin up your
your um entire application and then you fire
um entire application and then you fire up
up requests just to make sure that
requests just to make sure that everything works so unit testing
everything works so unit testing integration testing test driven
integration testing test driven development you will learn
development you will learn at that on the course as well and then
at that on the course as well and then more important also how do you test
more important also how do you test external services for example stripe
external services for example stripe right i'm going to show you a stripe use
right i'm going to show you a stripe use case
case where we don't want to connect um to
where we don't want to connect um to their
their real systems to perform the integration
real systems to perform the integration tests
tests so i'll show you how to test against
so i'll show you how to test against external services
external services so yeah so um as i said springboard
so yeah so um as i said springboard doesn't give you
doesn't give you uh much in terms of testing so um if you
uh much in terms of testing so um if you want to do like contract testing
want to do like contract testing they do have spring contract tests which
they do have spring contract tests which is um
is um i actually haven't used it but it's it's
i actually haven't used it but it's it's there but this is more for
there but this is more for when you are working with microservices
when you are working with microservices but apart from that
but apart from that um you know testing it's something that
um you know testing it's something that if you know it then you can just use it
if you know it then you can just use it and test your applications if you don't
and test your applications if you don't know it then i highly recommend it to
know it then i highly recommend it to enroll to my course where i teach you
enroll to my course where i teach you about testing
now let's go ahead and learn how to take our api
our api produce a jar that we can then run
produce a jar that we can then run multiple instances of our application
multiple instances of our application so let me go back to intellij and in
so let me go back to intellij and in here
here what i want you to do is the following
what i want you to do is the following so if you see a target folder in here go
so if you see a target folder in here go ahead and delete it
ahead and delete it and once you delete it open up maven so
and once you delete it open up maven so maven tab
maven tab and then here you can clean the project
and then here you can clean the project first
first so this will get rid of the target
so this will get rid of the target folder and in fact let me just show you
folder and in fact let me just show you so if i run this project
so if i run this project there we go you can see that so let me
there we go you can see that so let me actually stop it
actually stop it so you can see that we have a target
so you can see that we have a target folder now this target folder contains
folder now this target folder contains a couple of things so it contains the
a couple of things so it contains the classes
classes in here generate sources
in here generate sources and basically that hasn't finished right
and basically that hasn't finished right so what i want to do is the following so
so what i want to do is the following so here
here let's stop the application from running
let's stop the application from running and open up maven and then
and open up maven and then do a clean so run the clean job
do a clean so run the clean job so this will get rid of the target
so this will get rid of the target folder you'll see in a second
folder you'll see in a second and there we go so that's finished and
and there we go so that's finished and you can see the target is gone
you can see the target is gone now let's install the application and
now let's install the application and make sure your database is up and
make sure your database is up and running
running so install and this will clean validate
so install and this will clean validate compile test package verify
compile test package verify and then inside we're going to have the
and then inside we're going to have the jar file which we can then run manually
jar file which we can then run manually so just give it a second so you can see
so just give it a second so you can see that now it's running the tests
that now it's running the tests we haven't got any tests to be honest so
we haven't got any tests to be honest so there's nothing
there's nothing uh that we've done with testing and
uh that we've done with testing and again if you want to learn about testing
again if you want to learn about testing go ahead and check my website where i've
go ahead and check my website where i've got a
got a complete course just on testing and
complete course just on testing and there we go so you can see that
there we go so you can see that we have a green tick in here and then we
we have a green tick in here and then we have the target folder
have the target folder now inside of this target folder so here
now inside of this target folder so here if i expand this
if i expand this you can see that we have demo and then
you can see that we have demo and then basically snapshot and then
basically snapshot and then jar so this is what we are interested in
jar so this is what we are interested in now let me open up the terminal from
now let me open up the terminal from here so open up terminal
here so open up terminal and then you can see that i'm inside of
and then you can see that i'm inside of demo so this folder here
demo so this folder here is this one and now i can navigate to
is this one and now i can navigate to target
target so cd and then target
so cd and then target and now we can run our application from
and now we can run our application from here
here so here i'm going to say java
so here i'm going to say java dash and then
dash and then jar and then the name of the jar so demo
jar and then the name of the jar so demo and then i've just pressed tab and you
and then i've just pressed tab and you can see that we have auto completion
can see that we have auto completion there
there now i'm going to press enter just give
now i'm going to press enter just give you a second
you a second and basically the logs are too big in
and basically the logs are too big in here
here let me see if i can zoom out
let me see if i can zoom out so i can't really zoom out we can see
so i can't really zoom out we can see that we have a couple of things
that we have a couple of things and basically the application now is up
and basically the application now is up and running right
and running right so which means that i can navigate to
so which means that i can navigate to localhost
localhost so if i open up my web browser
so if i open up my web browser and here this is the previous example
and here this is the previous example but now if i refresh you can see that
but now if i refresh you can see that the application
the application still works and we could test all the um
still works and we could test all the um post put delete basically everything
post put delete basically everything will work
will work so basically we've managed to do this
so basically we've managed to do this right so we've packaged up our
right so we've packaged up our application into hr
application into hr and now we've run one instance
and now we've run one instance if you want to run a different instance
if you want to run a different instance it is completely up to you
it is completely up to you and you can run as many as you want but
and you can run as many as you want but you need to
you need to specify the port so in here because port
specify the port so in here because port so if i um open up in here
so if i um open up in here so if i press ctrl c
so if i press ctrl c right so that shut down the application
right so that shut down the application and if i
and if i refresh you see that now it doesn't work
refresh you see that now it doesn't work but if you want to run for example the
but if you want to run for example the application on a different port
application on a different port just run the same command and then you
just run the same command and then you can say dash dash
can say dash dash server dot and then port equals to
server dot and then port equals to and then let's say 80 81 for example
and then let's say 80 81 for example enter there we go
enter there we go up and running and then refresh this
up and running and then refresh this doesn't work
doesn't work but now here if i say 8081
but now here if i say 8081 and check this out you can see that the
and check this out you can see that the application is now working
application is now working so there you have it you now know how to
so there you have it you now know how to package up your application
package up your application and then from a jar spin up an instance
and then from a jar spin up an instance that contains your application basically
that contains your application basically from now on
from now on you can basically take the jar you can
you can basically take the jar you can deploy it to a server
deploy it to a server or you can dockerize it you can pretty
or you can dockerize it you can pretty much do
much do anything you want with your jar file to
anything you want with your jar file to deploy it for real
deploy it for real users um to use your application
users um to use your application if you have any questions go ahead and
if you have any questions go ahead and drop my message otherwise let's move on
all right so it was a pleasure teaching you about spring boot
you about spring boot let me know what you thought about this
let me know what you thought about this course and what you want to see
course and what you want to see next so the next videos
next so the next videos i'm planning on providing you guys with
i'm planning on providing you guys with more
more tutorials on spring and springboard
tutorials on spring and springboard framework
framework so probably the next video will be
so probably the next video will be something along
something along the lines of creating a full
the lines of creating a full registration service
registration service um where maybe we send an email
um where maybe we send an email and then you have to register against
and then you have to register against their system and then
their system and then we'll also use some spring security
we'll also use some spring security stuff along the way
stuff along the way so um yeah so it was a pleasure teaching
so um yeah so it was a pleasure teaching you
you spring book let me know what you thought
spring book let me know what you thought about this video and
about this video and you know this is just the beginning this
you know this is just the beginning this is just the beginning and on my website
is just the beginning and on my website i've got courses
i've got courses uh on spring daily jpa spring full stack
uh on spring daily jpa spring full stack um testing as i said so
um testing as i said so go ahead and check those courses out i'm
go ahead and check those courses out i'm going to leave a link in the description
going to leave a link in the description of
of this video because that way you will
this video because that way you will boost up your skills um and
boost up your skills um and become a better developer right so
become a better developer right so knowing
knowing spring boot and java well enough
spring boot and java well enough there are so many jobs out there and i
there are so many jobs out there and i can guarantee you that
can guarantee you that with the courses that i have for you
with the courses that i have for you you won't be disappointed and in fact i
you won't be disappointed and in fact i i have
i have so many students that have benefited
so many students that have benefited from these courses
from these courses some of them they they they got jobs
some of them they they they got jobs others they they finish university and
others they they finish university and also got jobs
also got jobs so yeah so go ahead and check those
so yeah so go ahead and check those those courses also join the community i
those courses also join the community i would love to have you there join the
would love to have you there join the community
community um and yeah this is all for now thanks
um and yeah this is all for now thanks for watching
for watching assalamualaikum and i'll catch you on
assalamualaikum and i'll catch you on the next one see ya
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.