CRUD Operations using Spring Boot in IntelliJ IDEA | CRUD REST API | Postman | MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone ramesh here back again with another video in this video i want to show you how to create a spring boot crude or sdip application using intellij idea well i have already published many videos on you know how to create spring boot crude apis using eclipse sts id okay and many of you guys suggested me to use english idea to build spring boot crude rest apis well in this video we're gonna you know use english idea to create a springboard crude rest apis line by line coding and when i use mysql database and when i use postman rest client to test all the spring boot arrest apis and when i use ping data jpi which internally uses hibernate to build a repository layer or a tower layer okay great without further ado let's go ahead and let's create a springboot project using spring initializer and let's import springbook project in english idea and let's go and build spring boot crude rest endpoints line by line coding in english idea alright great in order to create a springboard application we are going to use bing initializer well let's head over to your pivot browser i am in a chrome browser and in a new tab just type start dot spin dot io this will bring some spring initializer website okay so spring initializer is basically a website which we can use to create spring boot application okay well here we have a project types like maven project and gradle project let's keep marvin project selected as it is and language java spring boot version 2.5.5 so don't choose snapshot or other releases just keep default selected one that is 2.5.5 so this is the latest and stable version of spring boot now go ahead and enter the project metadata over here so let's give group id as net dot java guides and artifact id as spring boot back end and description something like demo project for spring boot rest apis all right and package name net dot java guy dot spring boot perfect packaging keep jar and java version 11 you can choose 17 or 8 as per the jdk installation on your machine okay so go ahead and click on add dependencies over here and here we are going to choose the dependencies let's go ahead and let's choose spring web dependency well we want to build a rest apis guys that's why we are going to choose spring web dependency well spring web dependency we use to build rest apis as well as spring game uc applications and spring web dependency internally provides apache tomcat as a default embedded container so go ahead and pick up this dependency apart from that we are going to use spring data jpa well in order to talk with the database we are going to use spring data gpa and spring data gpa internally uses hibernate as a default jpi provider okay so spring data jp is very uh useful module guys it reduces a lot of boilerplate code that is required to develop the dowel layer or repository so we are going to use spring data gpa so apart from that we are going to use a mysql database at the back end so in order to connect mysql database from our springboot application we are going to use mysql jdbc driver all right so apart from that we need a longbow dependency to reduce a boilerplate code okay so these are the dependencies that are enough to create our screen boot application and exposing crude arrest apis okay so next you can explore this project by using explore option over here for example you can see what are the dependencies you have added to springboot application you can explore the project structure okay so just go ahead and close it and click on generate to generate this project as a zip file on your file system so go ahead and click on generate so look at here this springboot application is downloaded as a zip file now let's go ahead and open in a folder and just extract this zip file okay now open english idea and import this spring boot application in integer idea so i have open integer idea and here you can see open button so you can click on open button to open existing project so go ahead and click on open and go to the directory where your project is downloaded so my project is downloaded under downloads folder so i go to download folder and just select springboot backend project and open in a integer idea okay so this will take a couple of seconds to import springboot project properly in english idea all right our springboot application is properly imported in integer idea next what we're gonna do is we're gonna create project structure for our springboot application well just expand expandable project go to root package right click on it new and then choose package let's queue package name as model so within a model package we keep all our jp entities so let's go and let's create one more package right click new one package and let's give package name as controller so within a controller package we keep all ours being a music controller so let's go and let's create one more package and let's call it as repository so within a repository we keep all our spring data jp repositories so right click on root package new and then choose package and let's create one more package something like exception all right once we have created a packaging structure for our springboot application next we're gonna configure mysql database in our springboot application we'll go to a resource folder within resource folder you can see application dot properties file so all the application level configuration we keep within our application.properties file so before configuring mysql database in springboot application first we need to create a database in mysql server so let me go to the terminal over here and let me connect to mysql server by using a terminal or command prompt if we are using mysql workbench then you can also create a database using mysql workbench okay so i have connected to mysql cell and here i'm going to create a database just exhibit the query create database and let's say any of the databases ems that is employee management system hit enter and there we go we have created create database and we have created ems database if you want to see the database just type the command show databases and you can able to see here ems databases listed in a mysql database server all right now let's get back to intellij idea and here we're going to configure mysql database configuration in springboot application so within application.properties file just type these properties spring.datasource.url and jdbc mysql localhost 3306 the port of mysql server ems is the database name just we have created and we have a disabled ssl and username root and this is a password for my mysql installation so make sure that you have you know replaced with this username and password as per your mysql installation okay i have seen that many of you guys don't replace and don't replace this username and password as per your mysql installation and you cannot able to connect your springboot application to mysql database so make sure that you will replace my sequel username and password as per your mysql installation in your machine and then we need to specify the hibernate dilate for my sql database okay so hypernet internally uses this dialect to create a sql and execute sql as per the database so in our case we are using mysql database and here we are using update value for ddl auto property because this property is very useful uh while development guys so this property i know will update the existing tables if tables are not existing database and if tables are not existing database then it will create a fresh tables okay so it won't delete the existing tables let's say if we add a new column to the existing table then it will simply alter the table it won't delete the table okay so very useful property but if you are deploying your application on production then make sure that you use the property value as validate or create okay create or drop for development i'm going to use update okay so these properties you can you can copy from my github repo okay i will host source code of this project after completing this course and you can get this code from the github repo or i will give a link in the video description for the same and you can also copy this code from my blog post okay i will i will provide a link in the video description all right guys now we have configured my sql database in springboot application let's run our springboot application let's see whether our springboot application is able to connect to my sql database or not okay go to springboard main entry point class and here we can run this class as a as a java main class okay because this class has a main method so we can run this application as a standalone and if you can see in a console springboot application is up and running in embedded template server on port 802 and there is no error in a console it means that our springboard application is successfully connected to mysql database let's go to model package right click on modern package new and then choose java class and let's give class name as employee hit enter and let's define few instance variables private long id so this will you know become a primary key for employees table and then private string first name first name of the employee similarly let's have a last name of the employee let's create one more field something like email id okay great let's use a lumbar connotation to reduce a bilateral code so when i use that add getter number connotation to create a getter methods for these private queues and i am going to use add setter number connotation to create a certain methods for this private page and i'm going gonna use no arrangement constructor and also we're gonna use all argument constructor okay great so instead of these two dependencies you can also use add data lumbar connotation for example i had data lumbar connotation but at the lumbar connotation internally provides so many things like it internally generates two string method hash code method equals method okay so we don't want all those methods so we're going to simply use add getter and add setter methods here set annotations over here okay great now i'm going to use jp annotation to make this class as a jp entity let's add add entity jpi notation to make this class as a jp entity and when i use add table annotation to provide a table details over here so let's give table name as replies you can also specify schema and other details okay to keep it simple we just give table name to this class and apart from that we're gonna use at id jpi rotation to make this id as a primary key and also we're gonna specify at generated value annotation here because we need to provide primary key generation strategy right so let's do identity as a primary key generation strategy and here when i use add column annotation to map a column name or to create a column name for this field let's give column name something like first underscore name and you can also specify a label here if you specify a label equal to false then this column becomes not null okay so go ahead and remove this similarly let's give column name for last name as well name equals to last underscore name okay and here we're gonna specify name for email email underscore id okay perfect if you don't specify add column annotation then jpa internally you know by default pick up this this property name as a column name okay if you don't specify at column annotation like this then jpeg is smart enough to give a column name as name of this field okay so we want column name as email underscore id hence we need to specify this annotation now what we'll do we'll run our springboot application and we'll see whether this employs table will get created in a database or not let me go to spring main entry point class and let me run this springboard project and our springboot application is up and running in ambient termcat server in port 8080 now let's head over to terminal if you have mysql workbench then you can go ahead and you can just open ems database okay i say use ems here database changed and i'll say show tables and there we go employees table is successfully created in ems database it means that our gpa entity created successfully okay next what i'm going to do is we're going to create spring data gpa repository go to repository package right click new and then choose java class and choose interface here and just give interface name as employee a repository hit enter and this repository should extend jpa repository and jpeg repository interface expect two parameters first is type up the jp entity second type up the primary key so we have given type as long to the primary key hence we have specified employee as a long as a type here as a second parameter and this is the employee gp entity type okay and here we don't have to add a repository annotation here because spring data jp internally take care of adding at repository annotation to to jpeg repository implementation class okay great now we have created employee repository which extends jp repository now this employee repository will get all the crude database methods okay all crude database methods to interact with the database all right once we create employee repository next we're gonna create one custom exception that is resource not found exception so whenever a required not existing database table then our application should throw the exception uh that exception will be again sent back to the client right so for that go to the exception package right click on it new and choose java class and let's your class name is resource not phone exception and hit enter and this exception should extend run time exception okay and go ahead and create one constructor over here public resource not found and here we're gonna pass message and this constructor takes a parameter as a message okay great now what we're going to do is we're going to annotate this exception with one very important annotation that is at response status annotation so we can also annotate this uh we can also use this annotation in rest api but instead of repeating this annotation in each and every rest api which throws this exception we can simply add this annotation in a common place okay in this custom exception itself we no need to repeat this annotation in all the rest apis which throw this exception okay so whenever a rest api is through this exception the rest api should return this exception message to the client right with status code not form for that we're going to define a value here status value is not not found right http status dot not found okay great well go to controller package right click on controller package new and then choose java class and let's give class name as employee controller let's annotate this class with add rest controller annotation and attached controller annotation internally annotated with add controller and at response body or rotation so once we add this annotation that is a rest controller notation to this class this class becomes spring mvc rest controller and capable to handle the http request and all the rest api that we are going to build that we will define within this class okay so now go ahead and auto wire employee repository private employee repository and employees positive great now let's create a method public with written a list of employees so you list up type employee that's your method name as get all employees okay and we don't have to pass any argument to this method so this method simply returns list of employees so we need to fetch a list of employees from the database for that we're going to use employee positive dot final method so look at your find out method returns a list of objects in our case list of employees all right now let's annotate this method with add get mapping okay now what we're going to do is we're going to define a common base url for this rest apis for that we're going to use add request mapping annotation here and here give a base url for all these rest apis that is api slash v1 slash employees okay great now this api will return a list of employees to the client okay great now before you know testing this rest api what we're going to do is we're going to insert few records to the database so that whenever we test this rest api this rest api will return those records from the database so go to main entry point class and here just implement command line render interface this will provide a run method and this run method basically executes whenever his premod application starts first time okay before writing any code within a run method first we're gonna add wire employee repository perfect now within a main method create object of employee and just set value to the employee employee dot set first name let's say ramesh employee dot set last name birthday and employee dot set email id remish at the rate gmail.com and i'm gonna call employee repository dot save method to save this employee object to the database similarly let me copy this code and let me i know insert one more required copy this paste it here and just rename the object employ one employee one okay and just change the values let's say john cena and email as seen at the gmail.com okay great now what i'm going to do is win a runner's premium application and we'll test get all employees rest api so go to main entry point class again and from here we can run our springboot application our spring boot application is up and running in embedded tomcat server on port 8080 let's head over to postman client so i'm in a postman client i will create a new request over here and choose http get method and type http colon local host localhost colon eight zero zero slash api slash v1 slash employees and go ahead and click on send button and there we go hdpa return a list of employee objects okay we have successfully got a response of the rest api as a list of employees it means that we have created get all employees html successfully we are going to build create employee rest api so create employees api basically handle http post request and it will get a employee json object from the request and it will convert it into employee java object and it will save employee java object in a database and it will return the employee saved object to the client all right go to employee controller and here let me write the comment build create employee rest api okay and just create a method public and the written type of the method is employee and give method name something like create employee and just pass method argument as employee object okay and this method returns the saved employee object okay just call employee repository and call the same method so save method takes entity object so just pass empire object and that's it so co method basically returns the saved object in our case its employee object okay let's annotate this method with add post mapping annotation to handle http post request and we no need to give a url something like slash employees over here okay because employees already added in it and a base url okay let me remove this perfect and here you can see method argument employee so we have an annotation in spring that is at request body annotation so this annotation will basically convert a json into java object okay great now let me save the file and let me start the springboot application and then we'll test this rest api using postman rest client all right our spring boot application is up and running let's head over to postman client and create a new request and make sure that you choose http post here and just type here http localhost colon8080 slash api slash v1 slash employees okay and go to body and go to raw and here choose content type as a json because we are passing json in request body and here we need to prepare the json well if you can see the employee object that we have passed to this create employee method it has first name last name and email id so we need to prepare json such a way that we need to pass key as a first name last name and email id so key should be same as first name last name and email id all right so go ahead and in a body section just add first name okay and value as let's say remesh and let's say last name so we have a record already with the name ramesh so let me add different record here something like okay and your last name is the first array and email id email id let's do something like further at the rate gmail.com okay and go ahead and click on send and there we go we have already two records in our database table now third record is inserted and let's take a return third record with id 3 okay it means that our create employee html is working as expected let's go ahead and let's build get employed by direct api so head over to empire controller and within the employee controller we are going to write the get template id rest api so let me write the comment over here build get employee by the rest api well let's go ahead and let's create a method public and the written type of method let's go ahead and let's use response entity so response entity is very useful class that we can use to construct the response of the rest api so go ahead and choose response entity and this is a generic class so we need to pass the type in our case we need to pass employee let's give method name as get employee by id okay and we need to pass the method argument for this method let's say id of type long now we need to fetch ample object from the database for that what i'm going to do is we're going to use employee repository to get the ample object from the database so let me first declare a object over here and let's use employee depository and it provides a method called find by id so find by id method takes id of type long i need to it returns the optional class of type employee so go ahead and choose this method and just pass the id well find by id method returns optional class and option class provides the method or else throw method well if record is not exist with a given id then our https should throw the exception and our html should return our errors response to the client right in order to uh you know handle this use case what we're going to do is we're going to use oreos throw method uh to create a custom exception and throw the custom exception okay so go ahead and choose or else throw method and if you can see here or else the method takes supplier as a you know argument supplier is nothing but a function interface and we need to provide uh you know a lambda expression implementation for this interface so go ahead and choose this method and we are going to write a lambda expression and here is the syntax new and then create object of resource not found exception and throw this object okay let me break this sentence here and we need to pass the custom error message something like employee not exist with id and just pass the id over here okay great that's it now we got ample object from the database now this method should return a response entity and just call ok method here so we're going to call ok method because it internally provides 200 status code we don't have to manually specify 200 status code as a response of this you know response entity class we simply call okay method and we need to pass a body in our case we need to pass employee object in a body that's right now we have created a method which returns a employee object let's make this method as a rest api by using spring provided annotations so let's go ahead and let's add aggregate mapping annotation at the top of this method okay and we need to pass id so within a curve basis provider id and we need to you know bind the value of this id to the method argument for that i'm going to use add path variable annotation that's it pretty simple isn't it now save this class and go ahead and run springboot application again well our springboard application is up and running let's head over to personal client and let's test this rest api so in a prosperon client create a new request to test this rest api and make sure that you choose http get method over here and just enter the url http localhost 8080 api slash v1 slash employees and just pass the id here we are passing id one uh we need to get a employee with id1 right that's the reason i'm passing one here and go ahead and click on send button and you can able to see here uh rest api return the response with the you know id1 okay we got a response as an employee object with id1 if you want to retrieve employee with id2 then go ahead and pass id 2 over here and there we go the sdf return i employ object with id 2. if you want to get a employee with id 3 you can just pass id 3 and hit send button and there we go rest api return i employ with id3 it means that get employee id rest api working as expected let's go ahead and let's build update employee rest api so head over to employee controller again and within employee controller we're gonna write update employee rest api so let me quickly write the comment over here build update employee rest api okay great now quickly write the method public and the response type we are going to choose here response entity well response entity is very useful class to construct the response object so go ahead and choose response entity response entity is a generic class we need to provide a type let's go and let's provide a type as an employee and go ahead and give method name as update employee all right i need to pass first argument as id and second argument as the type of the employee object update details let's say employee details okay so employee details uh basically this object we are getting from the client so client call this rest api that is update employees dpi and it send updated employee information in the request right and that information we are going to bind to this employee object all right great now what we're gonna do is we're gonna get employee object from the database and we need to update the employee object and we need to again save that employee object to the database for that let's go ahead and let's first fetch employable from the database so in order to do that let me declare object here employee and let's say update employee and when i use employee repository to get a object from the database so employee repository provides a method that is defined by item method bind by id and find id method takes id as a method argument and it returns optional class of type employee so go ahead and choose this method and we need to pass id all right let's say uh a object with given id is not existing database then our stp should throw a resource not found exception right for that we're gonna use oral stool method and this method internally takes supplier as a function argument and we need to provide a lambda expression implementation for this supplier interface all right so go ahead and call this method and just throw the exception and so we're going to provide lambda expression right so here's the syntax we're going to create object of the resource node from exception and this method will simply throw this exception object so let me break this sentence and let's give a custom error message something like employee not exist with id and just pass the id okay perfect now we retrieved empire object from the database okay and here in the method argument we have one more object right and this empty object contains all the updated information now we need to you know set all the information from this object to update employee object for that what i'm going to do is we're going to simply call get sector methods to set first name last name and email here we're gonna use employ update employee dot set post name let's get first name from the employee details object similarly last name okay update employee dot set last name and get last name from employee details similarly get email from employee details and set to update template object name for details dot get employee get email id all right it means that user can update employee first name last woman email okay now update employee contains all the updated information now we want to send we want to save this employee object in database for that again we need to use employee a repository and let's call it save method to save this object now once we save updated employee object to the database we're gonna simply return the updated information to the client all right again we're gonna use the response entity dot ok method and then i'm gonna pass update employee object in a body that's it pretty simple isn't it so what we have done we have retrieved employee object from the database and we have set all the updated information to update employee object and we have saved that employee object to the database and we have simply returned the updated employee object to the client now we have created a method now let's make this method hdpi by using annotations so let's go ahead and let's use at put mapping annotation so whenever you have you know requirement to update the resource then you have to use output mapping annotation okay just try to understand the difference between post and put so post is used for creating a resource put is used for updating the resource well now we need to pass the method argument that is id so we can pass id as a path variable in a url and we need to bind the value of this id in the method argument for that we're going to use add path variable annotation we have a second argument that is employee details well we have one more annotation that is request body annotation so this add request body generation internally convert json into this employee details object all right so this completes our update employee rest api implementation now let's go ahead and let's start springboard application and let's test this update employee rest api using postman dash client so let me go ahead and let me restart the server so our spring boot application is up and running now let's go ahead and let's test this rest api using postman's client so let's head over to first manage client and create a new request to test this rest api and make sure that you choose put http method over here and here just type http colon localhost it's red00 slash api slash v1 slash employees okay and we're going to update employee with id one so i'm going to pass one id in a url itself okay and go to body and in a body we need to pass the employee information okay and here i choose content type json and in a body we just need to update employee first name last name and email okay so let me go to get request here and here i will just pass one to get a employee object with id 1 to just check employee first name last name and email so you can see here employee first name remains last name email id remember it gmail.com okay so notice here the id one now we want to update this employee first name last name email id okay right now we have a rematch for the rearranged gmail.com so let's go and let's use this put request to update this information so let me simply uh update first name from ramesh to ram from jado an email id ramadre gmail.com okay so go ahead and hit send button and there we go the employee id with one is successfully updated okay what are the fields updated first name last name and email id okay and if you can make a get request to get an employee with id1 you will get updated information for example let me hit a send button here and there we go first name last name email id is successfully updated for the employee one it means that our update employee rest api is working as expected let's go and let's build delete employee rest api let's go to employee controller within employer controller let me quickly write the comment over here build delete employee rest api let's write public and let's use response entity as a return type and this is generic clause so let's pass http status as a type let's give method name as delete employee and just pass the method argument id all right now we want to check whether a employee record is exist in a database with this given id or not for that let me declare employee object here and let's call employee repository and then find my id method just pass the id all right and just call or else throw method well or else the method internally uses supplier function interface so we're gonna use lambda expression to implement supplier interface so go ahead and use lambda expression now new and then just create object of resource not for an exception and just pass a custom error message over here employee not exist with id and just pass the id as argument perfect now we have just added a condition just to check whether employee is exist with a given id or not if employee does not exist then this will throw a resource not for an exception well now go ahead and use employee repository dot it provides the delete and delete by id method so now we have employee object so we can call delete method and we can pass the employee object to this delete method so that this delete method will delete a record from the database and look at here the return type it doesn't return anything okay why it is a written type so go ahead and choose this method and just pass employee object to it so this will delete an employee record from the database all right now go ahead and just return status code new and then response entity and here in a body we pass http status dot no content all right so we're not returning anything to client hence we're just passing http status as no content all right now we have created a method that is delete employee method let's make this method as a rest api by adding annotation so let's go ahead and let's use add delete mapping annotation and we need to pass id as a template variable and we need to bind the value of this id to method argument for that let's go ahead and let's use add path variable annotation okay now we have completed delete employee rest api let's go ahead and let's test this rest api using postman client let me start the springboard application all right our springboard application is up and running let's head over to postmodernist client and let's create a new request to test this rest api and here make sure that you choose delete http method over here and we're gonna use the url http localhost 800 api slash v1 employees and we need to pass the id in the url for example here i am passing id 1 to delete a employee with id 1 ok so go ahead and click on send and there we go we got a status code 204 it means that our employee with id one is deleted successfully in order to verify this we can make or get employable id rest api call so i am going to hit a send button and there we go not found it means that employ one is not exist in database because we have just deleted it right let me delete one more employee let me pass two and hit send button and there we go we got a status code 204 and let me verify it by using get employee by id rest api so this is the get so i am going to pass id 2 and hit send button there there we go not found okay it means that our delete employee rest api is working as expected
Info
Channel: Java Guides
Views: 6,942
Rating: undefined out of 5
Keywords: spring framework, java, spring boot, postman
Id: aCZmPgBHc88
Channel Id: undefined
Length: 48min 17sec (2897 seconds)
Published: Fri Oct 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.