How to add Swagger in Spring Boot Project | Spring Boot Swagger | @javacodeex

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my channel in this video we'll explore how to add Swagger in Spring boot application so why we are using swagger we are using swagger for API documentation so if you're integrated Swagger with springboard application then you can use Swagger UI for API testing also now we'll create one spring boot application and we'll add Swagger UI into spring boot application first we'll create spring boot application and in that we'll add one user entity and we'll perform card operations from Swagger UI to create springbot application we need to go to the start. spring.io and if you see here my springboard application version is 3.3.0 and I giving group ID and artifact ID I'm giving user service this is a package name I'm using Java 17 and dependencies we'll add web and we'll deal this one with H2 database we need jpa and one more dependency we need lamb now we'll generate this project so project is generated we'll import this project into intellig ID so project is loaded into intellig ID so if you see this pom.xml file so we are using data jpa web and H2 database so apart from this one we need one more dependency for Swagger UI that is web MVC UI Open start web MBC UI this is the dependency required you need to add into the form. XML F now we'll create project structure like d now we'll create required packages so we need entity and next repository and next service then we'll create controller so with this project structure required packages are created now we'll create one entity in the entity package so here our entity is user so we'll perform card operation on this user so what the properties we requir we'll use user ID name and email private integer ID private string first name private string last name private string email these are the four properties so we can make this one as a entity class so annotation entity and we can add annotation data from lambach then we'll create one more annotation is required this is for AA specification purpose we'll use schema so in the schema we'll Define the description so description is this is user entity now id id we need to make autogenerator for that one we need to use annotation ID and annotation generated value and we can pass strategy generated type identity will use so it will generate automatic ID for each user cre creation here also we need to use annotation schema so if you see this Swagger version three so here also we can Define the description this is ID why we are using this ID unique identification for the record so you can mention the description for this unique identifier of the user so for example it is one user ID like it will generate like something one 2 3 4 something like that this will generate right so if you see there are some these are all the arguments are there you can use as for your requirement now I'm using only one like example so here I will Define one so this is schema this is description about this field what it is field so like that we can create description for the second field also so this is name of the user this is first user first name user first name so this one also can give an example like some name for example AJ and another one is you can use last name user last name so it is just like description about each field and next email this is for email of the user this is the example data what kind of data it will hold in this field okay so this is with this our class is ready now we'll create one reposit repository for this this is user repository this is interface this interface extends jpa JP card repository pleas extend jpa repository so here we need to pass user user is our entity then inter is integer is the data type and we can mark this one as annotation repository now repository is ready now we'll create service user service so in the service level we can provide few methods for card operation first one create user after that get user get all user delete user delete all users we can perform public user create user it will accept user as a argument so here we need to specify import statement entity now we need to create object for user repository to call Save user repository user repository we can use annotation Auto why to create the object now here you can use use user repository do save here you can pass user as a argument and here we will use return so with this our create user is ready now we'll create one more method for case we need to use optional user is available it will return the user else it will return null next one more thing we can use list of users it will return return list of users get all users here we'll call user repository do find all you can call find out it will return return all the users from the user table okay so now we created three methods now we can create one more delete we delete will not return anything you can use delete all users user repository dot delete all so I'm here I'm created delete all users you can create one more delete user by ID also now I created 1 2 3 4 now create the apis in the controller class to integrate the Swagger for this applications create the new class user controller so our API specification logic need to mention inside the controllers so first the class level we need to use request controller annotation and request ma to make this one as rest API API SL users so now first API we'll use public user save or we can use like create user create user so we need to pass user as a argument we can add import statement now it is done so we need to call the method inside the service class so we can need to create user service object here for that one we need to use annotation Auto okay now so this is the method so so first we need to specify some annotations and and at top of this method first one is annotation operations if you see annotation operation this is from Swagger operation what kind of operation it will do so it will do the create user multiple fields are available based on that you can use but as for this example I'll use summary so what is the use of this operation it will create user will create new user inside database okay so you need to go with one by one by one what is the use of all these fills inside inside the operation there is a multiple things are available okay so next inside the apart from operation there is one more operation we need to use whenever you hit this API API will give the response either success or failure if this failure then what is what we need to show if it is Success what we need to show right so that one also we need to specify annotation AP response if you you see there is a annotation API response and responses both are there so annotation AP response we use inside you can see the Response Code if if the response code is 2011 then user is created successfully we can Define the description for that one also so this is the response code in the description you can mention user created successfully so all this description you can able to see in the Swagger UI we'll show you now this is create user right so we need to specify post mapping so now we need to call this user service user service dot create user and we need to pause the user argument and it will return this user at the time of returning also we need to use response entity but I'm not mentioning the response entity direct I'm mentioning user object okay so here we are using annotation operation annotation AP response two annotations we are using As for the Swagger integration right so there is one more AP we will create now public user get what is the AP required as per this one so get user by ID get user by ID here we need to pass anotation path variable so here we miss to specify the accepting the request from the client so here we need to specify annotation request body so here single here we are specifying only ID so in this case we need to go with either path variable or request form so I'm going with path variable now integer ID user ID now we need to call user service. find method what are the method available in the user service get user by ID we need to pass ID then it will return user so again here also we can use two anotations like operation and API response you can copy this one and here is a get mapping because it is retrieving operation now we can specify the two operations what is the use of this operation we need to specify so we here we are accessing the user based on the ID so it is like get a user by ID so we need to specify description also return return a single user as for this end point it will return return a single user based on the ID and again we need to specify the response codes so if it is Success then it will return 200 as a response code then this description you can specify user phone if it is fail then so we are here we are specifying only one right so we are a response but if you want to define the multiple responses then we need to use API conations if you see API two are there if you want to Define only single one you can go with API response if you want to Define multiple then you can go with API responses inside the API responses we need to Define multiple responses how if you see there is a value the value it is expecting the array okay so now we can copy this one and past inside the API responses this is one API responses if the response status code is 200 if response status code is 404 there is a issue user is not available then it will return 4 if you getting 404 then it is a user not for so API responses and API response that is the difference between these two okay so with this we added AP specification for this particular method now we'll add one more method public list of users will access from the database okay list user get all users so if get all users and will call user service dot get all users it will return return list of users available from the database import statement now for this one Al we need to mention when is for anation Operation what is the use of this API we need to specify so from this we'll get all the users get all users description return all the users return list of users so this is the operation now we need to do specify the response API response so here we'll go with only single one you the response code is 200 then directly we'll mention description users found so here not only the operation API response so inste we are using Response Code and description apart from that there is a multiple properties are available you can see content extensions headers references so you can use all these things based on your requirement now we created get all users we need to specify the end point for this one we are using annotation get mapping so here using an path variable right so here we need to specify that ID user ID so it will come from the client and we need to set to this so now get all users are done get user by ID is done create user is done now we can create one more API for delete public White delete all users so at the last we'll delete all the users user service. delete all users so it will won return anything so here also you can specify the operation annotation operation so here this one we are using for delete operation delete all users so this is Operation and we can specify annotation API response if the API response is 200 it is user users are deleted successfully if AP response is 44 there is a issue in the servers okay so for that one again we are using API responses then we need to Define two responses for this one this is the then annotation API response so in the API response if the response code is 200 so this is successfully user deleted user deleted successfully so the API response is 40 4 AP response code is 404 then we need to Define user not phone okay so based on the response codes you can Define the API responses also now we have created four end points for this one these APS from the Swagger so we are dealing with H2 database right so we need to add the properties into application. properties so here I'm added S2 database properties so I'm using server port number 9898 okay now we'll start this application now application is started it is up running so here we are using user entity right so user is a re reverse keyword in the database so at the time of creating the table it will give the issue so we need to specify table name as a user like this as you can use any other different kind of entity also okay now we'll go to the browser and we'll see we using H2 database so 9696 so our H2 database is ready you can use password okay it is open this is my user table if you performed operation there is no entry now we'll check with our Swagger so Swagger HTML see now our shager UI is open if you see one get one post another one is get so if you see in our API site the user controllers so one is for create user one is for get user by ID another API for get all users another one delete all users so first one is get get all users create users get get user by ID then delete it is not showing anything is wrong with the delete all users operations API responses we missed to specify this one HTP request method it is a delete mapping okay now we'll quickly restart this so if you want to perform this uh restart server restart automatically then we use the devops tool plugin in the p. XML file in this case we no need to start and stop this server so now we can go we can refresh here now we can see delete also so if you quickly see the what are the specification and the description we mentioned here first come to the controller so in the user controller first API is the create user here we are specifying it is a create a new user when it is uh Response Code 200 it will the user created successfully you can click the create new user so here if you see it is expecting uh arguments like ID first name last name email ID example schema this is so this schema we are mentioning in the entity right so if you see this is the Response Code if it is 200 user created successfully okay only one we are mentioned as the response code now we can try to hit this API so here what are the arguments ID will will be automatically here automatically it will generate now we can try first name last name email ID so this example coming from The Entity we are mentioning in The annotation schema so example one JJ Tech this weld is coming from there okay so now if you try to execute this is one it is created AJ take this one you can go to the database and you can check this one yeah user is there now we if you want to and insert one more entry you can click I can text a tech test a example.com and I'll execute it now we can go to this and you can run this query now second entry also inserted like now we can go to the one more one one more like G all users so what it will expect in the G all users get all users there is no parameters it is required if you go with our API specification also so here get get all users so nothing is there so when I execute this one it will give the list of users now we have two users so it will giveing the two users then one more API we need to check get user by ID so here we need to pass user ID so if you want you can click on the TR out and here we need to specify if you user ID is one and execute it will give user ID one related details so if you give two it will give two okay now delete one is spending you can go with the delete and you can perform this TR because here delete when you click on the delete it will delete all the users from the database Okay click this execute now if you see Server response code is 200 okay so user deleted successfully okay so if you go with the database and you can try this one see there is no records in the database so like this you can test from the swab I hope this video is helpful for you please like share and subscribe my channel
Info
Channel: Java Codeex
Views: 1,341
Rating: undefined out of 5
Keywords: spring boot, swagger, how to add swagger in spring boot, spring boot 3, swagger in spring boot, swagger ui, spring boot swagger, how to add swagger to spring boot, spring, spring boot tutorial, spring boot swagger ui, swagger spring boot, spring swagger, swagger in spring boot api, spring security, setup swagger 2 in spring boot, spring boot tutorial for beginners, spring boot swagger 3 example, spring boot restful api documentation with swagger 2, spring swagger 3
Id: zINJ21OFJEQ
Channel Id: undefined
Length: 27min 45sec (1665 seconds)
Published: Wed Jun 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.