Spring Boot Tutorial | Full Course [2021] [NEW]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 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 on how this framework does work and we're going to build an api where we're going to have three 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 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 this course 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 on my website for free yes for free you can enroll and you will get a 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 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 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 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 has dependence injection built-in configuration great community and a bunch more so i'm super excited for this course next let's go ahead and learn how to get started with springbook 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 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 with spring boot now in order for us to get started with spring boot let's go ahead and navigate 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 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 in here we can pick dependencies that our project needs so this list is quite huge and by all means feel free to 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 and let's scroll down so they've got a bunch of things spring security if you want so we're gonna leave that out for this 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 such as postgres you can also do it for this course let's connect to a postgres database so here let's select the postgresql driver so i've just pressed command there again 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 kafka and spring batch java mail and you can see that the list is just crazy including graphite and 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 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 project with intellij 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 so i highly recommend you to give it a 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 allows you to download and manage all of the ides and there we go so now i'm going to click on open 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 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 boot starter data jpa and then start a web as well right here and the dependency for postgres is right 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 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 dependency right here which is springbootstarterparent there you have it you've successfully created an empty springboard application next let's go ahead and start the application and watch it running 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 goes so let's open up dem application 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 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 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 and static template and 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 but this makes sure that it's actually 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 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 implement a simple restful api so far we have the server up and running on 48080 so what i want you to do is to navigate to your web browser and then 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 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 so close that and application.properties as well so here what i'm going to do is right 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 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 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 let's go ahead and start the server 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 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 of and then string in here and here i'm going to say list dot and then of and then i'm going to pass hello as one element and then world as another 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 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 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 and then we're going to give it some attributes and behaviors and then ultimately students will end up in a database but for now let's just begin with a simple class that will represent a student and then implement the student based off everything in this diagram 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 so so so this will be our model right here so public class student and now let's go ahead and define private and then we want long for id we also 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 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 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 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 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 of and then student and then in here what i'm going to do is on a new line i'm going to delete that and then say new and then student 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 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 just like that and then comma and then here let's just simply say 21. we could actually subtract this so 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 and by the way you see that we have id 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 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 apis with springboot 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 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 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 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 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 and here we're going to annotate with add and then rest controller and then what we also 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 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 get and then students so now we have a get mapping for our student controller so let's start the application there we 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 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 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 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 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 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 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 list we also want this to come from a database but we're going to worry about that in a 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 so let me just put this like this you can see that we are having a reference to student service and then here we pass it inside of the controller now this is not going to work because we don't have an instance of student 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 so how do we tell that this student service should be injected into 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 should be auto wired for us so this will be magically instantiated for 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 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 because now it knows where to find this being right here so if i go back once 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 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 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 now let's restart the server 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 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 and then get it back out if you have any questions go ahead and let me know otherwise let's move on 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 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 or properties and then paste that in there we go so this is the configuration that we 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 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 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 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 from enterprise db.com and i'll leave the link in description of this video and download for windows version right here and you can download the latest version 13.1 as i speak also i'm going to leave a link for 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 what we're going to do next to connect from our application to our database we have to do the following so in here remember that we've changed 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 including the advanced databases and database design and implementation where you will master everything there is to know about 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 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 and then all privileges on and then the database that i've just 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 give it to postgres so just like that now if i clear the screen and then type backslash and then l you can see that we have this database right here called student so if i connect to it backslash c and then student and then backslash d and no relations so now let's open up intellij and i'm going to open up the palm.xml before remember we commented out this dependency so i'm just going to 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 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 how to do next 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 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 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 what is happening under the hood right let's go ahead and test these changes that we've done using spring data jpa using these annotations at entity at table and then the id with a sequence as well so let's go ahead now and restart the application and right here let me put this a little bit bigger 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 and we have create sequence student starting at one increment one so basically this sequence is this one so sequence generator and allocation size right here so increment of one so that's what it means and then we have a create table statement in here have a look create table and 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 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 so if i open up my shell so in here remember before when i type 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 and all of these columns so this is pretty nice so what we are missing now is this part right here so the data access 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 with our database 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 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 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 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 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 let's carry on this course 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 student package and this class i'm going 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 and then jpa and then repository right 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 that's why this is long and this is the type that we want this repository to work upon now let's annotate this with add oops not backslash two but 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 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 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 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 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 we can save and we can flash we can 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 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 there we go so server up and running and if i open up my web browser so remember here before we had one 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 so in here what i'm going to do is i'm going to say select oops select star and then from and then student semicolon enter and have a look 0 rows so let's go ahead and add some 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 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 say command 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 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 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 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 and alex just like that and then here and this with semicolon and there we go now let's restart the server 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 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 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 semicolon and then execute 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 these three other http methods so post for saving new resources put for updating the lead for the leading so if you have any questions drop me a message otherwise catch me in the next one 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 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 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 to another provider behind the scenes everything will still work because it will follow this implementation right here so from persistence so what this transient does basically it just says right this field right here there is no need for you to be a column in our database so which means that age will be calculated for us so let's go ahead also and remove age from the constructors so here so age is gone let's remove the age from here so that's gone and from here as well and also we actually forgot from here so there we go and in here h goes away as well and now if i go back to our student config so in here so this no longer works there we go so that's gone and also in here that goes away and now we want to basically calculate this right so we want to basically calculate the h for us and the way we do it is inside of student so in here where we say get and then age so what we're going to do is say period from java time dot and then between and i want to get the date so the start date so this will be date of birth or dob and then local date and then dot now and then here i want to get and then years there we go and now we have the number of years and also let me say this.dob oops 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 before we had two students and then age was 2121 because this was actually art coded so now this should actually be real so if i refresh and you can see that now age is 21 and this fair enough is 17 and also in our database so if i describe the database you can see that age is no longer a column in our database and this is because in here so if i collapse this so remember we added add and then transient next let's go ahead and implement the ability for our api to save students from our client for our post request we want to send this payload right here to our server and we want to check whether the email exists if it doesn't then we save the student to our database if the email is taken we want to throw an exception so let's go ahead and open up intellij and in here inside of student controller you've seen this class and remember we have implemented get mapping now let's implement the api that will take a payload and then store that in our system so post is used when you want to add new resources to your system in our case we want to add a new student so here let's go ahead and say public and then void and then i'm going to say register and a new and then student and this will take a student just like that and now what we want to do is invoke the service dot and then add new and then shouldn't and then pass student inside now from here what i'm going to do is press option and then enter and i'm going to create this method inside of the service class and there we go so at this point i'm not going to do anything because i want to go back in here to the controller and in order for this to work we need to do the following so here this will be at and then post and then mapping and inside we're not going to configure anything because this is more than enough and we want to take the student that comes from the client so this json blob right here we want to map that into this student by taking it from the response or actually request so request and then body so this is what this means right so we take the request body and then we map it into a student right here so now we're going to add the student so let's open up the add new student and for now let's just say south and then student so let's just rerun the application and within intellij right here you can see that we can test so in here we can test our api so i'm going to say open in client there we go so this will be opposed to localhost and then 8080 api view on students oops there we go and i actually need an example oh actually yeah so there we go so this is an example with the payload so here we want that but instead what i want is this payload right here just like that and this will do this request in here but for now we're not implemented we haven't implemented this logic nor this one we're just logging to see whether we can send a request to our server so here i'm going to run this request so run there we go that seems to have finished and let me open up the console run and check this out so in here we have student id null name below email bilal ahmed and then the date of birth and then age equals to no so this is awesome so we've managed to take this payload and then send it across to our server so in here you see that i'm using intellij and you can also use for example postman or any risk client to perform this operation in here but the reason why i like intellij is that everything is integrated especially if you are using the ultimate edition next let's go ahead and take this student from our service class and then check whether the email exists if it doesn't we save if not we throw an exception all right so now that we have the student right here we can perform some business logic and what we want first is to open up the repository so in here we have our student repository and we want to have a custom function here that will find a user by email so to do that is very simple and here we can have this done in couple ways we can have an optional and this will be student and then we can construct so let me import that first we can construct the query based of the method name so find and you can see that i'm getting auto completion 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 such as select and then start from and then student where email equals to whatever we pass so this is one way of doing things or we can annotate this with add and then query in here and this we can say select and then s so lowercase s so this is an alias from and then student and then s where and then s dot email equals to question mark and then one so i'm actually going to leave this in here because you know what is happening with this name so again i can comment this or i can be actually more specific and say that this is the query that i'm actually running and bear in mind that this is j this is jpql and not a straight sql and this student right here this one is our class our student class so this name here so when we said at entity and then student so this is this and then we can access all of the properties so email age so on and so forth so now let's go back to the service class and in here we can do the following we can say repository so our student repository dot and then find by email and then student dot get email and again we can we can do lots of validations here but i'm going to keep it super simple and in here i'm going to say we're going to try and find a student by that given email right so student by email and let me put this on a new line just like that and now i can say if student by email dot and then is and then present so if this is the case i want to 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 are better ways of handling exceptions and you should have your custom 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 present we want to save our student and again you can check whether the email is valid or not you can do you know much more complex validation in this but i'm just keeping things simple so here now i'm going to say repository so student repository dot and then save and then we want to save the student just like that and there we go so to be honest this is everything you can see that we've added a new method into our repository and we have some business logic going on next let's go ahead and test this and see it working next let's go ahead and test this so let me stop the server oh actually um in here so this is the server and it's not running so just give it a second there we go so up and running now let me go back to this request in here that we had before and what i'm going to do is run this request so this one to student and then with the same payload and then run you can see that this post right here was a 200 status code and in here let's go ahead and try and fetch all the students you can see that we have bilal so this is now in our database and id 3 in here so this is working so if i try and send the exact same request so i'm going to send the exact same request you can see that we do get a status code 500 in here and the message is empty so let's fix this message here so to get the message from our exception that we just through we open up the application.properties and in here at the bottom let's go ahead and say server and then dot error and we have include message so this guy right and let's say always so restart the server there we go so if i send the request again so in here so remember every time that we restart the application we lose everything um so the first time around that we send this request this will work there we go so 200 status code so in here 200 and if i send the request again you can see that now we get a status code 500 but the message here is the one that we added in our exception and that is email taken so there you have it you can see that we implemented the post functionality nicely and right here so we actually implemented the get and post right here next let's go ahead and 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 a given student by an id so open up intellij and in here let's open up the controller so student controller and what we're going to do here is the following we'll have another method so public and then void delete and then student and right here this will be at and then delete mapping and for deleting what we want is inside i'm going to say that this has a path equals and then within i want to pass the student and then id there we go just like that now inside we can grab this student id by saying add and then path and then variable and refer to it so student and then id just like that and this will be long so this is the type and oops and this will be id just like that and we can enforce that it's not null and other things but for now let's just keep it nice and simple now let's go ahead and say student service dot and then delete and then student 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 and this will be student id in fact 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 from the path so this is a path variable and you'll see this in a second so now let's go back to the service class and in here i want to say student repository dot and then check this out we have a 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 let me actually see whether we have exists so student repository let's just say here student repository dot exists yes we have an exists that returns a boolean so let's just use this instead 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 student weight id and then plus id oops i'll actually shoot an id and let me put this just like that and then plus does not exists otherwise if this is the case i want to say student repository dot and then delete and then in here i want to delete by id and then fast student id and to be honest this is everything for this api call so let's restart the server there we go so in here 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 so here let's just take all of this and in fact we just need that and then in here i guess we need to add pound sign three times just like that paste that in change this from post to delete and then student and here i'm going to delete student one so if i refresh this we should have two students right maryam and alex so i'm going to delete mariam and in here if i run this and by the way this is the path variable right forward slash and then one and this here is what we captured inside of this controller so this id right here now if i run this you can see that we have a 200 status code which means that the request went fine and if i refresh this you can see that 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 student with id1 does not exist there you have it we've implemented this method right here and now we can delete students from our system 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 functionality to be able to update both name and email so put is used when you want to update a resource in your system in our case we want to update name and email so for this exercise what i'm going to do is to have a method like this one here so update student in your service class and then use this annotation at transactional which you haven't learned about it but by using this annotation right here it means that you don't have to implement any jpql query so you can use the setters from your entity that you get back to check whether you can or cannot update and then you can use the setters to automatically update the entity in your database again use the setters to update the entity when it's possible so go ahead and try and write some business logic for 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 where we want to update students so remember i asked you to update name and email so what i went ahead was i added this method right here and in here we have a path variable so student id and you've seen this but then we have a request parameter in here for both name and email where they're both not required and then i pass it down to the service with the name update student and inside of this update student i've told you to use this annotation at transactional so here i'm just doing simple business logic i'm checking whether the student exists with that id otherwise we throw an exception and then if the name not equal to null and the name length is greater than zero and the name provided is not the same as the current one so if that's not the case we want to set the name just like this and we do the same for email so it's the exact same logic right here but we also check that the email right here hasn't been taken if it's taken we throw an exception otherwise we update the student by just saying shouldn't set email so here you see that we're not using any queries from our repository in here we're not using any queries and this is because of this annotation right here at transactional which when we have this annotation the entity goes into a managed state and i cover all of this in my spring data jpa course so it's really important that you understand exactly how this annotation works but in a nutshell this is what's happening now let me go ahead and show you that in here we have mariam and alex right here and what i'm going to do is i'm going to have a new request and this will be request to v1 and then student and then one we want to update mariam and we want to assign a new name called maria now if i send this request you can see that it's a 200 and now in here this should be maria instead of merriam so if i refresh you can see that there we go so maria instead of mariam let's also update the email so here i'm going to just say that the new email now so in here i'm going to say and and then email equals to and let's just say this time is just maria and then send there we go 200 status code and if i refresh you can see that the email has been updated so there you have it as i said if you want to learn more about spring data jpa go ahead and check my course where i teach about all of this but basically the entity life cycle right here where i mentioned that the entity went into a managed state so in here manage state it's because of this so it's really important that you understand this in order to fully grasp the concept 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 want to talk about testing because testing is something that you as a software engineer you should know and when it comes to sprinkle everything that you know about tdd mocking testing external services and basically junit all of that stuff you can take all those skills and use them to basically test your applications right so in this course we haven't done any testing but testing is something that you should know inside out so i've got course for you um on testing and i definitely recommend you because um that will set you apart from other developers right so i remember you know back in the days i used to work with people that wouldn't test their code and then deploy the production without any tests but you know if you want if you are serious about it then you should know testing so in my testing course you you're going to learn about uh what is testing you're going to learn about the junior framework assertions uh mocking you know why should you use dependency injection and you'll see you know when it comes to testing why it's so important um so you're going to learn about mocking as i said and also i'm going to show you um integration tests right so this is when you spin up your um entire application and then you fire up requests just to make sure that everything works so unit testing integration testing test driven development you will learn at that on the course as well and then more important also how do you test external services for example stripe right i'm going to show you a stripe use case where we don't want to connect um to their real systems to perform the integration tests so i'll show you how to test against external services so yeah so um as i said springboard doesn't give you uh much in terms of testing so um if you want to do like contract testing they do have spring contract tests which is um i actually haven't used it but it's it's there but this is more for when you are working with microservices but apart from that um you know testing it's something that if you know it then you can just use it and test your applications if you don't know it then i highly recommend it to enroll to my course where i teach you about testing now let's go ahead and learn how to take our api produce a jar that we can then run multiple instances of our application so let me go back to intellij and in here what i want you to do is the following so if you see a target folder in here go ahead and delete it and once you delete it open up maven so maven tab and then here you can clean the project first so this will get rid of the target folder and in fact let me just show you so if i run this project there we go you can see that so let me actually stop it so you can see that we have a target folder now this target folder contains a couple of things so it contains the classes in here generate sources and basically that hasn't finished right so what i want to do is the following so here let's stop the application from running and open up maven and then do a clean so run the clean job so this will get rid of the target folder you'll see in a second and there we go so that's finished and you can see the target is gone now let's install the application and make sure your database is up and running so install and this will clean validate compile test package verify and then inside we're going to have the jar file which we can then run manually so just give it a second so you can see that now it's running the tests we haven't got any tests to be honest so there's nothing uh that we've done with testing and again if you want to learn about testing go ahead and check my website where i've got a complete course just on testing and there we go so you can see that we have a green tick in here and then we have the target folder now inside of this target folder so here if i expand this you can see that we have demo and then basically snapshot and then jar so this is what we are interested in now let me open up the terminal from here so open up terminal and then you can see that i'm inside of demo so this folder here is this one and now i can navigate to target so cd and then target and now we can run our application from here so here i'm going to say java dash and then jar and then the name of the jar so demo and then i've just pressed tab and you can see that we have auto completion there now i'm going to press enter just give you a second and basically the logs are too big in here let me see if i can zoom out so i can't really zoom out we can see that we have a couple of things and basically the application now is up and running right so which means that i can navigate to localhost so if i open up my web browser and here this is the previous example but now if i refresh you can see that the application still works and we could test all the um post put delete basically everything will work so basically we've managed to do this right so we've packaged up our application into hr and now we've run one instance if you want to run a different instance it is completely up to you and you can run as many as you want but you need to specify the port so in here because port so if i um open up in here so if i press ctrl c right so that shut down the application and if i refresh you see that now it doesn't work but if you want to run for example the application on a different port just run the same command and then you can say dash dash server dot and then port equals to and then let's say 80 81 for example enter there we go up and running and then refresh this doesn't work but now here if i say 8081 and check this out you can see that the application is now working so there you have it you now know how to package up your application and then from a jar spin up an instance that contains your application basically from now on you can basically take the jar you can deploy it to a server or you can dockerize it you can pretty much do anything you want with your jar file to deploy it for real users um to use your application 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 let me know what you thought about this course and what you want to see next so the next videos i'm planning on providing you guys with more tutorials on spring and springboard framework so probably the next video will be something along the lines of creating a full registration service um where maybe we send an email and then you have to register against their system and then we'll also use some spring security stuff along the way so um yeah so it was a pleasure teaching you spring book let me know what you thought about this video and you know this is just the beginning this is just the beginning and on my website i've got courses uh on spring daily jpa spring full stack um testing as i said so go ahead and check those courses out i'm going to leave a link in the description of this video because that way you will boost up your skills um and become a better developer right so knowing spring boot and java well enough there are so many jobs out there and i can guarantee you that with the courses that i have for you you won't be disappointed and in fact i i have so many students that have benefited from these courses some of them they they they got jobs others they they finish university and also got jobs so yeah so go ahead and check those those courses also join the community i would love to have you there join the community um and yeah this is all for now thanks for watching assalamualaikum and i'll catch you on the next one see ya you
Info
Channel: Amigoscode
Views: 475,166
Rating: 4.9380317 out of 5
Keywords: learn spring, learn spring boot, spring boot tutorial for begineers, java spring boot, java spring tutorial, spring mvc tutorial, spring mvc, spring security tutorial, netflix and spring boot, spring boot 2, how to use spring boot, what is dependecy injection, spring boot initializr, java tutorial, learn java, amigoscode, java, how to code
Id: 9SGDpanrc8U
Channel Id: undefined
Length: 97min 30sec (5850 seconds)
Published: Sun Jan 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.