Learn Spring Boot in Simple Way : From Beginner To Advanced

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we are going to understand what is a springboard how to create spring boot application internals of spring boot how to work with Banner in the springboard how to develop rest API by using springboard how to generate Excel report using springboard application how to perform thread operations with the database using data jpa how to dockerize our spring boot application how to deploy spring boot application in the AWS cloud and how to connect with AWS RDS database using springboard application before going to start let me introduce myself my name is Ashok I'm having 10 plus years of experience as a Java full stack developer let's get started what is springboard springboot is one approach to develop spring-based applications with less configurations you can develop your application by using spring framework but you need to write configurations on your own to avoid that configurations boot came into picture by using spring boot we can create the application that you can just run that configurations will be taken care by Spring boot spring boot introduced form starters dependencies they have simplified to develop one web application you no need to add multiple dependencies one single pump starter web starter is sufficient if you add the web starter it will give you all the required dependencies to develop and run one web application Auto configuration in the spring framework as a programmer we need to do the configurations manually what are the bins available start the ioc container create the bin objects and all but in the spring boot Auto configuration introduced it will take care of the configuration which is required for our application how beautiful it is next one embedded server whenever you develop a web application to run that application we need to deploy that into server we need to download and set up that server and we need to package our application and we need to deploy our application here in the spring boot embedded server concept available that means spring boot is providing a embedded server to run our applications directly you no need to do any deployment in the external server when you go for spring boot it is having a internal server that is called embedded server or embedded container spring boot is having lot of demand to develop the web applications in the market nowadays [Music] right to build our first application with the spring boot what are the prerequisites so as part of this application creation I will be using Java 1.8 version and I am using spring tool suit IDE and Maven I'm going to use as a build tool so you don't need to worry much about them so in the future classes we are going to see about them in detail all right so in order to create our first application I am going to use spring initializer website so here so start dot spring dot IO is the URL to open string initializer right so using this website provided by the spring we can create our first application based on springboard so Project based on what built Maven or Gradle support is there with the maven and Gradle so I am choosing Maverick it supports info it's supporting for three programming languages Java kotlin and groovy all these three are going to run based on the jvm so here I am choosing Java and springboard so which version of the spring boot you want to use so if You observe they are only Choosing by default 2.2.6 is the latest stable version the remaining versions are available but they are still under snapshot right so stable versions We are choosing which is 2.2.6 and project metadata so you can give so here I am giving the project details com Dot codingtips [Music] right artifact ID what is the name of the project that you want to use zero one first spring boot application right so description what are the package name that you want to give so here I'm giving package name as com.codingtips and what is the type of the package that you want to use if you want to go for Standalone then choose jar so if you want a web application then go for war so in the boot web application also can be deployed in the jar format because Boot execution will begin from main method and here I'm choose we will talk about that in next classes right here I am choosing Java version at 8 and here we have options generate this project explore this project or share this project so here I am choosing an option explore so how exactly that project is going to be created it is giving the outline so here it is created one form.xml file in that so boot parent starter is available with version number we have chosen right and one dependency spring boot starter so this is called as starter form which is required to run our boot application and go inside the SRC so here uh whatever the package that we have chosen right in that package they created one class which is application.java this is called start class of the spring boot spring boot application execution will begin from this start class only people will call it as main class also the spring port and under the resources on properties file got created application.properties so here we can write our configuration properties like so if you want to connect with the database database configuration properties we will write here SMTP properties we will write any messages that you want to use in the application we will write in this properties file so in the form of key and value pair we can write config properties in this application dot properties file and test class also got created so to perform the unit testing so we can use this test class unit test we can write inside this test plus this is the total outline of our first spring boot application right now let's download this application so which we created this application by using start.spring.io website this is called Spring initializer to create boot applications then click on this download button then one zip file got downloaded right go to that jeep try to extract this [Music] extract through this folder the zip file I extracted right so this is the project that we created in the inside the spring initializer so take this project URL up to the location where com.xml file is available so here our com.xml file is available now so here I am choosing this path I copied this path now go to our IDE now so as it is a maven project I will import this Maven project into IDE this is spring tool suit IDE file import existing Maven projects paste the path which we copied of our project which we downloaded click on this browse so that it will point to that palm.xml it will identify the form.xml file then click on finish foreign [Music] this is one way of creating our boot application so here I used spring initializer website we can create that boot application directly from our IDE also right so that is the second way that we can use so first let's complete this process then we I will show you the second way also to create our boot application so it is taking some time to import because this downloading some dependencies which are required because this first time we are importing this project with that version [Music] yeah now this project got imported so this is our application that we downloaded from Spring initializer website so this is Maven project folder structure because we have chosen Maven as our build now let's explore this so this is our pom.xml file where boot starter parent dependency will be available and would start a dependency also will be available this is parent starter and Java 1.8 version we have chosen and boot starter dependency and by default test dependency also coming for our application right now SRC main Java main resources test Java is available in Java application.java class we have seen in the website so this is called as stat class which is entry point for our boot application properties file is available so where we can configure our configuration properties in the form of key and value pair and test class is also created to perform unit testing this is based on Spring boot best test fine so with this we are able to create our application let's try running this application once right click run as boot app if application runs successfully we should be able to see Banner Banner logo here like perfect so this is called Spring boot Banner so this printing the version number also and application contaminated so successfully we are able to create our first application based on the spring Boot and we are able to run it also [Music] when we create boot application by default on class is getting created that class is called as stat class or we can say that is main class in the spring boot application so in this class we can see one annotation that is at the rate spring boot application what this annotation does in the background this boot application annotation is equal to three annotations right they are at the rate configuration at the rate enable auto configuration and at the right component scan this one annotation is equal to three annotations right what these three annotations are going to do for our boot application first one is at the rate configuration configuration annotation helps in Spring annotation based configuration suppose you want to configure some pins you want to configure a data source you want to configure some other components that are required in our application for that we can go for at the rate configuration annotation this is class level annotation so to represent one Java class as configuration class we will use this configuration annotation right now inside this class we can write the beams also suppose I want to customize instantiation process of particular class right for example rest template I want to customize the object creation for the rest template class for that we can write a method and we can annotate that method with at the rate bin annotation right so earlier in the Legacy versions of the spring framework people used it to follow XML files approach to perform the configuration whereas from the spring 3.0 on Watts we have the freedom to move out of the XML files how can we avoid the XML files by using spring Java configuration approach so to perform this Java based configuration we will use this configuration annotation all right now next annotation at the rate enable auto configuration this is very very very important Concept in springboard so this enable auto configuration annotation tells spring boot to guess how you want to configure spring based on the jar dependencies that we have added so whenever when we are creating the project we are adding several starter pumps in the pom.xml right so based on those starter forms this annotation tells to boot to guess the functionality that requires to configure our application right now this annotation Auto configures the bins that are present in the class path so what is the advantage this simplifies the developer work by guessing the required beans from the class path and configures to run that application so if you add a starter pump in the pump.xml file then it will guess what are the components that are required to run this application for example so I added a dependency for H2 database which is embedded database so if I add that dependency in the form.xml file then this Auto configuration will guess that oh for this application H2 embedded database is required and it is going to configure that in memory database similarly if you add web starter in the palm.xml then this Auto configuration will guess that for this application Tomcat container is required to run so it is going to guess the components that are required to run our application so how it is going to guess what are the components that are required so let us see a simple example for that so if you go for your boot application here we added the dependencies in the pom.xml file so these dependencies are Maven dependencies right now so go to the project here we can see map and dependencies available so expand this map and dependencies here we can find one jar with a name called springboot Auto configure right expand this jar inside this jar we are having several packages now go to Don here we can find one file within one folder with the name called meta iPhone INF right expand this inside this we can see one file with the name call Spring dot factories now let's open this spring dot factories file I'll write in this file we will see what are the components that are loading by Auto configuration so how it will guess these are the configurations are required it depends on the dependencies you configure in the form.xml right fine let us check for jpa jpa repositories Auto configuration ldap Auto configuration Auto configuration radius Auto configuration like the context listeners logging configuration property placeholder configuration so what are the components that are required for our application that will be available as part of this spring dot factory spy that file we can find inside meta INF folder this folder is available inside our Auto configurer jar which is available inside our Maven dependencies so I hope you understood what is this Auto configuration Auto configuration is used to configure the components automatically which are required to you may get a doubt how boot will understand what components are required to run this application boot is going to read our form.xml file what are the dependencies that are specified in the form.xml file based on that it will configure the things that are required to run our application right next one component scan this is also very important component right in the springboard so what is the purpose of this component scan the components can tell spring to look for other components configurations and services in the specified package right in our application we are going to create several user defined components some predefined components will be coming in the jars which we are adding in the form.xml file and several other components that we are going to create like some controller classes we will create service classes will create and some repositories also we are going to create then how boot will understand which class should be considered as a spring bin for that purpose it will use component scanning all right so spring is able to auto scan detect and register your beans or components from predefined Project package so whenever you create a classes inside your package boot is having the capability to perform the scanning on your packages to register those classes as spring pins right if you don't specify any package then which package it is going to scan it will consider base package where that spring boot start class is available so whenever boot project is created one class is getting created inside a package that package will be considered as a base package so spring boot is going to scan that base package and sub packages of that base package also so let us see this here we have a project inside that we have one class application.java this is start class of our springboard so for this class we specified boot application annotation so you can go inside this annotation and we can see that this annotation is equal to boot configuration enable auto configuration and component scan just now we discussed about these three annotations configuration annotation to represent this class as a configuration class right Auto configuration based on the dependencies added in the build path it is going to identify what components are required to run our application and component is scan this component scanning is used to understand which classes it has to load and consider them as spring beings right now here I did not specify any component scan then what will happen boot will consider this package as base package so it will go to this package it will check for the classes and it will load those classes and will register them as spring beans so there are some annotations to represent our class as a spring bin for example let me create one class here foreign getting loaded by Spring boot or not if I click on this package new class so here I'm creating class with the name called car right now I will add zero param Constructor for this class to see whether object is created or not for this class right zero param Constructor is added just I am writing one sop statement for logging on the console cease out car Constructor right now run this application [Music] run as boot app so application execution will start from the start class where we have at the right spring boot application annotation and the main method will be called inside that we are calling spring application.run method Now application started and application got terminated also are we able to see that message on the console which we have written in the SOP no the reason is this class will be considered as a normal Java class because we are not representing this class as spring pane now here I am using one stereotype annotation to represent this class as spring beam that is at the rate component so component is predefined annotation in the spring framework which is part of org.spring framework dot stereotype package right now run the same application again run as boot app run as boot app [Music] now we can see the console Banner got printed and car Constructor message is also printed that means now this class is loaded by Spring boot as part of component scan did I specified which package it has to scan no that is built-in functionality in the springboard because there is a annotation boot application as part of this annotation component scanning will happen so what is the proof it is going to scan other packages also let me take one more package as a sale package for base package best package.com.codingtips com dot codingtips dot service right inside this I am creating one class new class car service right now let me represent this class also as a spring bin by using at the red component right I am writing zero param Constructor this is out car service Constructor if this message is printed on the console then we can say this package also scanned and loaded this class and recognize it as a spring pane right click the back project run as boot app now we can see car Constructor got executed car service class Constructor also got executed so with this we can say component scanning is built-in functionality as part of the spring boot application annotation so I annotation so it is considering this package as base package and this package will be considered as chilled package for the base package because it is following base package naming convention now let me create one more package [Music] without following base package naming convention simply I am for writing com dot da com.devo right click new class here I am writing a class with name called cardi Evo let me represent this class as spring pin at the red component foreign Constructor sis out car davo Constructor right let's execute this run as boot app [Music] now you can see only those two classes got executed right this card bio class is not executed because package naming convention is different so what is the best package the package which contains the start class will be considered as base package if the remaining packages also starts with the base package then those packages will be considered as chilled packages for the base package and those packages will participate in the component scanning com.do package not following base package naming convention hence this package will not participate in component scanning process right so I hope you understood what is the purpose of at the red spring boot application annotation right so spring boot application annotation is very important annotation in the spring boot applications right this one annotation is equal to three annotations at the red configuration at the right enable auto configuration at the red component scan at the red configuration is used to represent our Java classes as a configuration class that is used to avoid XML basic configurations enable auto configuration annotation provided to run our application application annotation is used to scan for the classes to represent them as a spring base [Music] here I am going to create one boot application using spring tool suit ID so first let's complete this process file new project here I am choosing spring starter project to create boot application clicking on next here we need to provide several details name of our application what is the build tool that we want to use I'm using Maven packaging type jar Java version 8 language Java group ID artifact ID and package name I'm giving the package name as com.codingtips clicking on next here we need to choose boot version so it is defaulted to the latest stable version which is 2.2.6 I am not selecting any dependencies because by default it will come with spring boot starter which is sufficient to run spring boot Standalone application so I'm not selecting any dependency clicking on next here full URL is prepared to download this project from Spring initializer website so as we are using toolsoot IDE to create the project we should have Net Connection for our system otherwise this is not possible right clicking on finish now our application got created I'm expanding this application so we can see the project folder structured so it is based on Maven build tool so Maven folder structure we can observe here com.xml file where we configure our dependencies SRC main Java where business components we will write here configuration properties we will write inside applicational properties file unit test classes we'll write and set this test package right fine so let's open this com.xml inside the pom.xml we can see boot parent starter is available and inside the dependencies section we can see spring boots start at a dependency this dependency is downloading all the required dependencies which are required to develop our boot application fine [Music] now I'm opening the preview I'm opening the class which is created by the boot which is application.java this is called as stat class this is the main class in our spring boot application boot application execution will begin from here only so inside this class we need to understand two things one is at the red spring boot application annotation and another one is spring application dot run method so in this video we are going to concentrate on Spring application dot run method so this spring application is predefined class available in the spring boot this run is a static method which is available in this spring application class so what is happening inside this run method why we need to call this method if I don't call this method what will happen so if we can understand these details then we can talk about how boot application will execute internally right first let me run this application and see what is the behavior right clicking on this project run as spring boot app so we can see in the console spring Banner got printed and some logs also generated and finally application got terminated right now I am running the same application by making a small change I'm going to comment this spring application.run method right now again do the same process just run this application and see the behavior right click on this project run as boot app so remember that I commented that run method right this time application started and application got terminated but nothing is printed on the console that means something is missing so what is that so bootstrapping logic is not executed when we commented this run method so this run method contains some logic which is used to bootstrap our spring boot application so let's get into the details of this run method right so it is predefined Method so we can see the logic available in the predefined method also so simply I am pressing my control and keep mouse cursor on this run method so here we have option open implementation so click on this open implementation now we got into spring application class which is predefined this is available inside your package call org dot springframework dot boot so inside this run method is available so calling the run method and again calling this run method in this run method we can see the logic which is available you can see the logic which is available right so this logic we can say bootstrapping Logic for our boot application let's understand what is this logic right so at the 229 line number run method logic is getting started inside this they are using stopwatch this is used to identify the time how identify the time taken to bootstrap our application then they are trying to use configurable application context reference variable currently which is holding null value then they are creating one arraylist object which is used to store errors which occur at the time of application startup right then they are getting listeners which are available they are starting those listeners then they're loading default application arguments then they are preparing environment so in this method some logic is available right so they are getting application arguments and they are trying to prepare environment which required for our app Edition then they are trying to call Print Banner method this Banner method is responsible to print Banner on the console when the application started so we have seen spring logo is printed on the console that is called as Banner so in this print Banner method again some logic is available so you can off the banner by setting this banner mode to off and we can pass our own Banner also if we don't choose our own Banner then default Banner will be printed so in the coming classes we will see how to customize that Banner as well right after that Banner this is the main concept preparing application context three level line number three level line number they are trying to call one method which is called create application context this method is responsible to start ioc container to required for our application now let's understand if you are developing a project by using spring framework then you are responsible to start the ioc container right but in the spring boot we are not writing any logic this ioc container getting started as part of the run method now see how this ioc container getting started now I am opening create application context method in this method we can see switch case is available right so here the logic is getting executed to identify what class it has to load by using class that for me there are two cases available case Charlotte case reactive if not these two then it will go to default case in this default one more class is loading by using this class dot for name so why they are loading these classes what is the logic behind this these classes are responsible to start the ioc container for our boot application right now how it will decide which case it has to use it depends on the starter that we specified in our application on that XML right so here we are having a starter spring boot starter this spring boot starter represents it is a simple Standalone application suppose if you specify starter as a web then it considers it is spring boot web application using Charlotte if you specify web Flex it consider this application as reactive based application right so based on the starter that we specify this is switch Case Logic is going to execute so I'm reverting these changes I am keeping it as springboard starter if you choose springboard starter web then satellite case will satisfy then it will use this class this is the class it is using to start the ioc container class name annotation config satellite web server application context suppose if there is webflex starter in the combat XML then this reactive case is going to satisfy this reactive programming is New Concept in the spring framework which introduced in five point X version right per flux is the startup for that if this reactive case is satisfied then it is loading this class this class name annotation config reactive web server application context it is used to start ioc container if these two starters are not satisfying these two cases are not satisfying then it will go under default in the default case it is using one class which is annotation config application context it is used to start the ioc container that means spring boot is using three types of classes to start the ioc container so I think in the spring framework or whenever you work with the spring framework you will talk about beam Factory application context those two classes those are two interfaces which are used to start ioc container so similarly in the spring boot three different classes are available to start the ioc container so in our project we are using simple starter spring boot starter so it comes under default this class will be used right now here class dat for name which is used to load this class once this class is loaded there can calling one predefined methyl call instantiate that class that is available inside bin utils bin utils dot instantiate class for the class which is loaded object is going to be created as part of that object creation ioc container will start and this create application context method will return the reference of the ioc container which is representing AS configurable application context right so what is the return type of this run method the return type of the run method is configurable application context now go back here right so three line three 11 line number got completed that means ioc container will be started then Factory instances then it is preparing the context required for that then at 317 stopwatch got stopped with this entirely identify how much time it has taken to bootstrap our application then logger is getting started then Runners call Runners it is calling one method call call runners in the spring boot will have two types of Runners one is application Runner another one is command line Runner so we can write our own application Runners and command line runners in our spring boot application what is the purpose of these Runners we will talk in future classes if the runners are available as part of the run method the runners also going to be executed once that is done finally it is returning that context returning that context what is that context configurable application context I hope you understand what is the logic available inside run method so mainly stopwatch starting logic is available are getting the reasoners and starting those listeners preparing the environment printing the banner and creating application context then calling the runners this is the main logic which is available inside run method this logic we can say bootstrapping Logic for our boot application boot application execution will start with this logic only right so once all that logic is completed then control will come back to our main method now here application and method is returning configurable application context right we can store that I'm pressing Ctrl 1 by keeping my mouse cursor at the end of the line click on enter Then I got context if you want to get any beam then you can use the reference of this context context.getbin Method All right so mainly as part of this video we understood about what is this spring application tag run method spring application return method is very important method in every spring boot application that method will take care of boot application execution process execution process will begin from this run method only if I comment this run method whatever the logic we have seen this complete logic will not be executed if that logic is not executed bootstrapping will not happen in our springboard application [Music] Banner in springboard so as part of this discussion we will understand what is Banner in the spring boot how to customize that Banner content and how to disable that Banner all right let's get started so here I'm taking even spring boot application which we created in our previous videos now first I am running this application and I will observe how that Banner will be print right click on this project and run as spring boot app right observe the console so here we can see spring is printed this is called Banner in the springboard so this Banner is getting printed when our application is getting executed right how this Banner will be printed that logic is available as part of run method so let me get inside this run method open implementation going inside the run method going inside this run method right here we have a 310 line number print Banner so 310 line number print Banner is available so get into the this method right so here some predefined logic is available to print that Banner if banner mode is equal to off then it is simply returning null value Banner will not be printed so with this piece of code we can understand we can disable the banner also next one it is trying to load default resource loader by using that class loader then it is trying to print that banner and if a banner mode is equal to lock then it will print the banner only in the log file right so if the bearer mode is equal to off Banner will be stopped if the banner mode is equal to lock it will print Banner only in the log file if these two conditions are not satisfied then Banner printer.print on the system.out then it is going to print that Banner on the console so for the banner we can set three modes of log and console console is the default mode right now let's go to this application and go to this properties file so spring boot banner mode we can set in two ways one is by using this properties file another one by using programmatic approach so first I am preparing this property swipe string Dot Banner spring dot main mode spring dot main dot Banner iPhone mode is equal to control space three options are there console log off so if I choose console it will print Banner only on the console that is the default mode we no need to set that if I choose this as log it will print the banner only in the log file if we have a login in our project for the logging purpose right then we can go for this mode if we set the mode as off then Banner is not going to print on the console right Banner will not be printed anywhere just we are disabling that Banner so earlier we are able to see the banner logo and the console now I set the mode as off and I am running this application again right click run as boot app so the console right now application started and application terminated but we are not able to see the banner because the mode is off right now how to off this Banner from program right here I am going to the start class of the spring boot so here I am commenting this line right so here I'm going to use [Music] spring application [Music] bit I need to create the object for this application Builder so new operator new spring application builder of start Class Type application class Dot there is a method banner mode off lock console here also three options are there now I am going for off dot run method by using arguments so right earlier we are calling spring application.run method now I am using spring application Builder class for that I am calling the method banner mode for that I am setting the banner mode as off then I am calling run method right this is the way we can stop the banner from program right now let's run this application again right click run as boot app fine application started and it got terminated also Banner is not printing on the console the reason the mood of the banner is off now so with this we understood how to stop the banner from properties file and how to stop the banner using programmatic approach right now the next part is how to customize this Banner it is printing spring as the banner content I don't want to print that spring I want to print my project name or I want to print my company name as a banner for my application how to customize that so spring supporting for customized banners also it supports two types of banners one is text type banner and another one is image type Banner here I am going to demonstrate on text type Banner so to work with the text type Banner we need to have ASCII characters here let me generate ASCII characters I am going to one website where we can generate the ASCII character for the given string here I am typing my channel name coding tips for this ASCII characters get generated simply I will copy these characters you can find this URL in the description box of this video so you can also generate your own custom text right I'm copying this text I'm going to my project resources folder there I will create one file with the name called banner.txt new file name I am giving as banner.dxt this is the default file name and I'm pasting my channel name in the ASCII format which is coding tabs right now let's run this application again and run as boot app [Music] observe the console now instead of the spring now it is printing coding tips as the banner in our boot application all right so similarly we can specify image also right now suppose if I want to change this file name then you need to configure the property in the properties file spring Banner location so in which location your banner file is available that file location also we can configure by default it checks for the banner.txt file under the resources folder which cancels as a class path so I hope you understood how can we work with Banner in Springport [Music] foreign [Music] rest API by using springboard and we will test the trust API by using Postman what is rest API rest API acts as an interface between two applications to exchange the information if one application wants to communicate with another application then we are going to use rest API rest API acts as a mediator between two applications for example if you take any banking application Google pay phone pay and paytm net banking transactions we are doing so you are using Google pay with One bank account and I'm using Google pay with another bank account if you transfer money to my mobile number in the Google pay the amount will be added to my account and similarly if you try to apply for your passport it will ask your other number how passport application will validate your other number is correct or not passport application will communicate with the other application here if one application wants to communicate with another application we need a mediator that's where rest API comes into picture rest API acts as an interface between two applications to exchange the data here currently in the market lot of business applications will communicate with the third party applications are internal applications to perform several tasks let's take an example a software company wants to credit the salary to all the employees in the month and and this is a software company will send this employees data to Bank application so that bank will perform all the transaction salary credits so one application will communicate with another application to perform the operations if you take an example of train tickets booking hotels booking and online food deliveries they are communicating one application will communicate with another application for business purpose that's where rest API comes into picture here client application will be available server application will be available they are also called as consumer application under provider application one application will provide the services to other applications that is called provider application another applications will consume the services from the application that is called consumer application provider application and consumer applications will be available now we are going to develop one provider application which will provide the services by using springboard let's start here I'm using spring tool Suite ID let me create one boot project new spring starter project here I'm taking the project name as spring boot rest API Maven build tool packaging type jar Java version 8 language Java here group ID artifact ID version package good click on next here in order to create the boot application we need starters those are called Palm starters in the spring boot we have a starter called Web starter so by using this web starter you can develop our rest API right now along with this I'm going to use the devtools dependency devtools is used to restart the server when we made some code changes along with that I am going to use lombok dependency to generate the Setters Getters for our binding classes so dependencies I am taking one is web starter second one is devtools and third one is lombok good with this let me create the project [Music] spring boot application is creating once this application got created I will create one binding class and I will create one rest controller then we will test that application by using Postman so project got created successfully you can go to form.xml and you can see what are the dependencies that we have added yeah you can see spring boot version 2.7.6 and Java version 1.8 here dependency web starter and devtools dependency lombok dependency this is the test dependency is coming by default I have not selected that it is default dependency for the boot application now in this project to represent the data I'm going to create one binding class let me create one Java class to represent the data I'm going to take a class with the name called Product in this class I'm writing three properties one is private integer ID and private string name private double price good now here to write the Setters Getters for these variables lombok provided one annotation called at the rate data along with this I want to generate a parameters reconstructor for that we have one annotation all aux Constructor which will generate a parameter reconstructor with all arguments of this class all the variables when we generate a parameter reconstructor zero param Constructor will not be generated by the compiler 0 param Constructor will not be generated for that I am going to specify no aux Constructor when you don't use Alex Constructor zero param Constructor will be added by our Java compiler by default but here we are telling to lombok to add the Constructor with the arguments then zero param Constructor will not come so I am using this annotation to get that Constructor good so this is our binding class which is used to represent the data then let's create one rest controller product rest controller I'm creating this rest controller will have methods which are binded to http protocol request and response we are going to deal with that methods here represent this class as a rest controller I am using one annotation called at the rate rest controller then I'm writing some methods here public string save product now I'm going to expect product data as a parameter product P so this save product method will get the product data in the form of Json the data I am going to accept from request body for that we need to use one annotation called at the rate request body then this method I am binding to a post request by using post mapping so here I can give slash product right so here in future we will write the logic to store the record into database table logic to persist but for time being I'm not persisting that log data I'm just printing on the console saying that the record we have received once the record is received I will return a success message written product saved like this good this is a post request method which is used to insert the product now next public string get product so here I'm going to take product ID as a input now let me write one get request method at the rate get mapping slash product so this method will return the product based on the ID so here we don't have a database integration so just let me create one dummy product object product p is equal to new product by using Constructor we can set the data product let me take it as ID name and price right now I need to return this product as this method should return the product method return type I am taking as a product object and I am going to return p s this method will return the product so this product ID whatever the ID that we are getting that ID we need to represent as a path variable so for that we are going to use one annotation called at the rate path variable so if you want to make this record based on some conditions so here if PID is is equal to 100 then I will return this value if a PID is equal to 100 then I am going to return this product object else if PID is equal to some other value then else if if a PID W equals to 1.1 then I am going to return some other record product p now here product p is equal to 1.1 let me take it as hard disk and it is 3500. here I am going to declare one product object as a local variable product p is equal to null p is equal to new product B is equal to new product finally this method will return the product object based on the given ID good similarly let me write one more method which will return multiple products list of products public list of products get products get products I will create a list of products and I am going to return all the products objects as a collection so let me take it as product P1 and product P2 [Music] P1 and P2 so here Mouse and hard disk I am taking now let me represent these products as a collection so by using RS dot to list we can convert them to list object erase dot as list P1 comma beta bit it is going to give list of objects let me import java.util.list yeah it has given list of products now those products I am going to return from our method return products good let me bind this method also to http method by using get mapping slash products slash product method will take a product ID based on the given ID it will return the record slash products method will return all the records here I'm using hard-coded data in the next video we are going to see how to integrate our spring boot rest API with the database to store the data and retrieve the data so controller this is one method which is mapped to a post request it is expecting product data from the request body and this method is get product it is binded to a get request it is taking product ID as input based on that it is returning the data and here we have get products method it is binded to get request with the slash products URL pattern this method will return list of products all right so with this our API is ready spring boot application is ready let's run this application run as boot application run as boot application by default this spring boot application will execute in the embedded server embedded Tomcat server will be given by springboot you no need to deploy that into external servers it is having a embedded server embedded server is a Tomcat the embedded Tomcat server will run on the port number 8080 so Tomcat started on the port number 8080. now my application is running how to test this application this application does not have any UI it is a simple arrest API to test the rest apis we are going to use Postman tool I have already installed Postman application so this is the postman tool here get request localhost my application running in the same machine localhost colon 8080 slash products click on send when I send a request to slash products you see the response I got the product response with the 200 as a status code this is first record and this is second record two records we are getting is a response in the form of Json my method is returning the data in the form of collection but we received the response in the form of Json so when we send a request spring boot application process in our request and it converted that response into Json and it sent that Json response to client here our client is Postman Postman is sending a request to our rest API suppose I want to get the product based on the ID so I'm going to pass product ID as a path parameter click on send when I send a request with the 101 I'm getting the response with that product data suppose if I give as one or two click on send now I'm going to get the response with one or two let me check it oh here 100 and 101 sorry here 100 I'm going to give as a input when I give 100 I'm getting the response with 100 product data when I give the request with 101 I'm getting the response based on the given one or one so so this method is also working as expected then post method how to send your post request in order to send the post request we need to pass the data in the request body send post request URL pattern slash post so we need to send the data method is expecting data in the request body right so here select the request type as post this is the URL body in the body we need to set the data body in the request body we need to set the data go to body this request does not have any body let me take that body none raw data give the data in the form of Json here this is the Json data I'm passing let us see our application console really the data received by our application or not whatever the record we are sending that record I am printing on the console by using sop click on send when we send the request to the rest API using post so it is giving the responses product saved I send the data in the Json format let us see our console yes whatever the data we have sent the data received by our application it is printing the data in the console client to send the data in the form of Json here method expecting the data in the form of object spring boot will use message converters to convert the Json Json data into Java object whatever the object we have received the data we have received converted into object that object we are printing on the console for understanding purpose really are we getting the data from the postman or not yes we got the data then we are printing that data in future here we will write the logic to insert the record into database here we'll write the logic to retrieve the records from the database I hope you understood how to develop one simple rest API by using springboard so what is the conclusion to create the rest API we are using one dependency called Web starter it is providing embedded Tomcat server and we are using one binding class to represent our request data and response data and we created a rest controller by using at the rate rest control annotation we have written three methods one method is binded to post request it is used to save the data it is expecting data in the request to body one method bind it to get request it is expecting product ID as a path parameter based on the given ID it is returning the product data in the Json format third method which is also binded to get request but this method will return multiple products as a response in the form of Json all right [Music] we will understand how to develop spring boot rest API to perform crowd operations with mySQL database our final project is going to look like this database we are using as my SQL to communicate with the database we will use repository concept with the help of data jpa and we will write a service class which will contain our business logic and rest controller will be available in that we will write the methods and we'll map them to http protocol methods and we will have a binding class which will represent our request and response data XML file will be available to configure dependencies required for our application like my SQL driver web starter long bob dependency Etc we will have application.ymn that file will be created in the spring boot when we create a boot project in that file we will configure our data source properties with which database we want to communicate the database properties should be configured in our application and finally once the project development is completed we will use Postman tool to test our rest API by using Postman we can send HTTP request I hope you are good with project components all right without wasting our time let's start our development here I'm going to create one project using toolsoot IDE file new spring starter project I am going to create I am giving the name for this project as spring boot rest MySQL app spring boot rest MySQL app click on next here we need to select the several dependencies to create our project I'm going to select web starter this web starter will provide embedded container also to run our application I am selecting data jpa by using this we can develop the repositories to communicate with the database I am selecting MySQL driver to communicate with mySQL database people who want to connect with Oracle database they need to use Oracle driver here I am using mySQL database so I'm using my SQL driver lombok dependency to generate the Setters Getters required for our binding classes devtools dependency to restart our server when we made some code changes so these are the five dependencies I am selecting web starter data jpa starter MySQL driver lombok and devtools all right next click on finish so with this our MySQL spring boot MySQL rest application is getting created once this project is created then we need to start our development before that I want to configure data source properties in our project application and properties file is created now it is properties files are replaced with ymls right so I am going to convert that properties file to yml inside this yml I need to configure my database properties as I told that I will be using mySQL database in this video so in my SQL you can execute the query show databases use sbms sbms is my schema name show tables what are the existing tables in the database it is displaying in this I'm going to use one table called course details but currently if you see the table is not available my application will create the table dynamically in the database now this database properties we will configure in application.yml to save our time I have saved this data source properties file details now let me configure those properties here so data source username password URL driver class and auto ddl update this is used to generate our table dynamically show SQL whatever the queries that are generated by our application to print those queries on the console I'm using this and next I'm going to create one binding class which is used to represent our data I'm going to take that class inside a package called binding class name giving as course now I'm going to take three properties here private integer ID private string name and private double price three variables I am taking to generate the setups and Getters using Lomb work annotation at the rate data and to represent this class as the entity I'm using one annotation at the rate entity to map this class with your database table I am giving at the ready table annotation at the edit table is the Mandate not mandatory guys when you don't write the table annotation it will use class name as a table name class name as table name here I am writing at the rate ID annotation ID represents that this column is a primary key mapped column this variable is mapped with the primary key column in the table and generated value I want to generate the value for this column automatically for that generator we can give generator strategy we can configure for that strategy is equal to Identity as it is a mySQL database we can use identity generator at the rated data A Long Walk annotation will generate Setter's Getters for the variables and their identity to represent this as a persistence related component at the ready table to map our class name with the table name table is not mandatory if you don't write the table name class name will be considered as table name at the rate ID it is used to represent this ID variable is mapped with primary key column in the database let me give it as a course ID good string name it is a normal variable will be mapped with one column in the database table and the price variable mapped with one column if you want to map this variable name also with a particular column name you can write at the rate column annotation but that is optional good so this class I will be using as a binding as well as I'm going to use it as a entity class to persist the data then create a repository repository is used to perform operations with the database I am going to create a repository with the name course repository in the dot repo package this repository will extend the properties from jpa repository the advantage with the data jpa is it is providing ready-made methods to perform the crowd operations we no need to write the logic just use jpa repository that is extends jpa Repository it is expecting what is your entity class our entity class is course right and what is the type of the primary key I will be taking a generic that is serializable is a type of the primary key value public interface course repository extends jpa repository JPI repository is a predefined repository available in data jpa here you can write at the rate repository annotation but it is optional even if you don't write it at the time of project startup jpa repositories will be scanned by our spring Boot and they will provide implementation class for our interface in the runtime by using proxy here in the JPI repository there are couple of methods available like find all methods sort with find all find all by ID save all flush method save and flush there are several methods available we can access those methods from our interface now because our interface is extending properties from jpa repository good so jpa repository is ready so if You observe our repository is ready database is ready we configure database properties also in the yml file now we need to create our service interface to write a word logic let me create one interface course service I am creating this interface in a separate package called service now in this I'm going to write the methods public string upset I will be taking one method with a name called observed what is upset upset means it is a polymorphic method this method will be used for insertion as well as update if single method is performing both inserts insertion and updation that method is called as upset method insert Plus update that is called upset I am using the method name as upset single method will perform insert an update based on the primary key value and I am taking one method public string save course good sorry 7ups insert and be done by upset method only so here I am going to write one method which is used to get the particular course based on the given ID get by ID so I'm going to take integer course ID as a parameter and I will be taking one more method which will give all the courses available in the table get all get all courses good and one one more method I will be taking which is used to delete the particular course public string delete by ID for that I am passing course ID as a input so save Plus update will be taken care by our upset method get by ID will take the ID as input will give that particular course record get all courses will give all the existing courses in the database table delete by ID to delete the record based on the given ID now let me implement this interface new class course service impl [Music] it is going to implement that interface that means we need to override all the abstract methods available in our service interface perfect now to represent this service class as a spring bin I'm going to write one annotation at the rate service this service should communicate with the repository service class methods you should talk to repository methods so repository should be injected into our service through Auto wiring I am going to perform dependency injection by using Auto byte annotation private private course repository in multiple ways we can do that dependency injection guys so one is by using Center injection another one is through Constructor injection and third one is field injection now I am using at the rate auto wide annotation course repository is an interface so where is the implementation you no need to write the implementation that's the beauty of data jpa you no need to write the implementation for this interface jpa will provide the implementation that implementation class object will be injected into our service class good now upset method so how we are going to perform this upset operation insertion Plus update you don't need to do anything just you call course dot course dot I mean repository dot course repository dot save I'm going to pass course object I'm going to pass course object here I am going to return success good so here save method internally it will perform upset operation whatever the object you are passing if that object contains primary key value then it is going to update the object update the record whatever the object you are passing if that object does not contain the primary key then it is going to insert the record upset to nothing but insert or update them based on PK so whatever the entity object you are passing for this repository method if this entity object contains the primary key then it is going to update the record if this entity object does not contain the primary key then it is going to insert the record repository save method the repository save method is a upset method both for insertion as well as for update we are going to use the same method that's why I am also writing a single method in my service interface called upset good next one get by ID based on the given ID we need to retrieve the record and we need to return that record good for that we are going to use course repository dot find by ID there is a predefined method which is fine by ID that method is returning that method is written in optional object optional objects are introduced in Java 1.8 to avoid null Point receptions if you object if it is if it is null if you perform operation on the null you will get null Point exception now when we go for optional optional is like a container first we need to check that in that optional our entity object is present or not if it is present then only we are going to get that find by ID dot get and return that I'm going to call find by ID method in the repository repository dot find by ID with the given primary key value it will try to get the record with that given ID if record is available that will be stored into optional if the record is not available our optional will be empty now once we have retrieved that I am calling find by ID dot is present if this condition is satisfied that means record is presented in the optional object we can get that record by calling get method same record I am returning from My Method if satisfied then it is going to return null object record is not available [Music] [Music] good this method will retrieve all the records available in the table and it will return all those records and next one delete by ID based on the primary key we need to delete that record first let me check with the given ID record is available or not course repository Dot exists by ID in this repository jpa repository there is a method called Exist by ID Exist by ID method will tell you with the given primary key record is presented in the database or not if it is present this method will return true otherwise it will return false if given ID if the record is available my condition will be satisfied then I will come inside the insert if condition then I am going to call courserappo Dot delete course report dot delete by ID whatever the ID we got as input that ID I am passing to delete by idea method suppose for example with the given ID if the record is not available it is going to return saying that no record formed if it is available then it will return delete success delete success perfect [Music] okay correct it yeah so this is our delete method delete method is used to delete the record available in the table exists by ID based on the given ID it will delete the record and this method get all courses to retrieve all the courses available in the table by using find all method find by ID to get a single record based on the given ID I am calling repository dot find by ID that will return optional object and upset method this is our method which is internally calling repository.save method save method is the upset method which will perform insert or update based on the given entity object if you give the object with the primary key value with the primary key if record is available it will update if not available it will insert so this is our service interface to represent this class as a spring bin I am using at the rate service annotation inside the service we are injecting repository interface this interface does not have implementation we are not writing that jpa will take care of that jpa will create the implementation in the runtime and it will inject that implementation object into our service being in the runtime perfect finally let let us create our rest controller to deal with this good so I'm going to use a package in dot Ashok id.rest course rest controller in that we are going to write the methods to call our service layer methods to represent this class as a rest controller we are going to use at the rate rest controller annotation as we have seen in the diagram rest controller methods we'll talk to service service should be injected into rest controller service will talk to repository repository injected into service service should be injected into rest controller through dependency injection how can we inject it by using Auto wiring private course service core service I'm going to take the variable name as course service let's specify at the rate auto wide annotation so this course of service interface reference variable I'm using ioc container will find implementation class for this service interface will inject that class object into our rest controller first up let me write one method public string or we can take a response entity also public response entity as a string I am going to use one method called create course create course is a method that method is expecting course data which core city has to create that course data it is expecting in the request body so let us take The annotation as at the rate request body to read the data from the request body in the request body Json data will come from the postman the Json data will be converted into our Java object let's bind this method to post request by using post mapping slash course now it is going to call course service dot upside service method service method we are calling upset method that method will return a record I mean response it is written in some response that is status that is a status response now here I need to return the response back to the client right rest API will be accessed by client application to return the response to the client I am using written new response entity whatever the response that we want to return we can send the response response body comma status code HTTP status created post method is used to create the new record so I am returning the status with the created that is two not one I'm taking the request body I am going to insert that record and I am going to return the response with the HTTP status code AS 2.1 this is post request method similarly let's write one method which will return the response of one single course based on the ID for that I am taking response entity of course get course it is taking integer course ID that course ID we will receive as a path variable to read the data from the URL I am using annotation called path variable map this method to get request by using get mapping slash course slash course ID now simply call service layer method course service Dot course service dot get by ID whatever the ID we received in the URL same ID we are passing to service layer method service layer method will talk to repository will get that record that record I am going to return from my rest controller return new response entity response I am sending course data response I'm going to send course data comma status code okay it is a get request HTTP status I am giving as okay for post request I am giving created two not one for get request I am giving okay that means it is Success similarly we need to write one more method which will retrieve all the records from the table and will send to the UI for that I am writing one more method public list or you can say take response entity you can take the response entity and you can take list of course good get all courses get all courses perfect here I'm going to call courseservice Dot find all or get all what is the method we have written get all courses this method will return all courses as a collection that I want to return from my rest controller return new response entity of all courses status HTTP status dot ok good this method I'm binding to a get request get mapping slash courses when you send a get request to this URL pattern it will retrieve all the courses and it will send back in the form of Json next one we need one method to update the record and delete the record so public now I can copy this method I can copy this method instead of post mapping I will give it as a put mapping guys put Mappy so here update course insertion and updation both will take the course data and both are going to call service layer upset method offset method is a polymorphic method it will decide whether to insert or update based on the given ID so this is put request methods update course taking the course data calling service dot upset as I told you upset method will perform insertion or update based on the given data if you give primary key it will update if you don't give the primary key it will insert then similarly we need one method to delete the record get by ID is there similarly based on the ID only we need to delete the record that I am going to map with delete mapping at the red delete mapping course with the ID delete course we need the ID in the URL we are taking the desert path variable service dot delete by ID that's it with the given ID here I am calling course service dot delete by ID what that delete method is going to return delete method will return status that means message success or failure that status I am going to return as a response from this rest controller method so delete mapping method which is expecting course ID as a input expecting course ID as a input based on that we are trying to delete the record with this our rest controller is ready so finally our project is completed we are able to create a repository we are able to create service and we create a rest controller one binding class dependencies added in the form.xml data source properties configured in the application.yml now let us test this application by using Postman so first let me run this project guys [Music] as we already discussed in the previous videos spring boot application will execute in the embedded container by default Apache Tomcat will be used as embedded container it will run on the port number 8080 good application is getting started once it started we can test that application in the postman let us see database table is getting created or not we configured Auto ddl that means database table should be created failed to configure a data source attribute is not specified no data source could be configured it is not able to read our data source properties that we configured let's update that project Maven update we change it from properties file to yml and we added the data after that we forgot to update the project let's update our project Maven update I have given what's the project update is completed then we can run the project once again yeah update got completed right click on the project run as boot app [Music] thank you yes now you can see that guys create table course details so our table got created in the database check in the database select a start from course details our database table got created but there is no data we need to insert the data in the database now [Music] got it good now table got created in the database now our application is also started application is running now let's try to test this application by using Postman first it is going to first it is running in the embedded server right when we started that application it is running in the embedded server good go to postman let me make a post request first enter the URL localhost colon 8080 slash course we need to send the data in the request to body raw data in the Json format I will send the data in the Json format key and value id id I no need to pass because it is a auto generated name let me give the course name as a Java let me give the core surprise so I'm giving the course price as 3500 as a double value good I'm giving the data in the request body and I'm sending a post request to this URL click on send button when I send that request you can see we received the response as success with the status code as two not one resource is created check in the database earlier the table was empty now select this and run this query now you can see one record is inserted in the database one Java 3500 let me insert one more record in the database devops I'm giving this as 4500 click on send one more request I have sent check in the database select star from course details run this query now we can see devops record is also inserted now guys let me check course slash one I'm going to send you a get request with the course ID in the input get request I'm sending click on send based on the given course ID we are able to retrieve the record and we are getting the response in the Json format if I am giving course ID as one I'm getting the Java record if I give the course ideas to then I'm going to get devops record for example if I send you a request to slash courses get request with the slash courses send that I'm getting all the courses the two records inserted in the table first record and second record two records we retrieved as a Json that means we tested three methods as of now post method we tested that is working as expected we are able to inside the record by using this post method we are able to retrieve the single record based on the given ID and we are able to retrieve all the records by using this courses URL let us test put method and delete method also for put method we need to send the course ID in the guys go to put method let us pass the data in the request body here course ID Java Java price we have 3500 now let me change to 5500 I'm just trying to update I'm just trying to update the price of the course earlier when I am trying to insert the record I pass it only name and price that's why it inserted the record upset operation now I am passing ID also put slash course put slash course now once again observe select star from table name here Java course price inserted with 3500 now I am sending the course ID in the input name and price as 5500 click on send now see the response success we got the success response with the 200 status code for insertion we got two not one for update we got 200 status code go to database select the star from course details run this now you see Java course price changes to 5500 are we able to update yes by using put request we are able to update the record by using post request we are able to insert the record by using get request we are able to retrieve the records final one delete go to here click on delete select the delete request slash course slash two click on send I'm sending a delete request delete success I got 200 response go to the database select the table yes devops record got deleted second record is deleted because I have given the ideas too only we have one record now if I give one also course slash one with the delete click on send that record also got deleted check in the database yes table is empty because all the records got deleted I hope you understood how we tested this application by using Postman now this architecture we are able to complete right Postman for testing rest controller service repository yml file today configure data source form.xml to configure dependencies binding class to represent our data so I hope you are able to understand how to perform crowd operations with mySQL database by using spring boot rest API all right how to export database data to Excel file by using spring boot application all right let's get started in order to generate Excel file by using Java application we will use one third party API that is Apache file Apache organization provided by API Pi is open source libraries are open source API to create and manipulate various file formats why we need to use this Apache y third-party API because in Java there is no built-in support to work with Excel files directly if you want to create the Excel file or if you want to read the Excel file data Java does not have any direct support that's why we are using Apache Pi API here in this Apache boy API we have several classes and methods to work with the Excel file like hsf and xsf API is available to read write and modify Excel spreadsheets all right if you see any Excel file here one Excel file is called as one workbook [Music] Excel file is called as one workbook when we open that Excel workbook we can see sheets in the Excel file in the Excel workbook sheets will be available we can create multiple sheets sheets will be created based on the indexes and in one sheet multiple rows will be available Row 1 Row 2 Row 3 like that and in one row multiple columns will be available now if you want to generate this Excel file by using our Java application first we need to create a workbook in that workbook we need to create the sheet in the sheet we need to create the row in the row we need to create the cell and in the cell we need to store the data so in order to do all these operations Apache Pi API provided several components workbook is available sheet available row available is still available using this workbook we can create Excel workbook using sheet we can create the sheet in the workbook based on the index inside the sheet we can create the rows based on the index row index will start from 0 once the row is created in that row we can create the cell by using index inside the cell we can set the cell data so in order to generate the Excel report we need to use these components from Apache Pi API all right let us see how to develop one application to generate the Excel report here I'm going to create one spring boot application file new spring starter project I am creating it one rest API to export this data spring boot Excel export Maven project I'm creating packaging is jar Java version is 8 so here spring boot Excel report click on next I'm using lombok dependency data jpa I need to read the data from the database for that I am using data jpa dependency and mySQL database I want to connect so I'm using MySQL driver web dependency to generate the rest controller and the devtools dependency I am using by using these dependencies I'm creating the project guys here directly we cannot select the POI dependency in this STS IDE it is not coming we need to add that Apache 5 dependency manually in our palm.xml so lombok to generate the Setters and Getters data jpa to communicate with the database MySQL driver to load the driver to connect with the mySQL database web dependency to create a rest controller Dev tools to reload our code changes click on next and finish once this project is created we need to add Excel dependency that is poi dependency now here I'm taking this dependency pi over XML from Apache take this dependency add this dependency in the palm.xml I will share this source code in the git repository you can find the repository Link in the description of this video yes I'm adding the dependency details [Music] Point dependence I have added it is downloading the dependency it is next one we need to communicate with the database so data source properties I'm going to add by default properties file is created I'm converting the properties to yml nowadays people are using yml files instead of properties in this yml we need to configure our data source properties with which database we want to connect the database properties we need to configure in our yml file here I am configuring mySQL database which is running in my machine I'm giving localhost database name is sbms username and password MySQL driver Class Auto ddl also I am giving let me check the database table so here course details table is available select start from table when we execute this query to retrieve the data it is saying that there are four records available four course informations available four courses information is available in the database table now I want to develop the project which will export this table data into one Excel file good database is also ready data is ready I have configured data source properties in yml next one in order to read the data from the database table we need to create one entity class and repository interface let me create one entity class class name I'm taking as course it is a course entity I need to map this class with the database table to map the class with the database table we will use jpa annotations at the rate entity at the rate table table name is course details course details and properties in the class that are matching with database table columns private integer what are the variables available cidc name price available integer CID integer C name and private double price good these are the properties I want to generate Setter's Getters for these properties so I am using Long Walk annotation at the ready data good let me import all these packages [Music] Gap effect packages got imported data imported from lombok table and entity annotations imported from jpa and this is a primary key column mapped value so I'm using at the rate ID annotation to represent that CID is a primary key column in the database table perfect entity is ready to retrieve the data I am going to create one repository interface so let me take it as course Repository course repository here inside this repository I am extending the properties from jpa repository so jpa repository I am extending extends jpa repository JPI repository provided by Spring data jpa which is used to perform crowd operations with the database without writing any logic we can directly call the methods of jpa repository to perform those operations perfect with this my repository is also ready now let me write the actual business logic to generate the Excel file here I am writing one class called course service or I can say it as a report service we are generating a report right let me call it as report service report service we need to build our actual logic right my service should talk to repository so let me inject repository object into my service class by using Auto wiring course repository with the help of Auto wide annotation to represent this service as a spring bin I am using one annotation called at the rate service at the rate service and repository object is also injected into our service now let's write the actual method public void generate Excel here I'm taking HTTP servlet response as a parameter the reason is when users send a request directly that Excel file should be downloaded in the browser so I need to send that Excel file as a response that's the reason I'm taking HTTP satellite response object as a parameter whatever the Excel file that I am going to generate by using this method that Excel file I will add in the response object and I will send it to the client directly good so with this we are able to write our service method now what is the logic that we need to write in the service method first we need to get the data from the database course report dot find all when I call the find all it is going to give me list of courses available in the database table list off records available in the database table once we got the list of records as we discussed we need to create a workbook in the workbook we need to create the sheet in the sheet we need to create the row in the row we need to create the cell in the cell we need to set the data good so first let me create a workbook hsf workbook on creating it is a predefined class available in Apache Pi API hsf workbook I'm going to take this as workbook is equal to new hsf workbook workbook object got created once the workbook is created inside that workbook we need to create a sheet workbook dot create sheet for the sheet you can give the name also I'm giving the name as courses info courses info Let Us store this sheet into one variable hsf sheet once the sheet is created in the sheet we need to create the row sheet dot create row as discussed row will have your index first I want to create the header row so I mean table data metadata here ID here name then here price like this I need to set the data like this ID name and price so the first row I'm going to consider as a header row to represent metadata one row is created based on the index in that row we need to create the cell this one row is created in the row we need to create the cell row index will start from 0 cell index also will start from zero once row is created based on that row object we are going to create the cell row dot create cell selloff 0 dot set cell value I'm going for method chaining concept set of cell value here I'm setting the value first value is ID similarly I'm going to create cell 1 cell 2. ID name and price course ID course name course price so in a row I'm creating three cells cell index one cell index two zero one two so with that header row will be created and the header data will be stored into that next one once the header row is created now I need to create the data rows and I need to set the data row 0 already taken for the header now actual data should be stored in the remaining rows from the first row onwards the row index one how we are going to do that for this I am going to take first integer row index data row index is equal to 1 row index 0 already taken for header row data row will start from first index data row index is equal to 1 and I'm going to take for each Loop list of courses available let me take each course from the courses now now I need to create one row to store the data how to create a row here already we have a sheet object sheet dot create row Based on data row index based on the data row index now data row is created inside this data row we need to set the data data row dot create cell cell index always every row cell will start with zero create cell of 0 dot set a cell value I want to set course Dot get cosiding similarly data row dot create cell of cell1 dot set cell value of course dot get course name course dot get course name good next one data row dot create cell of cell index 2. dot set cell value I need to set the price good course dot get course price I'm setting this now row index 0 I have taken for storing header data and in that header row 0 is to sell first cell second cell that means in one row three cells will be created like this ID name price then my data will be stored in this row data will be stored in this row data will be stored in this row like this data rows will be created so in every row cell indexes will be available 0 1 2 again second data second record 0 1 2 like that fine once first row data is created once First Data row is created I need to increment the data Row for the second record data row index plus plus I'm using increment operator in order to increment that row index First Data will be first record will be created in the first row it is going to store the data in the first row in the three cells what's the record first record is inserted in that row then I am increasing row index then it is going to take then it is going to take second row to store the second record to a third row for third record for fourth record fourth row will be considered like this row index I am deciding here as one then I am incrementing that for every view record once this concept is done then we need to store this Excel data to a Excel file and that file we need to send in the response for that I am going to get output stream here already I have taken HTTP Starlight response as a parameter response dot get servlet output string response dot get output stream I'm getting satellite output stream as a parameter I mean as a return type get output stream method is giving me output stream object output stream object once that output stream object we got then let me use workbook dot right of output stream so whatever the data is available in the workbook I'm writing the data to Output stream okay guys once it is done I'm doing workbook Dot close I'm closing the workbook and I'm closing output stream as well so this is the method which will read the data from the database table which will create one Excel workbook in that workbook it will create one sheet in that sheet it will create one row that Row first row is called as data Row first row index is zero once the first row is created with the data then I am creating data rows by using the data available in the collection if you get 10 records from the table 10 data rows will be created if you get 30 records from the table 30 rows will be created for every record one row will be created based on row index once the rows creation is completed with the cells then we are creating output stream object from the response that output stream object we are using and we are writing the workbook data to that output stream once writing the data portion is completed then we are closing the workbook then we are closing our output stream got it fine now let us create one rest controller in order to access this service layer method report rest controller I am going to create report rest controller rest controller will talk to our service layer method so let us inject service object here private report service through Auto wiring I'm using at the rate auto wide annotation here rest controller I am using at the right rest controller inside the rest controller let us write a method so that we can make a request to that method then it will download the Excel file for us now let me create the rest controller method that rest Contour method I will bind it to a get request public void generate Excel report now for this method I am taking one parameter as HTTP servlet response we need the response object to send the data when my method is expecting HTTP servlet response so whenever that spring boot ioc container is loading our classes and when the request comes to that spring boot application it will understand that my method is expecting HTTP Starlight response so response object will be created by our dispatcher servlet here and it is going to pass that response object as a parameter to this method this method I am binding to a get request at the ready to get mapping binding to your url slash Excel that's it good now let us call our service layer method report service dot generate Excel by passing the response object that's it now this method is throwing one exception so let me use throws keyword throws exception perfect I'm calling my service layer method I'm calling my service layer method good whatever the record that we are generating that records will be stored into response object that record whatever the file that is generating that file should be downloaded whenever we send a request to this rest controller method right for that we need to set the content type how the response is going to send response dot set a content type we need to set the content type as a key content type that is going to represent in which format we are sending the response to the client application slash octet stream we are going to send the response as a stream octet stream that file will be generated file should be sent to the client and we need to set response a header also for that we are going to use key and value so string header key and a header value header value how we are going to send it that data we are going to send as a file attachment right content disposition that is the key we are going to write and header value I want to send this value as an attachment so what is the name that you want to use for the attachment you can give file name is equal to courses dot XLS that's it now let us send this response Dot response dot set header response dot set header key and value header key is available and a header value is available that's it so this is my rest controller class I am using at the red rest control annotation and in this rest controller I am injecting my service by using Auto wiring I am writing one get request method in this get request method I am taking response object as a parameter dispatcher select will call our controller method then it will pass the response object as a parameter response content type I am setting as octet stream because this method is responsible to download the Excel file I am setting the response header container disposition as a attachment the response should be sent as in file attachment with a courses dot XLS set a header I am doing that finally I am calling my service method by passing the response object good save this let me update my project Maven update [Music] project is getting updated once project update completed let us run this application right click run as boot application our application is getting started yeah fine guys with this our application got started spring boot will use embedded server Apache Tomcat Apache Tomcat is running on the port number 8080. now let's go to browser and let's make a request to our application and see whether Excel report is getting generated or not so I'm going to open browser now here let us make a request localhost colon 8080 slash Excel localhost colon 8080 slash Excel I'm hitting this URL and let us see what is the response that we are going to get from our application send the request when I send a request now see in the database query got executed that means it is retrieving records from the database table and let us see what is the response that we are getting from our application foreign we will understand how to deploy spring boot application by using Docker all right let's get started Docker is a containerization platform containers currently containers are trending in the market right container is nothing but easily shippable we can transfer the containers from one location to another location easily that's why the docker people has given the logo also as a ship logo inside the ship you can see the containers easily we can ship them our applications will become portable when we use docker here what is the main advantage of going for Docker if you want to run our application for example if you take a spring boot application in order to run the spring boot application we need Java software you if we want to use a database for our application then we need Java Plus database to run our application whenever we develop our application directly we cannot give to the client for execution we need to test our application in multiple environments then only we will give to client so we are going to test in the div environment City environment and uat environment pilot environment will be available and prod will be available when we want to test our application in multiple environments what about the softwares which are required to run Java software MySQL software and all we need to install those softwares in all the machines that will take lot of time to reduce that problem we are going to use Docker containers here when we go for Docker containers our application will execute inside a container and that the docker software will take care of all the required softwares those are called as dependencies of our application for running the spring boot application we need Java Java is a dependency for boot application if you have a database database will become dependency for our Java application instead of we are installing all the dependencies in all the machines we are going to use a Docker Docker will provide the infrastructure to run our application easily in any platform in any machine so that's why Docker is a trending in the market with the help of this Docker we can easily deploy our application into multiple environments as a containers how to dockerize the application in order to dockerize the application we need to write a Docker file Docker file contains a set of instructions to install the required dependencies for our application in the docker file we will specify where is our application code and what dependencies are required to run that code by using the docker file we are going to build a Docker image Docker image is like a package which contains our application code Plus application dependencies once the docker image is created you can take that Docker image and you can run that image in any place platform once you run the image Docker container will be created inside the container our application will be executed Docker file Docker image Docker container Docker file contains set of instructions to build the docker image Docker image is a package which contains application code Plus application dependencies once image is created we can run that image in any machine in animation once you run the image container will be created inside the container our application will be available Docker container is called runtime instance of our application in order to run the docker containers we need a Docker engine Docker software we need to install in our machine either Windows or Mac or Linux if you install the docker engine then you can run the docker containers by using Docker image once you run the docker image all the required dependencies will be taken care by Docker only you can simply execute Docker image in any platform your application will become portable all right let us see how to dockerize this application here I am going to create one spring boot application in that I will create one rest controller then I will package that spring boot application by using Maven a jar file will be created then I will write a Docker file for the spring boot application by using the docker file I will create a Docker image once the docker image is created I will run the docker image then it will create a Docker container once the container is created we can access that application in the browser all right let's get into the practicals of this here I'm using spring tool suit IDE I'm going to create one project new spring starter project I'm using the name of the project as springboot underscore Docker underscore app mavinize the ability tool packaging type jar group ID in data Ashok ID package artifact ID springboard underscore Docker underscore app click on next in order to create this boot application I'm using boot version 2.7.6 and I need a web dependency to create the rest controller I'm selecting web starter click on next click on finish spring boot project is getting created inside this project I am going to create one rest controller and I will map our method to one get request create a class I'm taking a class name as welcome rest controller inside this class we can write a method and map that method to http request public string get message here I'm going to return welcome to Ashok ID [Music] got it now this class is a normal Java class now I want to make it as a rest controller let us take a annotation at the rate rest controller let's map it to a method by using get mapping all right I'm creating a rest controller it's done so once this rest controller is ready now we need to package our application by using Maven we are using Maven as a build tool let me go to pump.xml in the form.xml we can configure the name of the jar file by using final name here I am giving the name of our application as ESB underscore Docker underscore app you can give any name you give this name the jar file will be created with this name it is not mandatory but optional here if you don't give that final name then it will take our group ID our artifact ID and our version a lengthy name will be given for the jar file so I want to make it as simple so I am giving the name as SB underscore Docker underscore app that's it in the palm.xml let us go to our workspace so this is our boot Docker app in this project form.xml is available Target directory is available inside the target jar is not available because we have not packaged our application at now let me open command prompt and let me package this project by using Maven here I am using mavengool mvn clean package when I use mbn clean package it is going to download all the required dependencies and it is going to package our application once application is packaged we can see the jar file in the project Target directory it is downloading the required dependencies as finally our build got success let us verify that in the Target directory with the given name our jar file got created so now I want to deploy this jar file by using Docker for that we need to create a Docker file in this project I am going to write a Docker file [Music] now in this Docker file we need to write the instructions to build the docker image the first instruction is from I am going to specify the base image open jdk either Java 8 or Java 11 or Java 17 which version of java we need to run this application we are going to specify that by using from then we need to copy the jar file from our system path to Docker container where the jar file is available inside the project Target available inside the target this is the jar file let us take this I'm going to tell here inside the target directory my jar file is available this is the path of the jar copy this is a jar into copy this jar into container location so slash user slash app is the default location in the docker VR so that means a Docker container will be created that is a virtual machine with Linux in that this location will be available I'm telling to Docker to copy the jar file from our project Target directory to Docker image in this location once the jar is copied to that location I will set the work directory as that location where our jar is available then we need to tell the docker to execute our jar file from that location so I'm going to use entry point instruction it is used to execute our instructions while container is creating how to run a jar file so we are going to use Java Java space hyphen jar then name of the jar file this is the jar file that it has to execute I'm telling to Docker to install Java 11 version copy the jar file from the project Target directory to this location in the docker then go to that location and execute our jar file this is the docker file that I am writing to create a Docker image once it is a Docker file is available we can build the docker image let's go to command prompt now before going to build the docker image first let me check are there any Docker images available in my machine and guys here make a note in order to build the image and run the image Docker software should be installed in my machine Docker software is running as a Docker desktop in my Windows machine Docker is running Docker software is running now as the docker software is running I can execute the docker commands I'm going to use a command called Docker images when I execute a Docker images command it is not giving me any output that means no images are available in my machine no images are available in my machine now in my project Docker file is available by using that a Docker file I need to build Docker image so what is the command that we are going to use to build a Docker image Docker build command we are going to use Docker build iPhone T iPhone T represents a tag name that means image name you can give any name for the image I'm giving the image name as my USB app spring boot application space dot this dot represents that it will check in the current directory in this current directory project directory Docker file is available to build a Docker image Docker file is the input Docker build is the command iPhone T represents name of the image dot represents current working directory it will search for the docker file in the current working directory execute this command once we execute this command as per the docker file it is going to build a Docker image Docker image creation is in progress yes finally our Docker image got created so how to verify the docker image is really created or Not by executing that command Docker images yes when I execute the docker images command it is saying that when image is created 20 seconds ago earlier when we execute the docker images command it has not given any output now when I execute the docker images command it is giving that image which is created USB app is the name of our image this is the idea of the image just 20 seconds ago our Docker image got created once the docker image is created we need to run that image so that Docker container will be created so what is the command to run Docker image right docker run iPhone D iPhone D is optional iPhone D represents detached remote iPhone P it is a port mapping so Docker container nothing but a virtual machine will be created in that machine our application will execute I need to map that container port to our host machine Port host Mission nothing but our computer where Docker engine is running in my machine I want to use port number 8080 Docker container that means our springboard application will execute with 8080 as a port number here this 8080 is a container Port which is using by our spring boot this 8080 is our host Port I am mapping container port with a host Port Docker run iPhone D iPhone P Port mapping space image name what is our image name SB app is our image name spring boot application using 8080 host Port I'm using 88. you can change the host Port also no problem iPhone D represents detached remote that means once this command is executed my command prompt should be free to execute other commands if you don't use iPhone D container will create you will see the logs directly you can't execute any other command that's why I'm going for detached mode execute this command it is creating a container yes container got created so this is giving some output what is the proof to say that container get created let us see the containers which are running by using Docker PS command when I executed Docker PS command it is saying that one container is running this is the name ID of the name of the container this is the image which used to create that container this is the command which executed by Docker engine 6 16 seconds ago our container got created this is the container port and this is the host Port now Docker container is running that a Docker container put a map to host Port now let's try to access that application in the browser let's go to browser localhost colon host Port what is the host Port we have given 8080 let's hit the URL yes we are able to get the message welcome to Ashok it so who is giving this message our container is giving that message inside the container our spring boot application is executing in the spring boot application our rest controller is available Rusty control is having a method which is binded to get request with the default URL pattern so when I send localhost colon 8080 slash it is giving the response for us so with this we are able to deploy spring boot application by using Docker for example if I deploy this springboard application by using Docker in the Linux machine that machine public IP we are going to use to access our application all right good if you want to understand more about the docker so here I have prepared some videos on the docker what is Docker and how to work with Docker what are the docker commands that we need to know here we have used two commands one is Docker build command and Docker run command Docker build command is used to build the docker image Docker run command is used to run the docker image and we have executed Docker images command which will display what all the images are available in our machine and similarly we can use the docker PS command to see what are the containers running in our machine I hope you understood how to work with the docker if you like this video please subscribe to our Channel and click on Bell icon for upcoming videos I will share this Docker commands video URL also in the description [Music] how to deploy spring boot application in AWS cloud all right let's get started WS cloud AWS stands for Amazon web services this AWS is one of the leading cloud provider in the market so what is this cloud cloud computing is the on-demand delivery of it resources over the Internet with pay as you go pricing that means whatever the resources that you need to set up your application whatever the infrastructure that you need to set up your application you can take that infrastructure over the Internet with pay as you go pricing if you want to set up an application you need some machines you need some servers you need storage you need Network you need database all these it resources you can take from cloud provider like AWS pay as you go pricing means how much you will use for that you need to pay AWS providing 200 Services over the web whatever the it resources that you need you can take from the AWS instead of we are buying owning and maintaining our data centers servers network storage everything you can take from AWS AWS is one of the leading cloud provider in the market all right so with this we understood what is springboard and what is AWS Cloud spring boot is used to develop spring-based applications with less configurations AWS is one of the cloud provider which will provide it resources over the internet now we are going to create one boot application and we will deploy that boot application in AWS Cloud what is the procedure I am going to follow I will create one boot application in my local machine I will package that application using Maven goals and I will launch one ec2 instance in the AWS cloud with Linux operating system I will connect to that Linux machine using mobile extern software then I will install Java software in the ec2 because to run the boot application we need Java once that Java got installed then I will upload this jar file into ec2 instance from the mobile extreme then I will run that boot application then we can access that application in our browser alright let's get started with this I'm using spring toolsuit IDE to create boot application file new spring starter project build a tool I'm using Maven packaging type jar group ID in data showcity artifact ID boot web app base package in dot Ashok ID click on next version 2.7.2 the latest version of the spring boot web dependency I'm using that is a web starter to develop web application using springboard all right the boot application is getting created in this boot application I have just added one web starter guys nothing else that web starter will provide the support to develop the web application and it will provide Tomcat as the default embedded container so when start class is created start class is the entry point for boot application execution let us create one rest controller I'm creating a controller with the name called Welcome rest controller welcome rest controller in this rest controller I am going to write one method so just welcome message method I am writing return welcome to Ashok it [Music] I'm not writing any logic here currently just I am writing a method which is returning a message this method I am binding to get request using git mapping annotation to represent this class as a rest controller I am using get the rate rest controller annotation and I am importing that package rest controller package I have imported get mapping package also imported all right first let's run this application in the local and check it run as boot application [Music] you see on the console the boot application started and it deployed into Tomcat server which is running on the port number 8080 he has seen I have not downloaded the Tomcat I have not installed the Tomcat the Tomcat is coming with a spring boot as embedded container now let's try to access this application in our browser it is running in my local machine so let's go with localhost colon 8080 default port number welcome to ashokite we are able to access the application in the local same application I want to deploy in the AWS Cloud all right for that I am going to use my AWS account and I will launch one ec2 instance currently in my account no instance is in the running State let me launch one ec2 instance Amazon Linux T2 micro which is a free tire eligible one instance I am taking default storage 8GB tax you can give a tag name for that it is optional step I am giving the tag name as Linux vm1 [Music] Security Group guys if you don't have your Security Group you can go with the default Security Group it will create here we need to enable SSH protocol to connect with the Linux machine we need this SSH protocol with 22 port and our spring boot application runs on the Tomcat with the default port 8080 you need to enable that 8080 Port also using add rule option you can enable that Port here 8080 Port you can enable here custom traffic anywhere right if you already have a security group you can select existing Security Group I have created One Security Group Associated Security Group in the security group already I have enabled that inbound rule custom TCP rule with 8080 and SSH protocol 22. you can use your existing security group or you can create new Security Group make sure SSH protocol and this is TCT with 8080 both are enabled in the inbound roles next review on the launch here it will ask one key pair file it is used to connect with our ec2 instances securely so if you have your keypad already select it otherwise you can create a new key pair I already have that I am selecting existing one launch instances so this instance launched in the AWS so it will take some time to get started instant state currently in the pending let's wait till it comes to available state [Music] once this instance is ready we can connect it to this instance using mobile external all right yep currently this instance is in a running State now select this instance it will give you one public IP select this public IP address go to mobile extram I have downloaded mobile extra in my machine using this mobile extram I will connect to this Linux machine all right session SSH remote host that is the public IB username the default username for this instance is easy to use a connection I'm using this private keypam file open okay I connect it to this machine all right so it will ask you to update the existing packages using em package manager sudo M update yes updating the existing packages in this Linux machine once it is done I'm going to install Java software in this Linux to run our boot application all right the update got completed currently check Java is available in this machine or not Java version is not available Java currently not available in this machine sudo M install Java I'm installing Java here confirm Java installation got completed so let us check Java version foreign yes Java open jdk 17 got installed all right currently in this machine do we have our application to run no our application is not ready now I am going to package our application as a jar file all right so once this application is packaged as a jar file we can upload this jar file into Linux machine and we can execute currently if we go to this application Target directory Target folder no jar file is available let's package our application using maven so right click run as Maven build so I am going to use the goal as clean package apply run Maven goals I'm executing to package our application as a jar file packaging type we mentioned as a chart so the jar file will be created mavenclean plugin it is going to delete the target folder then it is going to execute compile plugin compile plugin will compile our application then it is going to package our application as a jar file [Music] sticking some time guys build a failure see here it is saying that no compiler is provided in this environment perhaps you are running on the JRE so in our application build path currently JRE available so we need to add the jdk go to build path configure build path libraries click on this JRE edit alternate GRE currently jdk not available let's add it next home directory let's go to our C directory where Java is installed let's go to our C directory where the Java is installed see program files Java add the jdk select the jdk folder don't select up to bin select opto only jdk click on finish select the jdk now apply apply and close now you can see jdk is available select it finish apply close now jdk is added let's execute map and goal once again Maven build fine guys now finally our build success so let's go here and check the target directory here we can see Springwood jar file got created now we can run our application using this jar file let's go to mobile extram here currently in this machine our jar is not available I'm going to upload the jar into this Linux machine jar is available in my Windows machine I want to run that in the AWS ec2 instance so using mobile extram you can directly upload the jar file here we have upload option guys see here upload to current folder click on this and it is going to ask you where is that jar is available select the location select the location of that jar open that yeah this is my jar file I'm selecting this jar click on open that jar file is getting uploaded into my working directory yes the jar file got uploaded let us see LS iPhone l jar is available yes we can run this jar Java iPhone jar jar file name I'm trying to execute this application in the AWS Cloud ec2 instance now you see the jar file is getting executed [Music] application started on the Tomcat with port number 8080 this is the default container which we are getting Now application is executing in the ec2 instance which is created in the AWS Cloud now how to access this application in our browser so for our easy to instance public IP generated right using this public IP we can access this application earlier I accessed using localhost which is running in my machine now my application is available in the AWS Cloud to access that we are going to use the public IP of ec2 instance colon port number 8080 colon code number 8080 public IP colon 8080 [Music] one second colon 80 . now see here I'm able to access that application in my browser where this application is running now this application is running in AWS Cloud this is the public ipf of ac2 and 8080 is the port number so I hope you understood how to deploy our spring boot application into AWS Cloud so these are the steps I have followed [Music] how to connect with AWS Cloud database by using spring boot application so first let's understand what is this Amazon RDS service in the AWS Cloud which is used to manage relational databases here if you want to communicate with your database first we need to set up the database software in our system we need to take care of that database we need to take care of database security we need to take care of database backup database update lot of things that we need to take care in order to maintain a database which is required for our application here instead of we are managing the database we can use AWS RDS service that is a managed service in the AWS Cloud by using that service we can set up a relational database in the cloud Amazon RDS RDS stands for relational database service in the AWS here the advantage of going with this RDA RDS is several Administration tasks like availability of the database scalability of the database security of the database performance of the database backup such a kind of administrator activities will be taken care by AWS only you just need to set up your database in the AWS cloud and you need to connect to the database by using your application the remaining the management related Administration related tasks will be taken care by AWS Cloud that's why most of the projects in the companies most of the companies will prefer This Cloud databases because cloud provider will manage our database we will just use the database and we will pay rent for that this AWS Cloud providing the database based on pay as you go service how much you use for that you need to pay the money for AWS Cloud so I'm going to show you a demo how to create a database in the AWS cloud and how to connect with the database by using our spring boot application all right so here I logged it into my AWS account I have selected the region as a Mumbai now here AWS RDS service is available you can search for that service in this account currently no database is available let me click on create database here you can select what type of database you want to create relational databases like what I kill MySQL postgres SQL Server several databases it is supporting as part of RDS service so I am going for standard create database engines which are supporting by RDS Aurora bicycle mariadb postgres Oracle SQL Server like that several relational databases are supported by RDS I'm selecting mySQL database engine now in this mySQL database engine here MySQL version is selected as 8.0.30 I'm keeping like that I'm not changing that version and I am going here do you want this database for production purpose or due test purpose or free tire I'm using this for free tire I'm selecting this free tire here [Music] good now availability and durability settings database identifier name of the database so I'm not changing that identifier credentials you can change the username and password by default username is admin password you can select that password I am selecting password right confirm the password [Music] let me choose the password and let me enter that password so I have given password and confirm password instance configuration T3 micro instance then come down connectivity default VPC it requires a network to create the database AWS will provide one default VPC for the network the default VPC is selected by default and here you need to select Public Access yes we need to give the public access as yes because my application is running in my local machine my application should connect with the cloud database so public access the database should have a public access select this option as yes and the security group if you have Security Group already in the AWS account you can choose the existing Security Group if you don't have the security group then you need to create the security group and you need to select that Security Group I have selected my by default it selected default Security Group which is available and I don't have any availability Zone preference so password authentication Eternal configuration so here at initial database name so in the in the MySQL we need to create a database by using Create database database name if you want to create some database by default you can give the name of the database here I'm giving the database name as courses database some courses database I'm giving as a name then simply click on create that's it yes You observe what are the options that I have selected standard create option I have selected mySQL database I have selected and free tire I have selected then I have selected password username by default admin password I have given my own password then [Music] VPC by default default VPC only here I have changed Republic access to yes because we need to connect with the database from outside security group default only then in the additional configuration I have given the database name as courses database that's it yeah click on create database so when we click on create database it will take some time to create the database in the cloud database creation is in progress now you can see that database creation is in progress once this database is created we can see the database endpoint that means the username of the database and you can see credentials of the database here database creation is in progress your database might take few minutes to Launch credentials details when you click on this button it is giving the username of the database and it is giving password of the database this username and password one time downloadable guys so I'm going to take my database username as admin password Ashok 321 I can copy that password in our springboot application data source properties will configure in application.yml so in the previous video I have shown you how to connect with the mySQL database and how to perform crowd operations this project is developed in the previous video now here same application I'm using just I'm changing username and password username is admin and password is Ashok 321 earlier this application connected to mySQL database which is available in my machine that is localhost now I want to connect with database available in AWS Cloud so still database creation is in progress let's click on the database identifier once this database is created successfully here it will display endpoint endpoint represents the URL of the database and the port number for mySQL database default port number is double three zero six only so we need to wait till this database creation got completed once the database status is created it is active then here we can see endpoint URL and we can see port number right and meanwhile we have selected One Security Group for this database right that Security Group will stop our requests if you don't enable the particular protocol this database will be running on the protocol double three zero six let's go to the security group and you need to enable the port number the port number of this mySQL database is double three zero six all right go to Security Group in that Security Group inbound rules concept will be available so edit the inbound rules in the inbound rules you need to enable a protocol right which protocol we need to enable so we need to add custom DCP the protocol as double three zero six anywhere so we need to add that protocol and save that that means we are telling to this AWS Security Group to allow the request for that double three zero six protocol that is the meaning of that now still database creation is in progress let's wait still it is getting created instance database still the status is creating open the database and verify still endpoint URL is not generated we need to wait till the endpoint URL got generated right database status still creating yeah right guys now we can see that our database status is backing up now Here If You observe the endpoint URL got generated and the port number is also generated so endpoint nothing but the database host where the database is running and what is the port number it is using double three zero six double three zero six port number we have to enable in the security group inbound rule we have already done the process take this database host go to application.yml file instead of localhost now I am going to give the database host which is created in the AWS Cloud double three zero six port number I have given initial database name as courses database name I have given as courses and this is our driver class username password and database URL in the database URL earlier we have given localhost to connect with our local database now our database is created in the AWS Cloud so I am giving the localhost as AWS RDS database endpoint perfect so with this we are good now once this database is created are we able to connect with the database or not how to verify that for that we can use MySQL workbench let me go for a workbench and let me test database connection really the cloud database is working as expected or not once we take the confirmation then we will run the application and we will see that here I will create a new connection in this MySQL workbench new connection connection name AWS RDS MySQL and hostname hostname here it is displaying as endpoint take this endpoint name give it as a host port number double three zero six username is admin password Ashok 321 I have given [Music] click on OK now test collection check it as successfully made the SQL connection that means by using this MySQL workbench we are able to connect with the database available in AWS Cloud your first connection is successful here that means our application also can connect for that now let me connect to this and let me check database is created or not we have given the initial database name as courses so let me execute a command show databases I'm executing a command called show databases let me run this query yes so show databases see here I have executed it is giving the database name as a courses now use the database as database name use database name courses then show tables I want to display all the tables available in the database no tables available because it is a brand new database just now we have created now let's go to our boot application in the boot application we have repository we have entity class which is mapped to a database table and in the yml file we have given ddlr2 update table will be created in our database good run the project before that let me do a maven update update update the project force update okay once the project is updated successfully then let's run this project and see table is getting created in the database or not and we will insert one record also and we will test that good update successful run as boot application [Music] spring boot application is getting started this is spring boot application should connect with database available in the AWS Cloud because I have configured AWS RDS database properties in application.yml foreign [Music] started in the console you can see that create table query got executed so that means it connected with our database and it created table also in the database now let's go to workbench and let's check show tables earlier it has not given any result now execute show tables now you can see course details table got created in Cloud database then select star from table name what is the table name course underscore details let's select this query and execute it in the table we don't have any records now my application is a rest API so let me send you a request by using Postman and let me test record is inserting or not go to postman I want to send you a post request so here I'm selecting the request type as post localhost colon 8080 slash course so in our application we are having one method in the test controller that method is binded to post request it will take the course data in the request body as a Json that will insert the record into database table let us test this functionality localhost colon 8080 slash course data I am giving in the body as the adjacent click on send when we click on send here you can see on the console insert query got printed that means inside query is executed go to the database and check that select star from database yes one record got inserted we are able to communicate with AWS Cloud database by using spring boot application what we have done for that we created a database in the AWS Cloud we have taken endpoint of the database and we have taken port number we enable that port number in the security group double three zero six is the default port number the credentials of the database we configured in application.yml username password and database host and database port and database name with this we are able to understand how to communicate with the AWS Cloud database by using spring boot application thanks for watching this video please subscribe to our Channel and click on Bell icon for more updates
Info
Channel: Ashok IT
Views: 4,837
Rating: undefined out of 5
Keywords: Spring Boot, Java Development, Web Development, Backend Development, Spring Framework, Spring Boot Tutorial, Spring Boot Course, Java Spring Boot, Spring Boot Basics, RESTful API Development, spring data jpa tutorial, spring boot rest api development, spring boot with docker, spring boot with AWS cloud, spring boot with AWS RDS DB, spring cloud, spring boot full course, spring boot annotations, spring security, learn spring boot in simple way, ashok it, spring boot ashokit
Id: aNS9G5fyj5Y
Channel Id: undefined
Length: 161min 37sec (9697 seconds)
Published: Mon Jul 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.