Spring Boot Project | Banking Application using Spring Boot 3, Spring Data JPA (Hibernate), & MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone Rish here back again with another video in this video I want to build the restas for a simple banking application using spring board spring data jpa and mySQL database well we are going to basically build the simple version of banking application and we want to use the latest version of spring Boot and latest version of spring data jpa and latest version of myql database okay so without further Ado let's get started so just let us jump into the you know browser so I'm in a Chrome browser and open the new tab over here and just type start. spring.io this will brings up Spring initializer and here select the project MN and choose the language Java and let us keep the stable version of spring boot as up now that is 3.2.1 so this this is the latest and stable version of springbot as of now next go to the project meta data over here and here let us give group as net. Java guides and let's give artifact as banking app next project name is same as artifact that is banking app next description let us give the description something like demo project for sping boot banking app next let us give package name as net. java. banking next let us keep packaging as a jar and let's keep jav version 17 next go to the dependency section over here and here just search for web and let us select this spring web starter dependency well we use this dependency to build the rest apis and this dependency internally provides Apache tomat server as a default embedded container so let us select this dependency next let us type spring data jpa well we want to build the jpa repositories using spring data jpa well this spring data jpa dependency internally provides latest version of hibernate okay so let us select spring data JP over here next type MySQL so we want to use my SQL gdbc driver to connect our you know springbot application with the mySQL database next let us select the Lok dependency well we want to use Lok dependency to reduce the boiler plate code such as get setting methods Constructors two string equals and hash code methods okay so let us select lbook dependency over here all right so as up now these dependencies are enough for our spring boot application next go ahead and click on generate button over here so before that you can also explore the spring boot application using explore option over here so just click on explore and here go to the pom.xml file and you can see the springb version jav version and the dependencies as well okay perfect next click on close button and go ahead and click on this generate button to generate the spring boot application as a jip file okay next go ahead and open this Gip file in a folder perfect next let us unzip this Gip file all right next let us open the iner idea so here I have open inle idea next click on open option over here to open the existing project in my case the project is downloaded in a download spold so let me go into the download spold over here and next let me select the project let me click on open now we have successfully imported the spring boot application in in idea next let us go ahead and let us start you know developing this spring boot application next step is we will configure the my SQL database in our springbot application so that our spring app application can able to connect to the mySQL database so go to the SRC folder and go to main folder go to resources go to application. properties file so here inside application. properties file we want to configure the my SQL database details okay so before configuring myql database details in the application. properties file let us first create the database in a MySQL server so in our case we are going to use myql workbench to interact with the myql server so here let me quickly create the new database so here let us write the SQL statement create database followed by the database name so let us give database name as banking app next let us select this click on execute and next refresh Keat and here you can see banking app database is successfully created next we can make this you know schema or database as a default so that we can only focus on this database next let's go back to Inger IDE over here and go to application. properties file and here let us configure the you know mysq database properties so just type spring. dat Source do URL equal to jdbc and then MySQL and then Local Host and then Port 3306 and Then followed by the database name banking app so this is the GDB URL that our springb application will use to connect with the my SQL database and here we are using Local Host because our myql server is running in a you know local machine that's why the host name is Local Host and this is the default port for a myql server and this is the database name next let us configure the my SQ database username and password so here spring. data source. username in my case the username is root next let us configure the password spring. data source do [Music] password well in my case this is the password but make sure that whatever the database username and password that you have given while installing the MyQ database that you have to provide over here next let us tell hibernate to automatically create the tables using JP entities for that let's configure the property spring dot GP do hibernate do dtl hypen Auto equal to update all right so this property basically ensures that the hnet should you know create the database table automatically based on the JP entities that that we will create in the springbot application next we don't need to add hibernate dilate for a mql database because spring board 3 internally uses hibernate 6 and hibernate 6 will automatically configure the hibernate dilet for a database based on the jdbc driver dependency that we add in the spring boot application in our case we have added my SQL gdbc driver so hibernate 6 will automatically configure the hibernate d for a mySQL database okay perfect next let us run the springbot application so that we can make sure that our springbot application can able to connect to this banking app database so go to our main entry point class over here and From Here let us run the springbot application and here click on enable button to enable the lbo connotations and notice here there are no errors in a console locks it means our springboard application is successful connected with the myql database next step is we will create a jpa entity so before that let's quickly create the packaging structure for our spring boot application so right click on the base package new and then choose package let's give name as entity next let's create one more package right click new and then choose package let's go package name as controller next right click new and then choose package let's VI package name as service next let's create one more package let's call it as repository perfect next go to entity package right click on it new and then choose Java class let's give class name as account next let us Define the fields for this account class private long ID and then private string let's give a name as account holder name and then private double balance well to keep it simple let us have these three fields for our account class next let us make this class as a JP entity by using JP annotations so let us annotate this class with ADD entity annotation to make this class as a JP entity next let us configure the table details by using add table annotation so here let's use name attribute to give a name to the table let us say accounts perfect next let us use lbo conations to automatically create a get set methods for this account class so here let us use at get lbo connotation and next let us use at set lumo connotation next let us also use the Constructor related annotations for example no argument Constructor and all argument Constructor annotation next let us use few more JP annotations over here so go to the ID field and let us annotate this ID field with at ID annotation to make this ID field as a primary key in our database next let us use add generated value annotation to configure primary key generation strategy so this annotation has a strategy attribute and let us give the generation type as a identity so this will basically you know automatically increment the primary key next here let us use add column annotation to configure the column name for this field so here let us use name attribute and let's give column name as accountor holder name okay perfect now we have created account JP entity next let us run this you know spring application and let us see whether the hynet will automatically create a table for this account to JP entity or not okay so go to main ENT point class over here from here let us run the spring application I notice here there are no errors in the console let's go to database and go to banking app database refresh this imas go to tables and there we go accounts you know table is created and the columns like ID account holder name and balance it means the table is successfully created and the columns are also you know looks good okay so let's go back to integer idea next step is let us create spring data GP repository for this account entity okay so go to repository package right click on it new and then choose Java class choose interface and here let us give the interface name as account repository perfect next let us extend this interface from jpa repository interface and notice here jpa repository interface is a generic so we have to pass two parameters first is the entity name in our case account second is the type of the primary key in our case long okay so if you go to account GP entity so we have given the long you know type to this ID field right so this long we are passing as a second parameter to this GPA repository interface okay great well once we create account reposit interface that extends JP reposit interface then this account reposit will get a crude database methods to perform crude database operations on this account JP entity all right perfect next step is create add account rest API so before creating the rest API let us first create the service layer well the controller layer is depends on service layer so let us first create add account method in a service layer and then we will create the RP and then that RP will internally call the service method so here go to service package right click on it new and then choose Java class let us give I mean let us select interface and let us give interface name as account service okay next let us Define the method over here so let us say account and then create account and let us pass account well we can also use a dto to transfer the data between client and server okay so what we will do we will use the dto so let's create a new package for it and let's do package name as dto and here let us create a class and let us give class name as account D and here let us type the field private long ID and then private string account holder name and then private string let us go double and then balance all right and here let us use at data L connotation to automatically generate Constructors getter Setter methods next let's go back to account service interface and here instead of account JP entity let us use account dto so here also let us pass account dto okay perfect and let's view the name as account D and let's remove this unused import okay perfect next go to service package and let's create one new package and let's call it as IML impl stands for implementation so all the implementation classes we keep under this IML package so right click on this package new and then to Java class let's your class name as account service IML so this is the naming convention next this class implements account service interface and Implement its methods so here just move on this class click on this Implement methods select the method that you want to implement click on okay perfect next let us annotate this class with add service annotation to automatically create a spring Bean for this class next let us inject the dependency so here let us first declare the feied private account repository account repository next let us use a Constructor BAS you know dependency injection to inject this account repository dependency so here right click and then generate and then Constructor and select the dependency click on okay all right next here we can annotate this Constructor with at to annotation to automatically inject the dependency but spring you know 4.3 onwards we can ignore this at annotation because whenever spring will find the single Constructor in a spring beIN then spring will automatically inject the dependency okay perfect next go to create account method over here and within this method we need to convert account dto into account JP entity and then we'll save that account JP entity into a database so this is the conversion logic right so let us create one mapper class and let us you know write the common logic like we can convert a dto into JP entity and JP entity into dto so here go to dto package or else let's create one more package and let's call it as mapper next within a mapper package let's create a new class let's call it as account mapper so within account mapper class let's create static method public static and let us give the return type account and let's us give method name as map to account and let us pass account D2 as a parameter perfect next let us convert this account D into account so here let us create the object account and then account equal to new account next let us pass the details to the Constructor account d. get ID and then account d. get account holder name and then account D.G balance and then finally I return the account object now we have converted account dto into account JP entity similarly let us write one more static method to convert account JP en into account D so here just type public and then static and then return type as account dto let's give method name as map to account dto and let us pass account JP entity perfect next let us create account D object over here all right and let us pass values to the Constructor like account. get ID and then account. getet account holder name and and then account do get balance and then finally return this account D well let us see what is the compilation error over here it says the parameter Constructor is not available in account dto so go to account D here so this at data annotation don't provide the Constructor so here let us use add argument Constructor annotation okay let's go back to mapper class and there we go all right now we have created a mapper class to map dto into entity and vice versa next let us use this account mapper class in the service classes so go to account service simple class over here and go to create account method and here let's create object of account and then account and then let us call account mapper over here account mapper and then it has has map to account method and then pass account dto next let us call account repository over here and then it has save method so let us call Save method to save the account JP entity and then pass account jpn object to this save method so this save method basically return the saved entity so here let us introduce a local variable of type account and let's give variable name as saved account okay perfect and if you look at the create account method return type account D so let us convert saved account into account D so here let us remove this null and then call account mapper and then map to account D and then pass saved account okay perfect now we have created create account method it will call the account repository to save the account you know into the database next let us create the rtpi and then that RPI will internally call this method so go to controller package over here right click on it new and then choose Java class let us give class name as account controller let us annotate this account controller class with at rest controller annotation so this at rest controll annotation will make this class as a spring mu rest controller class next let us annotate this class with at request mapping annotation and let us pass the base URL over here let us say/ API SL accounts okay so this is basically a base URL for all the rest P that we are to Define within this account controller class okay great next let us inject the dependency for that let us first Define the field that is private account service account service next let us use a Constructor based dependency injection to inject this account service dependency so here right click generate and then Constructor and then click on okay now we have injected account service using Constructor based dependent injection and here again we don't have to use add at annotation because this class has only one Constructor so spring will automatically inject the dependency next here let's create a add account rest API while creating a rest API using spring boot is very simple first we need to create a method and then we'll make that method as a htpi by using spring annotation okay all right so just type public and then let us use response entity as a return type and this is generic so we can pass account d as a type let's go method name as add account and then pass account D2 as a parameter to this method all right perfect next let us have a return return statement over here return return new response entity and then call account service dot create account and then pass account dto next let us pass the HTTP status that is created HTTP status created perfect now we have created add account method it will you know call account service. create account method and this create account method basically return the saved account so this is the first argument second argument is HTTP status okay perfect next let us make this method as a rest TPA by using spring annotation so in in this case this method how to handle the HTTP post request for that let us use at post mapping annotation next here let us use one more annotation that is ADD request body annotation so this at request body annotation will you know map the request body to this Java object so request body basically contains Json so this at request body will automatically convert that Json into this Java object okay perfect next let us go ahead and let us run our SP application and let us taste this add account rest API so from here I'm going to stop the running application first and I'm going to run the application again I notice here our spring application is up running in a am Ser on Port 80 next let us open the postman res client and here let's create one new request and here let us choose HTTP post method and just type the request URL HTTP Local Host 8080 SL API SL accounts okay so if you go to Project again and if you go to account controller the URL is /i/ accounts so this is the URL we're using over here/ API / accounts okay next go to body and then select raw select content type as a Json and here we need to pass the Json in the request body so let us quickly create the Json object so if you go to Project again and if you go to account dto so this account dto has field account holder name and balance so these fields we have to use in a Json so let me copy this fre name and let me paste it over here and then let's you holder name in my case Rish next balance let us say 10,000 okay next go ahead and click on send button and there we go we got the response you know from the server and if you look at the ID here one so this ID we are getting from the database and look at the status 2001 created let us verify in the database so go to my SCH bench go to accounts table select rows and you can see the account is you know successfully stored in a accounts table okay perfect now we have successfully built add account rest API next let us build get account rest API so let's go back to our inage idea so here let's first change the service layer so go to account service interface and let us Define the method so here let's give method name as account D let's give method name as get account by ID and then pass ID as a parameter perfect now we have defined get account by ID method in account service interface next let us implement this method in a account service impl class so go to account service impl class over here just Mouse class click on Implement methods select the method that you want to implement click on okay next let us implement this get account by ID method over here so here just call account repository it has find by ID method and notice your find by ID method returns optional type account okay so just call find by ID and then pass ID over here and then call oral throw method over here well let us say the account with the given ID is not existing database then we have to throw the exception for for that we can use oral store method over here so just call this oral store method and then here let us pass the Lambda expression implementation well this ORS method takes supplier functional interface and we have to provide the Lambda expression implementation for this supplier functional interface so here is the Lambda expression to create a new exception and throw that exception new let us you know throw the runtime exception over here you can create your own custom exception and you can you know throw that exception from here so let us say account does not exist okay perfect next here let us take the return you know return value from this method let us say account next this get account by ID method returns account dto so let us convert this account jpn object into account dto for that let us call account mapper and then it has map to account D and then pass account perfect and here let me align this properly perfect now we have created get account by ID method in a account service ample class next let us create get account that will internally call this method so go to controller layer and go to account controller class and here let's create get account rest Epi so here let me first write the comment get account rest Epi let us type the method public and then response entity and then pass account dto as a type let's view method name as account get account by ID and then pass ID as a parameter to this method next let's call account dto and then call account service do get account by ID and then pass ID perfect next let us have a return statement return and then response entity dot okay and then pass account dto in a body perfect now we have created a get account by ID method so let's refactor this now we have created get account by ID method it Returns the account object and look at the okay method so this okay method internally Returns the HTTP status 200 okay all right perfect next let us make this method as an htpa by using spring annotations so this method how to handle HTTP get request for that let us use add get mapping annotation okay so this add get mapping annotation will map the incoming HTTP get request to this particular method next let us pass ID to this annotation within a double code SL within a curly braces just pass ID so this ID is nothing but a template you know variable next we need to bind the value from this template path variable to this method argument for that we can use add path variable annotation over here all right perfect now we have built get account htpi next let us rerun our spring application and let us test this get account rest API so from here let us stop and R the springboard application we notice here was spring applications up running in EM server on P 8080 next let's head over to the post client over here and here let's create a new request let us enter the URL http colon Local Host 8080 SL API SL accounts and then pass account ID one next click on send button and there we go we got the response from the server and look at here the ID One account holder name rames balance 10,000 and there TTP status 200 okay now we have developed get account rest API next let us build deposit rest API well basically we're going to develop the rest API that will deposit amount into a account holder name for example here the account holder is rames he has a balance 10,000 next we are going to deposit some amount into this you know rames account holder all right so basically we want to add some amount to the existing balance okay it's pretty simple so let us go to integer IDE over here and let us Post develop the service ler so go to account service interface and here let's give account d as a return return type let's give method name as deposit and let's pass first parameter as ID next let us pass second parameter as a amount that we want to deposit into account now we have defined deposit method in account service interface next let us provide the implementation for this deposit method in a account service class so let us jump into account service sample class Mouse over this class click on Implement methods over here and then click on okay next let us implement this deposit method well within a deposit method first we'll check whether the account with a given ID is exist in a database or not if the account with a given ID is not exis in database then we will throw the runtime exception right so here we have already written a piece of code so let me copy this piece of code and let me paste in a deposit method now we got the account for this particular account ID next let us call its get balance method and then add this amount to existing balance okay next here let us introduce a local variable double and let's call it as total equal to perfect next let us call account and then let's call it set balance method and then pass total okay perfect next we need to save this object into a database for that let us call account repository and then call S method and then pass account object perfect next this save method returns saved account object so here let us introduce a local variable account and then saved account next this deposit method returns account dto so let us convert this account JP in into account dto so here let us remove this null and then call account mapper it has map to account D and then pass saved account okay perfect now we have implemented deposit method it will deposit a amount into a account and the logic is pretty simple we will get the existing balance from the account and we'll add the amount to the balance and then we'll get the total amount and then we'll add that total amount as a balance to the account and then we'll save that account into database okay it's pretty simple next let us build the deposit RP that will internally call this method so go to account controller and here deposit rest TPA all right so here let's type public let's give return return type as a response entity and here let's go the type as account dto and let's give method name as deposit deposit and then let us pass two parameters to this method long ID and then second parameter as map it takes key as a string value as type double so basically we'll pass you know uh amount in a request body for that we'll use a map okay next let us call it as request per perfect next within this deposit method let us call account service it has deposit method and then let's pass ID as a first parameter second parameter we need to pass the amount so we can get that amount from the request object so here request dot get and then we can pass key as a amount okay next let us introduce a local variable amount account D account d okay next let us have return statement return response entity do okay and then pass account dto in a body now we have created a deposit method and then internally we will call account service. deposit method and then we will pass ID and then amount so we can extract this as a separate variable as well so here what we'll do we store in a local variable let us say double and then amount equal to request. getet and then pass this amount over here okay so this will basically improve the readability next let us annotate this deposit method with at put mapping annotation will we use output mapping annotation to map incoming HTTP put request to this method next within a double quote slash and then pass ID next slash deposit so this is the URL for this rest API next Let Us store the value of this ID into a method argument using at path variable annotation so here let us use at path variable anotation to bind value of this template you know URI variable to this method argument next here let us use one more annotation that is at request body annotation well this at request body annotation will map the request Json body into this map Java object okay perfect now we have developed deposit rest API next let us go ahead and let us run our sprut application and let us test this deposit rest API so here let me stop and reun the sprot application all right our sprot application is up running next let us head over to the postman climent door here and here let's create a new request and here let us choose HTTP put method and here let us enter the request URL so let me copy this and let me paste it over here next let's go to body and then choose raw and then choose content type Json and here let us pass the Json and the key as amount and the value let us say we want to deposit 5,000 okay that's it next click on send button so this is Method not allowed well here we need to give the correct URL so let's go to let's go to in idea and let's see the URL that we have given to the rest API we have given the URL id/ deposit so we need to append this deposit in the URL as well so here's SL1 SL deposit and then call I mean click on send button and there we go in a response you can see the Json account One account holder name raage and the balance is 15,000 earlier it was 10,000 now you can see the balance 15,000 okay it means we have successfully deposited 5,000 into account rames next let us say we want to deposit 10,000 next click on send button and you can see the response the total balance 25,000 it means the deposit rpay is working as expected next you can see the same change in the database table as well so go to account table right click select rad and you can see the balance 25,000 okay it means we have successfully built deposit rest Epi okay great next let us build withdraw s API so let us go to inte idea and let's create the withdraw htpi so let us first change the service layer and then we'll change the controller layer so go to account service interface and here let's define the method let's give return type as account D let's give method name as withdraw and let's pass parameter as long ID let's also pass the second parameter that is amount perect now we have defined withdraw method in a account service interface next let us provide the implementation for this method in account Serv service ample class so let us jump into account service ample class just m on this class click on Implement methods select the method that you want to implement click on okay perfect next let us implement this withdraw method over here so here first we'll check whether the account with a given ID is exist in a database or not if the account with a given ID is not exist in database then we will throw the exceptions like account does not exist okay so that piece of code we have already written in a you know existing methods so we can copy that piece of code from here so go to deposit method and copy this piece of code okay and again go to withdraw method and here simply paste it okay perfect next let us add one more condition like if the withdraw amount is you know greater than the balance then we'll throw the exception like insufficient amount okay so here let's add the condition and then account. get balance so if the account balance is less than the withdraw amount then we will throw the runtime exception so here throw new runtime exception and let us pass the message in sufficient amount perfect next here let us write the logic double total equal to so here just call account do get balance minus amount so in this case we are withdrawing the amount from the balance that's why we are using a substraction okay next let's call account dot set balance and then pass total okay perfect next let us call account repository and then call Save method and then pass this account object next this save method return the account know saved account object so let us introduce a local variable of type account let's call it as saved account perfect next if you if you can look at the withdraw method over here it Returns the account dto so let us convert account entity into account dto so here let us remove this null and let's call you know uh account mapper and then it has map to account dto and then pass saved account okay perfect now we have implemented withdraw method internally it will call account deposit do find by ID method let us say if the account with a given ID is not existing database then we will throw the runtime exception next we have added one more condition like if the withdraw amount is greater than the balance then we'll throw the runtime exception like insufficient amount next let us say the balance is greater than the withdraw amount then we'll write the logic like we will withdraw some amount from the existing account balance and then we will add that total to the account and then we'll save that account into a database and then we'll return the updated information okay the logic is pretty simple next let us build the withdraw HTP that will internally call this method so go to account controller so here let me first write the comment withdraw rest AP so just type public and then response entity and then pass account D let's give method name as withdraw and then pass two parameters po parameter as ID second parameter as a map the key type is string the value type is double and let's call it as request perfect next let us get the amount from the request so here double and then amount equal to request dot get and then pass amount all right next account dto account dto equal to account service do withraw and then pass ID and then amount next let us have a return return statement return return response entity. okay and then pass account dto in a body perfect now we have created withdraw method it will internally call account service. withdraw method and it will pass account ID and the withdraw amount okay next let us make this method as a RTP by using spring annotations so here let us annotate this method with at put mapping annotation well this put mapping annotation will map incoming HTTP put request to this method and then let us pass the ID and then withdraw perfect next let us bind the value of this ID to this method argument by using at path variable annotation next let us use one more annotation that is ADD requ PR body annotation now we have built withdraw rest TP next let us re our sprot application and let us test this rest DPI so from here let us stop and run the spring boot application all right our sprot application is up running let's go to postman client over here and here let's create a new request so let me copy the request URL from the existing rpay request so here let us select HTTP put method and then pass request URL Local Host 8080 /a/ accounts SL the account ID and then withdraw all right so here we're going to basically withdraw some amount from the account with ID one next let us go to body select draw choose content type Json and here let us pass the withdraw amount let us say I want to withdraw 5,000 next click on send button and there we go in a response you can see the account holder name and the balance earlier the balance was 25,000 now you can see the balance 20,000 because we have withdrawn you know the amount 5,000 okay next let us say I want to withdraw 5 more th000 then what I can do is I can click on send button and you can see the response the balance is 15,000 next let us say I want to withdraw 10,000 then the balance should be uh 5,000 right so let's click on send button and you can see the balance 5,000 it means the withdraw htpi is working as expected next let us build get all accounts rtpi well first we'll change the service layer and then we'll change the controller layer because controller layer is depends on service layer so let us head over to account service interface over here and within this interface let us declare a method so let's give the method return type as list list is a generic so let us pass account dto as a type let's give method name as get all accounts perfect next let us go and let us implement this method in account service impl class so let us jump into account service impl class over here just Mouse over on this class and here you will get a link to implement the methods click on this Implement methods select the method that you want to implement and click on okay next let us write the logic to implement get all accounts method over here so within a get all accounts method let us call account repository it has find all method and Noti here find all method returns a list of account so just call this findall method over here next let us get the result of this findall method in a list so list and then account let's call it as accounts okay now we got a list of accounts next let us convert this list of account into list of account D so here let is call accounts so accounts is a list and list has a stream method so here just call stream method and then stream has a map method so let us use map method to map entity into dto and look at your appap method takes function as a functional interface we need to provide the Lambda expression implementation of this function functional interface so let us call this map method over here and let us pass the Lambda expression implementation well here let us pass account entity object as a parameter to the Lambda expression and then this is the Lambda symbol and then call account mapper it has map to account d method right and then pass account entity object next let us call collect collectors. to list method okay perfect well here we are using account mapper do map to account D2 method to map account entity object into account dto and finally we are returning the list from the stream and this list basically contains the list of account D okay so here let us have a written statement and remove this return null okay now we have implemented get all accounts method it will return the list of account dtos next let us create get all accounts HTP that will internally call this method so let us head over to account controller over here and here let me write the comment get all accounts rest API perfect so here let us first create the method and then we'll make that method into rtpi so just type public and then response entity response entity is the generic let us pass type as a list and again list as a generic so let us pass account d as a type let's do method name is get all accounts okay next within this method let us call account service and then call get all accounts method so this get all accounts method return list of account dto so here let us have list and then account dto let's call it as accounts perfect next let us have return return statement and then response entity it has okay method and then pass accounts in a body perfect now we have created a method called get all accounts it returns a list of account DS next let us make this method as a rest TP by using spring annotations so here let us annotate this method with ADD get mapping annotation okay that's it next let us go and let us rerun our spring application and let us test this get all accounts rest API so from here I'm going to stop on the springbot application we notice here our springbot application up running in EMB server on port 8080 next let us head over to the p person client over here and let's create a new request and let us choose HTTP get method over here and let us enter the request URL well let us copy from the previous rest API and just paste it over here and here let us remove these parameters and just keep Local Host 8080 sl/ accounts next go ahead and click on send button and there in a response you can see a list of accounts so as of now there is a only One account now what we'll do we'll add a few more accounts okay so go to the add account hpay over here and here go to the body and just change the account holder name from rames to John and then balance let us have 20,000 click on send button now the John account is created next let's create one more account let let us say Sanjay and then let's say balance 30,000 click on send button and there we go sanj account is created next let's go to get all accounts HT API and next send the request and there we go in a response you can see a list of accounts and there are total three accounts in the list all right it means the get all accounts is working as expected all right great next let us go let us build delete account rest API well first we'll change the service layer and then we'll change the controller layer so go to account service interface over here and here let's define the method so let's give return return type of the method as a wide and let's give a method name as delete account and then pass account ID as a parameter now we have defined delete account method in account service interface next let us implement this method in account service impl class so let us jump account service ample class over here and just Mouse on this class click on Implement methods select the method click on okay all right next let us implement this delete account method over here well first We'll add the condition like if the account is not exist for a given ID then we will throw the Lear time exception okay so that logic we have already return in a withdraw method and deposit method and get account by ID method right so let us copy this piece of code from the get account by ID method and then let's go back to delete account method over here and within this method let us paste this logic so here basically we are adding the condition like if the account with a given ID is not exist in database then we will simply throw the runtime exception like this okay great if the account with a given ID is exist in database then we will you know delete that account so for that let's call the account repository it has a delete by ID method and then pass account ID all right perfect so this delete byid method returns a wide so we don't need to return anything now we have implemented delete account method it will delete a account by given ID next let us build the delete account reg that we will internally call this delete account method so let's go to account controller over here and here let's write the comment post delete account rest API just type the method public and then response entity let us pass string as a type let's do method name as delete account and then pass account ID perfect next within delete account method let us call account service it has delete account method and then pass account ID so this will basically delete the account by ID next let us have a return statement return response entity do okay and then pass the success message here let us say account is deleted successfully something like that perfect now we have created delete account method it will delete the account by using account service. delete account method and return the success message next let us make this method as a rest by using spring annotations so here let us annotate this method with at delete mapping annotation we use add delete mapping annotation to map incoming HTTP delete request to this particular method next let us pass the urri template variable over here so here bra within a double code slash within a curly braces ID so this ID is nothing but a UI temporate variable and this ID basically contains the account ID so we need to find the value of this ID into the method argument for that let us use add path variable annotation okay perfect now we have implemented delete account htpa next let us run our sprot application and let us test this delete account htpi so from here I'm going to stop and R the spring board application well notice here our springut application up running next let us head over to the post client and here let's create a new request let us choose HTTP delete method over here and here let us enter the request URL so let me copy this and let me paste it over here Local Host 8/ / accounts so let us say we want to delete the account with id1 so just pass ID one in the URL next click on the send button over here and there we go you can see the response account is deleted successfully with HTTP status 200 okay next let us verify in the database so let's go to MyQ here and go to accounts table select rows and notice here the account with ID one is deleted from the accounts table next let us delete the account to the ID to as well so let's go to personal client and here let us pass the account ID to in the URL and click on send button and there we go we got the success message next let's verify in the database so here let us refresh the table that is Select R from the table and there we go the account with id2 is successfully deleted it means the delete account reg pay is working as expected all right great
Info
Channel: Java Guides
Views: 132,726
Rating: undefined out of 5
Keywords: spring boot project, spring boot banking application, spring framework, java, java guides
Id: 5i379k0Xh_s
Channel Id: undefined
Length: 61min 19sec (3679 seconds)
Published: Sat Feb 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.