Spring Boot - Getting started with Spring Boot (EASY)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what up guys welcome to getting started with spring boot my name is Nelson and in this tutorial I'm going to show you exactly how you can start writing applications using spring boot now just trust me on this but spring boot it is awesome it is brilliant but what it is exactly so spring bility is just a framework that you can simply plug and play so they provide you some cord api is that you you know you don't have to you know do some of some of the configurations for example you don't have to you know set up all the security you've got things built in already for you for example you feel you need some jamek stuff they have that built in as well so you know also restful api is you know it is super easy to actually use it once you know how to how to use some of some of the you know the annotations and the api is that they provide so what they say is you know it makes it easy for you to create started on applique applications that you can just run and pretty much it is true right so you can you can you know create spring applications embedded tomcat with embedded tom card jetty also you know you can either pick maven or Gradle if you want to to to use with spring you know there's no need for XML configuration you can actually just use annotations and also you have things such as metric health checks and externalize configurations which is pretty cool so what we got a big building is this interior architecture where we have a several layers whereby the first one will be the control layer and it will be responsible to handle HTTP requests now this can be you know a get request a put request a put a post request and a delete request so after those requests being sent to our server then we can offload that to a service layer whereby the service level will be responsible to handle all the business logic for the application within business logic then we need a way to interact with a data access object or later per se to actually save and retrieve information from a database so you can use either my skill my SQL or even some fake database and I will even show you how easy it is for you to change your database once you have you know an implementation ready in place by using you know some some cool stuff so so that's about what we're going to be building after you finish with your back-end you can actually plug any framework that you like such as angular or react and create beautiful you eyes so that's ugly I'll leave that up to you but essentially the entities that we're going to be using for this tutorial I student so we'll have like students and you know database whereby we're going to be able to you know get students by ID get all students students update students and insert students so let's just get started so the first thing that we actually I'm going to do is to open up IntelliJ so just let me delete this how you always just plain here long so just let me delete this so you can actually use any IDE that you liked either IntelliJ or Eclipse but I prefer IntelliJ because you know is you know I think it's the standards nowadays and it's more robust and the UI looks nicer and you've got tons of cool stuff built in already into IntelliJ so what you want to do is to create a new project so go to file new project then make sure that this is a maven project and also make sure that you're using Java 1.8 so then you're going to click Next so then your group ID and because it's maven we need a group ID so you c-calm dot your name and then artifact ID that's the name of your actual you know your actual project we you can call it spring spring boot just like so and then the project name this is where the actual project will reside on your machine so you can call it the same spring boot just like that so after you're done just just just press a click finish and then you'll have a project exactly the same as mine so you'll have your your your parent directory then within that you'll have source and you'll have just let me delete this because this is not needed for now so you'll have source and then you have palm so within source you'll have main resources and I forgot that we need a Java so you have main Java resources and within source as well you'll have tests and this is where all of the tests will actually go so within main this is where our source code will go so this is Java you know you can guess Java code and then resources this can be like you know configuration files HTML and you know some other things that will be relevant for your project and also will have a pom file and this pom file is is where we're going to actually get all the dependencies that we need for our project so if I right click on my external libraries you can see that I've got many jar files in my in my class path so what I'm going to do now is go to Google and then I'm going to say spring build example and then click on the first link just like so then if you scroll down build with maven so we're going to be using maven so as you can see we have the same structure so all here so all I need pretty much from this XML file are these tags so we'll need a parent dependencies this is where you know anything that we need for this project then properties will be saying that you know we're going to define Java 1.8 so I'm just just going to copy and paste that into IntelliJ and as you can see it is maven is actually results on some dependencies and sure enough if I sorry my bad I was actually showing all the dependencies for Java yeah as you can see now you know we've we've got you know many many libraries added to our project so before just let me delete that quickly so you can see so before we had you know nothing pretty much within external libraries and now my bad sorry I was showing you all the libraries that 1.8 provides so if I copy paste that then you can see that we'll have you know tons of libraries that they provide so you can see a spring context spring beans it's TS web ok it's 20 hours and when web MVC spring build auto-configure Jackson for JSON stuff log back so you'll have like you know tons of of stuff that you can play so what we're going to do now is simply just let me get this right there so what we're going to do now is to create this package structure for our application so let me go back with in Java I'm going to right click and then say package I need a new package so I'm going to say common taught your name so essentially this is your name or your domain whatever you want to say and then within that I'm going to create a class and I'm going to call it main so within main I'm going to create a another package called controller so you see that we need a controller layer then within controller I'm going to create a new package within your name so I'm gonna a new project package called service this is the service layer and then we did that as well I'm going to create another package called Dao just like so and then within deal I'm well within my packages wrong I need a entity entity package just like so so as you can see now we we have everything that we need for our project and now let's just start coding so let's begin by implementing the actual Dao so the Dao is simply the class that will give us some data from any database that we want so I'm going to call a student Dao just like so and essentially we are just going to start small and then build as we go so we're going to implement like you know for example gate student so we're going to do the whole cycle and then see if it works if it does then we can move for example to find is to get get student by ID deletion student ID update and delete so let's just go step by step and you will see how we can so at the end this will be like the same all over again but you know it is good for you to see the process so for this class we need a student because we're going to be ordinary students so let me go into my entity and then drop a class then I'm going to say student just like so now we didn't student we need a private int ID so every student will have an ID private string name so I will have a name and private string course and yeah that's it for now now what I'm going to do is simply press control space I need a constructor just like so with all of those then also I need a default constructor so I'm going to say public student and then nothing there and then I need as well a I need getters and setters and for this entity so I'll have all of those and that's it pretty much now I go back to my studio and for this we need so we're going to be working with some fake data for now but then I'll show you how easily you can change to a real database if you want so I also private static and then I'll say map where the key will be integer and then shouldn't so that will be like the student ID and then the algae I put the actual student object so I'm going to say students just like so now I'm going to initialize this in a static block so I'm going to say if students equals to new hashmap and then we do not all have integer and students and because I'm doing this in line I'll just do like so just like so and I'm going to put about few students so I'll have one with ID 1 then I'll say new student and then ID that'll be one as well and then this will be Sayid per se and inside the as computer science science computer science now let me just copy that command D I think it's control D on Windows so we have now our data and now we're going to provide a way to actually get some information so the first method that we're going to implement is public and then we'll want a collection of students and I'm going to say get judgments or actually get all students just like so and then I'm going to return this that student dot values just like so and essentially this will get the collection from from this hash map and give it to us now let's actually implement the service so this is where all the business logic will happen so essentially just let me copy all of this control command C and then say new and then student service is called the student service and we need pretty much almost the same thing so let's say get all students I'll just keep the names for now but here we need a way to access the DA also we need an instance of the Dao so I'm going to say private student Dao control space student Dao so then I'm going to invoke in the OL because we have a method that will return you know the data from our database and then I'm going to say get all students just like so nice and easy so now let's go to our controller now the controller will pretty much be you know almost the same I'm just going to copy the method and then we say student controller just like so okay and we also need to get hold of the Student Service so I'm going to say student service control space and then I'll do the same thing here where we're just going to return the student so we can you know the name doesn't really matter but you know it's always good to keep to keep them as the same so you know there's no confusion so I'm going to say student service dot get all students now we have you know a full cycle to get all students so we implemented the actual database then we have the service that uses the database and we have the controller which offloads the the request through student service now let's actually start using spring boot in action so the first thing that i'm going to to do is in my studio i'm going to use this annotation at repository 3 positi and this is saying that you know this is a repository so essentially every bin can be of type as component but this is this is too generic so we want to be specific with our beans and just let spring know now this is actually a repository for us so and then what spring will do will actually instantiate you know all the beans for me without me having to say new to all these all the other classes so then I'm going to go to my service and then I'm going to say this is mu this will be a service so this will be a service and to my controller I will say guess what this will be rest controller now controller rest controller so now that we have that we need a way to actually inject this dependency injection on these bins because at the moment you know if I don't have any any spring annotation I would need to say equals to Neil and then that so that way this is instantiated for us but the reason why people use dependency injection is to actually remove the neo keyword as much as possible so that you have a decoupled system and you can pretty much make changes in a very flexible way so I'm going to delete this and we're going to say at Auto Wyatt so what what spring will do is simply you know find a beam that you know I'll type student service and then we'll actually instantiate an injective forming here so this will be available for me right from the get-go so the same thing I need for my student DL so I'm going to say at all to Wyatt just like so and one last thing that we need is you know we said that this is a ref controller but you know with HTTP requests you need like a URL or an end point whereby you can say local off the port slash and then some pattern now we're going to define the pattern by saying at request mapping and then we'll give it a value so we're going to say students just like so and then for this method this will actually will be a get method so I can say control space to see what's available for me now I can say method and then control space this will be a get so this will be a get request and I could say value and this would be like the actual end point but I don't need to do this because as soon as they get to students this will be the method that will be triggered so I don't need to say that so that's that now let's actually start use the application now I'm going to go to Maine and then I'm going to say public static void main enter and the way that we start this application is just saying spring application dot run now we need to say that we're going to run from the main class and then we're going to pass any arguments from the command line if any so one last thing that we need to do now is just say that this application this main application is a springboard application just like so just let me in then this for you or make you more visible so as you can see now this will be the annotation that will be used to actually make sure that spring initializes the beans it recognizes you know all the annotations configuration and so on now to start the application we just have to click on the play button and say run main so if I run that you'll see that we have you know fittings going on but one day you really care is about this so you'll see that we have a a end point force live students and then this is the method this is a get method and you can see also that your server has started on port 8080 so if we go to any browser or your browser just go to your browser then say localhost 8080 forward slash students you see that you have your information back so student ID have Sayyid then it's been computer science and then you have Alex you who does finance and then you have an elders Maps so as you can see you know we have some JSON back we didn't have to do any jason conversion because you know this is done for us spring you know does it automatically by you independency called jax-rs no jax-rs jackson so as you can see here this is this is what is this is this is actual library which is converting our beans our poachers into JSON so now that we have you know one feature which is get all students less that's just you know get shootin by ID so I'm going to go to my ideal so I'm going to start from the deal and then I'm going to save public void students so we're going to return a single student just like so get a student by ID right and then we say int ID why would I say avoid okay so and then what I'm going to do is simply say return this dot students students dot get ID so we're going to get the actual ID so then I'm going to copy this method and then go to my service and this will be that so we can say and instead of using the DAO we're going to use the actual the actual dao as you can see here let me just say this in here as well as you can see here I'm not doing any logic to be honest and you know I just want to keep it simple for this tutorial but for example you know in this in this case you could actually you know do a check so you could again by ID you can actually check if if the student exists then if it does exist you return the actual data if it doesn't then you can return like a you know HTTP you know request a code you know the data is not available or whatever so now let's actually go to our controller and Britt a an endpoint someone said at request mapping and this will be will hire will have a value and then the value will be just like so and then I'm going to say slash and then I'm going to pass an ID from my your URL so this will be a method get' so this will be a get because we're getting some some stuff out and I'm going to save public and then student get student by ID and then we're going to pass the ID in tidy just like so then we're going to return gifts one student service dot get shouldn't buy ID just like so and pass the ID back so you know in this in this scenario we're just missing one thing which is we need a way to actually gather the ID from the URL and we do that by using add path variable and then we want the actual ID so we want the ID that you sent from the URL so to see this in action what we're going to do is simply run that and you can see that we have another endpoint so you see this one here and if I go back to my browser and say fourth slash one you can see that we have Sayid so if I'm going to get Alex entry which is number two and then we have m3 and as you can see which is getting a single data back now to remove something from our database it's very simple we what we're going to do is simply say let's actually start from here but request mapping and then this pretty much will be the same so just let me copy that but instead this will be a delete so we're going to delete something from a database now I'm going to say public void delete student by ID and then I'll pass the same thing at path variable and then we need ID and yet type the type of the idea will be in tidy and then we're just going to say student service dot remove student by ID and then we'll pass the ID so now if I press alt enter I need to create this method fair enough what I'm going to do is say this dot student do dot remove student Y ID and then wanna pass a ID and as you can see you know we could check as well if you know there is a student with ID that we pass and then if if there is we can you know perform this action else we can do something else so I'm just keeping that simple for you guys to understand so now I'm going to press alt enter create method and you can see we just walked our way back so now one way to do it simple sake it is that this dot students students dot remove and then I'm going to pass the key just like so and if I run this now for us to actually perform deletes we need a HTTP client so if you go to Chrome and then type I think it's rest client so rest client you'll have like you know that you know many of them but the one I actually like is this this one here postman you can also use advanced risk line of Jesus before and it's really nice so just click on it and I'm going to launch the app ok postman let me open up postman okay fair enough so slim it let me go to student sorry to the URL and then if I say if I say get so let's say this is the get request and send as you can see we have Alex you and now let's say that we want to delete Alex you or actually left let's let me let me get Anna so let's say I want to delete Anna all I need to do is from I get I need to say this is a delete now if I say send okay we have no response back because you know we we return void but we could have could have said like return an HTTP 200 code for example but let's let's just keep it simple now let me go to get and then let's say get all as you can see Anna is not on the database anymore so let's actually delete Alex as well Alex is number two this will be a delete and let's send that then let's get all students and oops sorry it's not the lead it is a get and as you can see only side it is in our database now let's implement let's say that this will be a put request so again let's start from the back so this will be public so this will be H let's actually return nothing for now I say public void and then let's say we go we're going to update a student after student update student by ID oops just like so inside a actually student so we need to say student student just like so and what we're going to do is simply say we're going to get shouldn't so and then it shouldn't and then I'll say equals to students dot get and then student dot get ID just like so so student already is defined okay then let's say s let's go to s for now and now one way to do is S dot set course and then I'm going to say shouldn't dot get course and then as dot setname you know let's say that the idea you can't really change any and then I'm going to say student student dot get name let's say that you want to change the name of a student just like so and then we want to put it back into the database so we're going to say students dot put and then the idea will be student don't get ID and then we're going to put the student just like so so this will update and let's actually implement actually let's just do this one for now now let's go to our service and then I'm going to copy that and say update student by ID like so actually it's not even update to the by ID it's just update student and let me remove that from here well update student just like so and now all I need to do is say student Dao or this is gonna be more specific student Leo dot update student ID and if I'm a student just like so and then for our controller we need a way to actually handle this so I'm going to say 23 just like that the value will be the valley will be will be nothing there and the method will be a foot so we're going to put something to the database and I'm going to pass the actual student so I'm going to pass a student just like so and then here I'm going to say update student and then I want to pause the student just like so so now let's actually test this and see if it works so let's say that we want to test this will be post money right here so let's say put and then the body will be raw this will be like a JSON just let me copy the sky here so we're going to update side first almost let's actually test that we have you know okay we have three people in database now let's actually do a pull request so I need to say okay in the body I'm going to send something Jason Jason I'm gonna send that JSON back and let's change this is like let's place please is full name say say guy Dean and II did just it it maps as well I always say it is master change his mind so now what we're going to do is say send so if I say send okay oops no pointer exception now the reason why we may have a null pointer exception is because we need to read the actual body that we sent so to do that we need to say at request body so that will actually be inside so that will be a student so we're going to we going to read the request body sent from this request and sure enough it should work let me just go back okay so many of them open let me go back let's let's do a get okay it is working now let's let's do a put send and okay we have unsupported media type so what we need to do now is say spring tell spring that okay we need to tell spring that this guy here will actually will actually accept a JSON so we're going to control space and then consumes so we're going to we're going to consume a media type media type just like so dot application JSON value so now if i refresh it should work definitely I haven't prepared this in an but I'm quite sure this will work so if I say if I do send oh my god we have okay just let me see let me go back so this is a put and consumes applications or JSON that's the ID proms okay message okay oh okay I know why so I think I think the reason is because we are sending plain text instead of JSON and we specifically said that we we want a JSON object so now if I say send surely enough now it worked so if I actually didn't work because I yeah so yeah I did what kind of change the data now if I say get students just and then send you will see that his name has changed now if I repeat this again so let me go to the body let's say now let's change this name completely completely let's call it Nelson Nelson did photography just like that and then I'll say send and if I do a get request if I send a request sure enough we have Nelson in the database now we need to implement what else we have to implement with all steps so now we need to actually insert a new student into a database that's very simple and the same as this delete so we're going to say insert student student student and exactly this will be a post and we're going to consume a JSON as well and the request body will be a student that we'll be inserting and then here I'm just going to say insert insert student student just like so let me implement this method well we're going to create this let's say void for now and then I'm going to invoke my student a student yellow dot insert student to DB just like so narrow pants student and we don't have this method at the moment this let me press alt enter create this method and then what we're going to do is say this dot student students stood tense dot put and then I'm going to say student dot get ID and then I want to play student just like so and I think we I think we're done to be honest let me go to my service we can use this to my specific but because we just have one you know that this doesn't there's no mess confusion them I mean we could live it without this but now let's actually you know test all of this so let's say let's rerun this as you can see we now have you know all the methods needed for this project and if I say okay so let's do a a get just to make sure that it is working showed enough let me copy this guy here and then instead of a put all I'll be triggering triggering a post and then this will be like let's say we have slightly 33 and this guy is is Mo Farah things like that is more Farah and Mo Farah the sports now if I say send fair enough we don't have any response but we could have added you know two hundred code but if I say now post if I trigger it but I get now you can see mo farah it is indeed in our database now let's let's just trigger some more posts so we have 33 we have 233 let's call it let's call him Michael Michael and Michael did pure science just like that let me post and then let's have Cheryl who did let's say it in English and then send and now let's actually get all the students so you can see that we have you know lots of students already in the database let's say I want to delete delete' student ID 44 that has to be a delete now send that's fine if I get all the students that has to be a get and you can see not there anymore and let's just do one more let's just do a to do to to do to do a put and let's change was it or was it that we said so let's let's change more Farah so 33 actually let me copy this so that'll be a put the body will be more fara and let's say that mo farah change his mind and he wants to do computer science as well computer science and then let's send a request this is a put sind fair enough let's do a get and sure enough Mo Farah did computer science so as you can see like like we have the whole application working and pretty much we managed to do you know the whole entire architecture where we had the controller layer where we did some HTTP requests then we had the service layer where we didn't actually have too much of business logic but that depends you know I just want to be quick for this tutorial and also we had a data access and this actually was responsible from getting data so now we we simply got data from you know we just got fake data but let's say that we had my sequel or database that we want to get data from so the way that you would do that is so going back to the dao class so what we need to do now is if you go to refactor then go to i think is extract okay yeah extract and then interface so we are going to extract all of these methods to an interface just like so okay let's say let's actually say this will be our implementation sh t let's refactor this to be our implementation so this will be in pull support implementation let's say fake student Dao implement actually that is less yeah just let's say implementation of implementation input for implementation now I'm going to refractor all of that say okay and now I'm going to go to the class and then go to refactor and simply say we will again extract and an interface now I'm going to select all of these so this is my public interface now I'm going to refactor and I'm going to call it Dao so this will be like the student Dao and refactor interface has been sexy created blah blah blah and replace them in usage we're going to proceed say yes so I want to select all okay and then do refractor so as you can see now we we completely changed the way that all the other classes use this interface so here instead of having using the fake data we're just using a simple interface and the way that we're going to you know make changes on this is like first let me actually show you the Dao as you can see it's just an interface now for example let's say that I have a DB database now if I straight a new class so they say mung student Dao input I mean you have to say in pull but no I mean just let's just leave it and then that implements what student Dao like so now I need to implement all the methods so I mean here you would you know make a connection connection connection I'm just faking this to be honest you know you would you wouldn't say it's not like this but essentially you'd have a connection and then once you have the connection then you'd have like your your template template class where you can say things such as insert delete you know all the methods that you you would want to do and essentially then all you needed to do all you need to do is if you go to to the service or actually to the fake data so now we have like a a conflict between all of these all of these classes because you know if we go to the service service uses a student Dao and you know we have more than two students you know so we have two classes that implement student Dao so spring you wouldn't know you know out of the box you know which Dao to actually inject so this is when we use at qualifier so we are going to use that qualifier and then we're going to say fake data then let's go to our fake data implementation and then at the top of the class I'm going to say that's qualifier and then I'm going to say fake data now if I go to I can say that this is at repository and the qualifier will be data right so now if I want to use let's say I want to use in my service I want to use data I'll just say data and then my implementation changes completely so if you had a my sequel database you could just say my sequel and then that would work perfectly fine so just to prove a point let's say that we are going to use implement this method so among the data so if I go to implementation let's just implement this guy here and I'm going to return let's say new array list or array list array list and let's say we have just one student there at the moment just like so and then this is add so I'm going to add a new student and that will be one actually yeah one and then the name open I don't know Mario and Mario did nothing it into the Lafayette University let's just let's just pretend no nothing so I mean I don't know how to do anything else pretty much just let me prove a point to you guys so if I open up my favorite that we start so as you can see spring will actually you know run all of that and if I go back and then say get all students check this out that I am so this is coming from a database right now and not fake database so as you can see is really really powerful and you know it's super cool super nice and essentially I mean every Java developer you know must know how to use one of these because it saves you a lot of a lot of time so if you have like an idea and you know you quickly want to have a a prototype up and running within within you know two to three hours you can have spring boot and essentially have a prototype that you can show to someone that you interested in so there we go so one last thing now that we are going to do so let's say that you want to to you know send this to someone so that they can they can actually test it so you know you've developed this nice application and you want to send to your friends or your boss and you want them to test this application so how would you do that so if you go to this tab so maven project and then click on that and then you have life cycle and now what we're going to do is simply say install so we're going to install this this project so click on that and that will install the project so you can see that the build was success now if I open up target and I have this jar file so I'm going to open it up so this to work you need to make sure that you have Java in your in your I think is in Windows I think is system variables so I think you have to have that and now let's say that I copy the sky onto the desktop and I name it let's say my app let's say I named app example so if I open terminal you say PWD so I think is let's let's just drag this here so this less cd2 uses desktop jar just like so so if you if you want to run this for example you know just from this jar file all you would do is say Java and then jar and then up dot yeah just like so and then oh actually I do have Java on my class path on my system variables twelve I didn't even know that there we go so now as you can see you know IntelliJ is not even running and if I go to this let's now open up Safari okay so far it isn't open let's open up Chrome let's go back to Chrome so and now I say students and because I was using check this out we have data coming back for for this and database so I know pretty much this is this is this is all I wanted to show you guys today I hope that you found this useful you know if you have any questions please please please leave them in the comment section below and if you also subscribe to my channel that will help me a lot in making up these videos for you guys because I love teaching and you know if you want me to teach anything that you want to sample I didn't cover security and didn't cover you know database and integration I didn't cover how to use property files you can you know just leave a comment below and I will make a video for sure covering those topics so without further adieu thanks for watching my video and don't forget to subscribe see ya
Info
Channel: Amigoscode
Views: 418,025
Rating: undefined out of 5
Keywords: spring boot 2, spring boot example, spring boot basics, spring boot api, java programming, aws, databases, mysql, spring framework, pivotal
Id: Ke7Tr4RgRTs
Channel Id: undefined
Length: 56min 53sec (3413 seconds)
Published: Fri Jul 15 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.