Learn SpringBoot in 10 minutes | SpringBoot REST API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hackers welcome back once you learn java the next best thing to learn is java spring boot java spring and java spring boot allow you to create java applications pretty easily to get started with spring boot we'll use the spring boot initializer the spring initializer gives us a starter package that we can use to create our first spring application we'll use maven as our java package manager this is similar to pip for python or npm for node our language is java so we'll keep that the same the spring boot version isn't super important here because we'll be doing the basics so we'll keep the default then we'll set up the metadata for our application i'm going to add blondie bytes and rename this to tutorial we'll package our application as a jar and we're going to use the latest version of java so that's java 15. if you want to use this version of java make sure you have the jdk for java 15 installed that can be done by simply googling java jdk and you can download it here now for the fun we'll add the dependencies this is all the code we get for free to keep things simple we're only going to include spring web for now but we could use any of these dependencies and this would enhance our application even more let's click generate and a sample project is downloaded we use the spring initializer because it helps us get some of the boilerplate code out of the way so we can focus on our application code we'll open it up and drag it into the desktop to edit our sample code i'm going to use intellij which is my ide but you could use any ide you like eclipse netbeans whatever ide you use you'll have to install it so if you want to use intellij make sure you install it from the internet ides just make it easier for us to write our application code we're going to import our tutorial project that's right here in our desktop and we're going to import the project using maven that package manager when we import with maven it'll pull down the dependencies for us we're going to select import maven projects automatically and we'll select the jdk 15. this matches up with java 15 which is the language version that we're using if you selected java 15 in the spring initializer then you'll want to download the jdk and select it here as well they have to match in order for your program to work let's click next we'll click next the jdk 15 is selected so this looks good the projects on my desktop we'll call it tutorial and let's load it up all right let's take a look here we have our source code main java our project this tutorial application it has our base main function that runs the spring application we have an application.properties file for configuration we have a folder for our test classes so our unit tests and integration tests we have a pom file that specifies the java version if you're getting any errors make sure that's set here it also specifies our dependencies and so that's spring boot starter test spring boot starter web and a maven plugin spring boot starter web is actually what we selected previously in the dependencies window the parent of this application is spring boot starter parent this gives us a bunch of stuff by default if you're having any issues with the loading here there are a couple things you can do likely it's due to mismatched jdk versions if you go to external libraries make sure this is 15 or if you're not using java 15 make sure all of these match so in the properties java.version it says 15. if i select on my project and go to module settings we'll select sdk here it says 15 all of this looks great and if you need to reset your path for your java version i have this nifty function in my aliases where i can just type in jdk 15 and it will switch to the appropriate jdk type if you want to configure this i'll show it in my bash profile here you can see it jdk open close parentheses it's a function it takes in a java version and then it resets where my java home variable looks at in this case it's going to look at that version and then it prints out the new version for us in this case java 15. this can be something useful you could also just do something like this right in the terminal and you can put in that version number right here and that will do the exact same thing it won't print out the version because you'd also need to add this command and everything looks great let's clear this up and we'll run our application to run it i'm just going to select tutorial application click run and this will start it up we see at the top it's using jdk 15 and it looks like we have something on this port if you're on a mac you can actually open up the activity monitor search for java and cancel these programs so that you can run your application it looks like a previous app was running before let's run our app again since we've run it before it shows up here in intellij so i can just click play and it started up here tomcat which is a tomcat server that's what spring is built on by default we're starting it at point 8080. now we need to add some code to make our app do anything so let's stop it for now and add some code we're going to create a simple web application all we want to do is display a message in our browser at localhost 8080. to do this we'll need to create a rest controller it's going to live in its own class we'll call it greeting controller and we'll give it the annotation rest controller the rest controller will be a component that contains routes of our application and what happens when users visit those routes to set up a route we'll create a function called get greeting and we'll have it return a message that we want displayed on our localhost 8080 page some people do a basic hello world message but we're not basic so we're gonna say hi it's me blondie bites and i like pizza i like to create a custom hello world message to make sure it's me and it's not some default thing coming from somewhere else in the application we are actually not returning void we're returning a string and now our application looks good this is just a function but to make it a route in our application we'll need to add the request mapping annotation so we'll just type request mapping these are annotations that are built into the spring framework see these import statements up here if we did not have those dependencies in our pom file then we wouldn't be able to use this now we're not done with this request mapping we have a bunch of different values we can give it we can give it a name value path method the path we're just going to give it the backslash and we could also write something like path equals but we don't need to do this we can just do the slash the default method is a get request so we don't need to add that here either if we wanted to make this a put or a post we could just add method equals post or whatever method we'd like to do here since we want to do a get it's the default so we don't need to add that so with the request controller and with the request mapping annotation when i run the app and go to the local host home page i should see our greeting let's run it let's see what happens all right our application has started on port 8080 so if i open my browser here go to localhost 8080 there's our message hi it's me blondiebytes and i like pizza with this application we have a running rest api with one get endpoint the home endpoint or the home route if we made this slash greeting stopped our application and ran it again then we would see this display message at localhost 8080 slash greeting let's try it all right it started up we're going to go to the home route we get an error this is good because our route is now located at slash greeting and there's our greeting you can have multiple controllers in your application with related routes put together so i like to put all the controllers in their own package to do that i'll go new package controller and we'll put our greeting controller in there this helps keep our code a little more organized what we've created here is a spring boot application when people say they're using spring or spring boot there's so many different things they could be talking about remember that long list of dependencies it's kind of like saying you're using amazon web services it's so generic that you really need to specify what services you're familiar with here we are using spring mvc model view controller our model is the return statement but we could have created model classes to organize and process the data we return our controller is the greeting controller which maps the greeting statement to a given route in this case slash greeting the rest controller annotation actually does two things for us it combines the act controller and at response body annotations this allows us to just return the string data rather than a full view itself it allows us to easily create a rest api when people say they use spring boot they often mean they use the spring boot application annotation in their main class to set up a bunch of stuff behind the scenes this makes their application easier to create and maintain otherwise there would be more code we'd have to write so if you want to create a quick rest api microservice using java spring boot with spring mvc is a pretty good bet i've recently gotten into spring and there's so many more concepts than we've discussed here but this should get you started with the foundation if you're interested in learning more spring concepts such as dependency injection and component scanning java beans or even what some of the annotations are just leave a comment down below because the next step after learning java is to learn java spring and java spring boot happy coding i
Info
Channel: blondiebytes
Views: 171,584
Rating: undefined out of 5
Keywords:
Id: YDRNMAJo0MA
Channel Id: undefined
Length: 10min 32sec (632 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.