Spring Boot Tutorial with Project | Java Development Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel another new video now if you've been following the channel for a while you know that we have done some pretty popular Java boot camps that have been watched by so many people and one of the questions I get quite a lot is how do we get started with development in Java so if you don't know my first open source contribution was in a Java project the kubernetes Java cined by red heart middleware and if you're someone who wants to get started with your open source Journey or just want to build production ready applications websites games or whatever you want to do using Java this video is for you so in this video we'll make a full- flesh application tutorial and we'll use spring Boot and we'll learn about the spring framework the features and we'll build a nice little website using spring Boot and my SQL because we need a database you'll be like what about databases we want to store stuff you know for our website so we learn about my SQL database integrate that into spring boot easily it's going to be pretty amazing and also we'll learn about build tools if you're working with development in Java you will come across various build tools like Maven and gradal we'll learn about that in this tutorial as well so nice little project we'll do that we'll set the foundation for some of the future videos I want to do in the Java development Series be that on Kafka and other things and if you have any questions make sure you leave those in the comment section below all the resources for like the MySQL platforms and everything the Java code all the things can be found in the description below so make sure you check out the links in the description below and uh you know if you want to try these out yourself and let's get started okay so let's get started first question is what is spring boot Well Spring boot is essentially a framework to build web and Enterprise applications in Java and the way it's designed is it that it makes it pretty easy with minimal configuration to create production ready applications so your development process is basically simplified because it provides a set of tools and templates for you to use so that you as a developer can focus on on writing the application code rather than dealing with setting up you know various configurations for so many components so in simple terms if I mention then um spring boot you can think of it as like a ready to use toolbox for building your Java applications so as a framework it comes with various tools and you know conventions that streamline your development process so one of the most popular Ways by which you can use it is if you want to create let's say web applications or a microservice infrastructure or something like like that spring uh boot is pretty popular for that and some of the most popular tasks are like HTTP requests managing databases and setting up various like configurations and managing dependencies and all these other things so all of these things that we just mentioned that spring boot is pretty popular with I'll try to incorporate it in one single project in this video so you'll get a nice little idea of uh you know the power of this tool and how you can get started and explore now one thing I want to mention is when you're starting with an open source project let's say a really big project project or learning a new technology or getting into a new domain of development try not to be an expert when you're just getting started obviously if you find interest you can then be like okay now I have learned some basic stuff and made some projects and I know what it's all about what the community is like and now I can you know deep dive and become an expert or get my certifications and stuff so I'd say for example in this video if this is your first introduction to Spring boot i' recommend have fun okay have fun be curious explore and if you want to contct to open source then you don't have to be an expert just learn enough to contribute if I say this like it's a big statement but after watching this video it is enough for you to contribute to Java open source projects because you will get stuck and you will find newer and newer things you'll be like okay this another project is using some other thing you know that you like quarkus or something you know that you may not ever have heard of so you'll be like okay that's fine um we started with spring boot we did not know anything about it so we just got started now we are getting started with this another project project and they have some new technologies being used no worries we'll learn and contribute while on the go this is very very important because otherwise people get overwhelmed so to summarize learn on the go learn while contributing that is what I would like to mention before we get started okay speaking of which what are the prerequisites in order to get started with spring boot what are some of the basic knowledge that you need to have well Java first of all but let's look at some of the other prerequisites okay by the way I'll leave all the links in the description below for the prerequisites the first one obviously is Java development kit because spring boot it's a Java based framework so you need uh the jdk installed on your system this is a website you can find the links in the description below for all the resources to get started and um it is always recommended to use the latest uh you know long-term support version of uh jdk and uh you can see that Java 21 and 17 are available right now 21 is the latest long-term support release for Java so I'd recommend you can use you can use that next thing you need is an integrated development environment or an IDE intell idea is a good one for that it's the most popular one and um you don't have to use it it's not like you you have to use this only um but uh this is the most popular one that is why I'm recommending it as an IDE and also it's free so you can use the community versin one you don't have to pay or anything you can use the free version should be more than enough for you to get started all right you also need build tools so you can use Apachi Maven or gradel because this is how our projects are going to be built so you know how I mentioned about uh various dependencies and configurations I'll talk more about the spring boot features in a minute but uh in simple language you can just tell uh your build tool like hey I'm making this project I want my SQL installed or something like that so in your XML file you can just add in my my SQL when you run the build project it will do everything for you it's pretty simple so Apachi Maven you can just download it it's very simple just download this folder okay un unzip it and add it to your path that's it just add it to your path and also set the Java home I'm not going to show it in this video because I've already covered it and I will tell you where I have covered it so the Java introduction to Java installation video in that I've covered how to set up Java home and how to so if you see you can just download it that's it and then you can go to install instructions you you can add the bin directory which is in this folder that you unzip there will be a bin directory so it will be Apachi Maven 3.9.5 /bin just add it to your path environment variable how to work with environment variables if you let's say that new that you don't know what path variable is environment variable is don't worry I've explained this as well in my Linux terminal commands tutorial that is very popular if it has crossed a million views in that I have showed you some important Lin commands and how to set up path and environment variable so you need to that do that as well okay next uh the spring boot you know framework itself uh you can uh install it as a you can include it as a dependency in your build tool so it'll install it so we don't talk much about that but just in case you're interested this is what the website looks like you can learn more about it in detail supports and samples and the spring framework so you can see you know spring for graphql Spring Security data so many other things so that's basically about it you can learn more about it on the website um I'll try to make it as easy for you to onboard uh and use these applications because we're going to be using a bunch of stuff you know web we'll be making a website we'll using spring boot uh MySQL and you know May and all these other things so don't want to overwhelm you that much but it'll be fun now lastly we're going to use a database so an open source data platform that you can use is aan again the links you can find in the description below we'll be using my SQL uh but you can see they have a bunch of stuff that that they offer you can see even for Apachi Kafka for Apachi Flink and for databases if you want to use there's MySQL there's redis there's postc SQL Apachi Cassandra is there and then there's for analysis open search influx TV grafana is there so many other providers and you can also deploy it using like kubernetes infrasture score like terraform thirdparty Integrations so you can see the compliance and U some of the other features like high availability and things like that I'm recommending this because it's um you know the pretty good in terms of like the open source support and the ecosystem it's very vast and uh pretty powerful it's like fully managed you're in control of your data because it's power powered by open source services and basically supports all the providers and all the toolings and it's pretty simple to get started with so we'll be using aan and uh yeah if you want to check out the links in the description below you can find the links to the platform you can use my link and you'll get some um you know you can get some free credits and stuff but yeah for that you can check out the link in the description below and that was about the prerequisites so I'm very excited you can see bunch of stuff we'll be using but it'll be a lot of fun and um yeah now one more thing I want to mention is about the spring framework now you may be confused okay Kunal what is the difference between spring Boot and spring framework and do you need the knowledge of spring framework in order to get started with spring boot short on answer is no it's always good to have but not really you don't have to know that much like in expert like what we'll be covering in this video is going to be more than enough for you to get started but the real question the doubt you may be having is spring framework versus spring boot what is the difference so both the spring Boot and the spring framework it's like the it's a part of the larger spring uh whole ecosystem and um when we talk about spring framework it's a as the name suggest it's a framework and it's used to build production ready Java applications and um yeah and as a framework it has a lot of features and modules for so many other things like dependency injection for your messaging your databases so on and so forth may you said the same thing for spring boot that is correct spring boot is actually built on top of spring framework but the thing is that spring boot has a lot of abstraction so in simple terms it will handle many things for you but with spring framework you have to do a lot of things manually like configurations and dependencies and all sorts of things you know the quote I mentioned in the start like it will with spring boot you only focus on writing your application it will take care of everything else so spring boot spring framework it's not like a versus thing it's like spring boot is built on top of spring framework you can use either of those to create production ready applications and all sorts of things but spring boot makes it a little easier for you to do it if you want more control if you want to do everything by yourself then you can use spring framework but ideally what is recommended outside in the real world as well is spring board because think about it right you don't have have to write the configuration code yourself so like dependency management application you know like configurations and packaging and all sorts of things spring W will take care of it and um you know because spring boot is taking care of it you can focus on writing your code but if you're using the spring framework itself that's like more more control you have so you have to do everything manually so if you want to develop web applications and microservices where Simplicity is of you know priority to you and you just want to get your stuff out I recommend using spring boot because will abstract away a lot of things offer you some boiler prate so you can focus on writing the business logic rather than worrying about all these other things but if you're like a really big Enterprise you need fine grain control over your applications and configurations and stuff you have a lot of resources and all these other things then spring framework is the choice to go I've also seen people use both of them simultaneously so they use spring boot to boost strap the application pretty quickly and then they use spring framework to offer like more customization and more complex okay so let's get started with a simple project I'll explain to you you know the components and stuff and all the the tools and like everything um while we are creating this project just so that you know how simple it is to get started with first thing we have to check if we have Java installed looks good Maven installed okay looks good so what we're going to do is we're going to create a new spring boot project you can do it manually you can do the command line like using mvn R type but we're going to make things simpler we're going to do simple stuff so you can use spring initializer I can select that the project I'm going to do Maven uh language Java this uh version is fine com. example demo demo demo is fine this is fine uh packaging jar is fine Java version this is okay and I'm going to add a dependency I'm going to do the web dependency so this is basically used to create um you know uh some basic web uh Web projects with uh with spring and um that's basically about it you can use uh I'll show you in just a minute you can add these dependencies and stuff manually as well all the things I'm doing right now using spring initializer you can do it manually as well but we're going to use spring initializer just so that it makes a life a little bit easier generate it has downloaded something let's see what that is so here you can see demo. zip has been downloaded it has been extracted not a problem I will open intellig idea and I will open a project which is the demo project over here desktop downloads demo open let's see what it has created for us so we have a source file which has let's say main Java this is the main file not a problem we have a pom.xml file this is the main file I want to show you uh we don't need the build right now it may look overwhelming but it's really not okay so don't worry I'll explain you what these things mean so this is my main like this is a demo application Java this is just normal Java file not a problem I'll write my code here this is fine this is a structure what it looks like the do Maven over here and um this is the test folder where all my tests will go pom.xml so pom.xml I want to share with you what this is all about this contains information about my project so like what all dependencies are in the project so let's say we have the spring boot starter web right so this is what I meant it's going to go to this let's say URL or website or whatever online thing repository and it's going to download this dependency this dependency may have some files for us that you want to use similarly if you want to use MySQL you will add a dependency for my SQL over here so that you can use my SQL and when I run the maven command it will work with it and it will download all the dependencies automatically and uh it will like I don't have to manage it Maven will manage it and the simple stuff like version I've given the name of the project right description of the project properties mean which Java version you using um the version of Maven and all sorts of things okay just the XML version of this file and coding and uh stuff most of these things you can ignore right you don't have to add these in but uh since it has created it for us automatically so we don't have to worry about it it's fine okay so this is what I want to mention to you now one more thing I want to mention is here you can see M Maven directory you will have all the dependencies like properties here you can change the MAV path you can go to your settings and you can go to build execution deployment build tools Maven here you can see using the MAV Maven rapper you can point it to in built one or you can point it to the one that is like the one you downloaded everything is okay looks good okay I'm trying to make things easier for you so for simple stuff like right now what we have just done is we have created a simple simple project it has a simple dependency that we have installed and we'll go to the terminal you can write the Mayan installed mvn install command and all sorts of things but what we'll be doing is we'll be using intellig ideas inbuilt like this Maven tool thing over here so you can check out your profile over here all the life cycle you want to clean you want to test you want to install you want to deploy just with one click it will do everything for you see just with one click did everything build success one click so you can use mvn install command as well but I'd recommend using the GUI when it makes life easier so why not and you can add in your own commands like we'll do over here shortly when I'll run the project but what I want to do now is Kunal this is fine and U this is just a simple boiler plate to run the application but we want to display a hello world let's say we do we run our web application on Local Host we go to /hello paath and we want it to show hello work world how do we do that springboard there's such thing known as a controller so controller is used to handle HTTP requests create a new file I'm going to call it hello hello controller and I will just paste my code over here simple code so this is what it looks like an annotation like rest controller if you go over here this is a spring boot application annotation so you learn more about annotations I've already covered it in my Java boot cam but I can say spring Boot and anotations okay so it's form of metadata that provides uh know data about a program annotations are used for supplement information and there are some uh core framework annotations as you can see over here configuration class level annotation spring boot like a component the controller so the controller is again a class level annotation which is used to handle HTTP requests and repository spring boot application this is a combination of these three annotations and it tells us that this is a spring boot application so many annotations are available over here okay so similarly we using a controller annotation it's telling us that hey this is actually defining like okay this is uh this file over here this class over here is used to handle HTTP request okay and as you can see I'm importing this class from where from the dependency that I inst installed so automatically these classes the code that has been written in the dependencies I am using it thanks to Spring boot I'm adding another annotation over here get mapping where I just add in if you want to check get mapping what it looks like as you can see over here get mapping and this is again in the this package which is in the that I got from I installed by m so it's saying when you go to the hello URL return this thing that's it run this function if you have done python sort of like decorators you know like in flask not exactly similar but if want to talk about in layen terms that's basically about it yeah create a controller add in a URL get mapping if you make a get request to/ hello it will return hello world simple have we done anything complex over here right now no I just created a project using the web UI I imported it it did everything for me I don't have to worry about you know every single thing in detail because I just want to focus on getting started and making projects these things just basic information about the file don't worry about it the main thing I wanted to show you was dependencies and here we have automatically added one dependency because we are using the stuff that is in that dependency see rest controller get mapping we're using all these things because we have added in the pom.xml and now we can run this let's see how we can run this okay now that I have everything set up I I can go to Maven over here and I can see my demo project and I can run my Maven command we can do it from um terminal as well but I can just say Maven spring boot run enter you can check all your dependencies and stuff over here as well plugins and the life cycle you can run directly commands from here like if you want to clean your repo you want to test do all sorts of things I think that's running now started the application and it's running so we can go to Local Host and see if we go to Local Host SL uh hello it should return hello world Local Host 8 hello hello world we have just created a web application using um Java that's it so simple right let's do a nice bigger project with like my SQL and uh see how how these components work and do a nice little project it will be making more sense okay so that was how easy it was to get started but now let's look at a more real world project we'll use a database and connect it with a spring rot application and it's going to be pretty cool so first things first we need a database so we're going to use a it's very easy to get started with you can go log in for free and you can try it out for free you can use the MySQL one for free as well uh for the free plan so I can just go select uh a new service there are a bunch of available over here I'm going to select my SQL okay I'm going to select the free plan and uh when I select the free plan I'm going to select uh the region so I'm in Europe I can select uh Europe I'll select uh England that's fine and free one that's okay and I can just give it a name I can say my [Music] SQL whatever name I can give it so I can select the name I can say my SQL Kunal and that looks good uh what I'm going to do now is I'm going to say create free service cool now if I zoom in you can see in my portal in the console I can see my SQL Kunal it's building and when it's built you will see that it's like turned green and here I have all the connection information this thing is is important because we'll use it to connect to our application but you can see how easy it was simple it took me like 30 seconds no less than that right so pretty easy to create a MySQL you know database over here in aan and um yeah free for you to use as well so that's pretty much about it very easy so now the next step is we're going to create our spring project so same thing we're going to do we're going to select the language Java I'm going to select uh Maven and uh the version we can select is uh 3.1 um five that's fine and the group name I want to give is I can say IO do Avent do spring do MySQL artifact uh demo name is demo description I can say spring Boot and um my SQL aens spring MySQL do demo jar 17 is fine and then dependencies that I need to install so spring web is here which is okay we need the spring web bun but we are now working with the database as well so we need spring data jpa so persistent data in SQL stores we need that one and we also need for my SQL my SQL driver don't think anything else is needed so I'm just click on generate and we have our file over here demo. zip I can open it in intell J idea open open same thing as before now we have see our dependencies here amazing pretty cool stuff so we have my SQL dependency spring boot starter web all sorts of things okay cool Source main Java demo application just the same thing as before all right now let's see how we can connect my SQL to it back in my Avent dashboard here I can see in my console that it's running so that's pretty easy and it was free I also didn't add any credit card so you can get started without that if you check out the links in the description below but uh what we need to do now is we have to add this even uh like the nice little MySQL um you know database that we have to our local project over here how do we do that when we talk about springboat it by default comes with the H2 database uh that's a default database if you're using a cloud service like this we have to add the connection attributes so you can see we're getting so many information over here like the password and stuff so one of the ways by which you have to connect the cloud based platform to your local repository is under this resources section okay and here I will go to application properties right so in the application properties I'm going to add in a bunch of things let me show you so here I have ADD added the uh hyper ddl auto update and the URL the username and password now you'll be like okay Kunal these are this these things are fine I'm saying this is like you know a specific stuff so I have to add the aan um data that I have over here from my uh connection information into it so if I talk about my database then this is the database name default DB I'm just going to add this over here my host is this one so I'm going to copy this and I'm going to add in my host over here and my port is 14503 I'll add in my port over here username and password is needed so username is this and password I'll just copy don't try to copy my password because I'll remove it after the after it's over and everything else looks good so that's one of the ways by which I have added all my Aven information for my cloud database that is running with my local repository so thing is it's pretty much boiler plate and I just added the information that is in the website over here for my connection and now we can connect it one more thing I want to mention is you have to add this value To None it's like the recommend Ed way to do it after your database SCS into production okay now in my SQL we need tables so we need to have tables and we specify some of the properties of the table some of the fields how do we do that so we have to do that uh using like our Java code and one of the things uh we can use is because we have installed these dependencies for my SQL there's an entity we can use so I'm going to create a user file okay and I'm not going to write the code like like line by line because the video will be too long but I'll walk through it and I'll leave the description uh in the description below I'll leave the code and uh so that you can check it out yourself and you don't skip on anything so the whole idea is that when we're using hibernate it's going to translate this into our mysq uh database right so as you can see it's an entity of public like class user ID generate value so you can see it has an ID it has a name it has an email and some functions just get the ID uh get set the ID so Getters and Setters if you have seen the object in pring playlist for the name and for the email very simple so entity as you can see uh available over here and uh that's pretty much about it cool so hibernate is going to use this and going to create our my SQL table now you can be anything you can have like uh you know if you're doing a project on let's say movie recommendations so you can have like movie name movie review and and things like that but we're going to use a simple user which has an ID name and email for example you have a group of classmates or role number marks and stuff like that you can add in one thing I didn't mention is what is this uh strategies equal to generation type identity this is how you set primary keys so even for my SQL does not allow creating tables without primary kees for security reasons that is why we have added this over here the next thing we're going to do is we're going to create a repository uh interface so that it will hold all these users for us so I'm going to create a new Java class interface I can call it user repository it's going to have this information over here just going to say import this user class extends the cred repository of uh we have our um you know the types over here user and integer this is basically going to be Auto implemented by Spring they're going to take a look at this um interface and uh CR if you don't know what it means it's uh for create read update and delete operations the next thing I want to do is now I want to handle HTTP request and you know what we have to create for that controller so I'm going to call it um main controller it's going to have this um file just added some URLs over here now let's go through it uh again I'm not going to type individually every line because it'll be timec consuming and since you're watching a recorded tutorial you can uh directly jump on what I mean and again the code and everything is in the description below so this is again an annotation it means that this class is basically a rest controller so a class level I'm telling what this class is all all about and here as you can see we have uh the path as demo it basically means the URL starts with Slash demo and uh this um thing over over here the user repository user repository is basically this thing the bean that starts with user repository so automatically spring will convert this into smaller user uh smaller case user repository variable so this is autogenerated by Spring and this is what we're going to use to handle our data now we have uh the post mapping and get mapping what this basically means is that this is only for post requests over here and um let's see what this annotation basically contains so it's pretty simple stuff now we're adding a path ad B just for post mapping so this is our post request we're going to create a new user so new user set name set email and then save it in this um you know variable over here and then return it that's is why we have the response body over here meaning that we are returning the type um user which is a response and the request param over here is the parameter um from like the over here from like the get or post request okay similarly we have the mapping and uh this is going to return uh uh response body so it's going to return a Json or an XML with all the users that you want to get so user repository. find all all the users and get all so if you go to this slash all URL it should list all the users if you go to slash add URL you should be able to add a user so we're adding a user and um SP much about it just 20 lines of code all right so controller is set let's move forward okay now we need to create an application class uh we already have one over here demo application and um it's already been created so not sure if you have to add anything else over here we did already know we already covered it this is like the entry point of the spring boot application we saw about this annotation that it consists three other anot notations which is the configuration enable auto configuration and component scan and um you know this is where the application starts like this is the spring boot application entry point uh so all the component scanning and uh configurations and everything that the auto configuration mechanism that happens um and it's the entry point of the application if you will this is like the simple boiler plate like how we just started and this main method that we have over here it's going to use the spring applications run method which is going to be used to launch the application okay and this entire application we have just built it has been built on Java so you don't have to worry about any underlying like any other dependencies or infrastructure problems or as such anything like that I think that's pretty much about it uh I think we can run the application and we can uh just summarize this is where it stores all our users in our interface and um this is the user type so it will create a type of this main controller which is for URL so I have two over here which is ADD and all and let's try to run this let me run this I'll just go to Maven um spring boot run so simple it is application should be up and running now started application that's good I can uh go to my project I can say curl Local Host 880 demo all it should show empty right now that look looks good and I can do the same thing I can add in a new user so demo add and I can add in some names I can say Kunal kushwaha email is equal to I can add in Kunal at xyz.com that's it so one person has been added now I try to get all the people who are added so it's showing me one person added we using the mySQL database in cloud and it looks good I can add another one Rahul Rahul R let's see if it shows both Kunal and Rahul now yes Kunal Kush with id1 and then Rahul Rana how cool is that nice so that's basically how it works and we're using our online uh you know nice um my SQL how easy it was to get started with aan and we learned about spring boot we learned about all these files controllers I think the next step would be for you to explore play around with it I'd say explore the aen platform so if you go to event home over here and you create a new service I'd recommend play around with it you can check out the links in the description below if you need some like free credits and stuff and um yeah create your own projects I'd say try post Grace uh we'll do some if you want me to do videos on any one of these let me know in the comment section below you can do Apachi Kafka and grafana I also recommend playing around with initializer try not to you know many times people think okay if we have more control and stuff we we deliberately try to make things complex we will learn effectively no these are just tools enough for you to get your work done for you to contribute to open source build projects if you do run into an issue like always never skip the basics is what I say tools keep on changing you know new library Frameworks keeps on changing I recommend also checking out the spring boot um you know documentation to learn more about it to learn more about annotations and stuff going to learn more about these object and pring Concepts you can watch my object and pring playlist but uh lastly I'd just say yeah one more thing I'm happy to give you some exclusive uh swag so if you learned something from this video you can use this uh entire tutorial follow it step by step and create your own project that's step number one you can use aan all the links can found in the description below and share what you learned you know share the project that you created on socials tag a tag V make devs you can tag me and we'll select a few winners we'll give you some exclusive swag and uh it'll be pretty cool so if you want to win some nice swag you want to join the learning public initiative we learned from something from this video I'd recommend build something quickly as you can see it was so easy like 10 minutes you can make something share it on socials tag a tagv make devs tag me and wait to get your swag if you're lucky but yeah lastly any questions or any requests for further videos I know people were requesting Kafka tutorials let me know in the comment section below I'm happy to do that final reminder all the description uh links are to the resources that you can check out um yeah have fun and also find open source projects I'll make another video on Java open source contributions more like a guidance video but till then have fun play around with it take part in the giveaway and I'll see you in the next one have a great day bye
Info
Channel: Kunal Kushwaha
Views: 127,908
Rating: undefined out of 5
Keywords: spring boot course, spring boot tutorial, spring framework tutorial, java spring, learn spring, learn java, java development, mysql and spring boot project, maven tutorial, java open source, red hat
Id: aS0t9HTO5V4
Channel Id: undefined
Length: 38min 23sec (2303 seconds)
Published: Wed Nov 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.