OpenAPI Generator Demo for Spring Boot | Generate Web & WebFlux Server Code | Swagger UI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends my name is Sarang and in this video we will look how we can use the open API generator to generate the server boilerplate code in a spring boot okay so we will create or we will generate two types of code one is reactive and another one is non-reactive so the reactive code would be generated uh using the open API generated for that we will need the spring web dependency and when we are generating the reactive code using open API generator we will add the dependency of a spring web flux okay so the first what you do is go to this spring initializer and add these two dependencies and download the project and open into your IDE okay so I'm using IntelliJ here so uh before we use the open API generator we need to write or design the API in an open API standard so for that if you're not aware about the open API specification I would recommend you to go go to this website and learn about the open API specification and how to design the API and how to write the API definitions okay so this website gives a basic structure where it says that we have to write the open API definitions in yaml or Json okay so go to this website and look how we can do the API first approach okay so uh this this uh the process that we're gonna follow is the API first approach so what does that mean is first we will Design the API and write its definition and then we will do the implementation of that apis okay first designing and then the implementation so while doing the implementation we will use the open API generator to generate the boilerplate code and then we will write the business logic okay so all that process is called API first approach so I would recommend you to go to this website also and just look into what is API first approach it has been used widely nowadays for designing or building the micro services and it has been adopted widely so let's get started so let's first design the API for that I will go to the resources folder and we will create a directory with the name Swagger and now I'm gonna create a file with the name API dot yaml so here I will write the API definitions of our of our API it's going to be two apis one is guest one is post another one is get so let's get started with that so I'm gonna write the open API definition version would be 3.0.1 I will just give the info about this API the title would be let's say user and the version of this API would be one okay and I will give the description as uh user resources okay and here I will just write the server details and it would be an array it could take uh more than one server details so here I will write localhost 8080 and this would be uh the local server so I will just write the description as local server okay so now I will add the parts and the first part would be a slash user so it would be when we are calling we have to use this uh the URL and then at the end we have to write slash user and this would be a post API okay and the I will add the summary as save user okay it's crucial to add the description and summary because we will also render the Swagger UI and the Swagger will take this particular file and create the UI for us so make sure to add all the description in the summary okay and we will write now the operation ID this will be used by the open API generator so I will explain you why it's needed later and then we will write the request body of how the request will look like for this particular API so this would be of the content would be of application Json and the schema of this particular request will be referred from the component sections okay so we will create this component section here so first let me write the model will be of type user dto okay so let me go to this components schemas and here I will write user dto okay so again I will write the the type would be object and the description is always necessary because many developers is going to use this file so it's better to write the descriptions for your apis and now I will write the properties of this particular object it will have ID and the type would be string and the format would be uuid okay and I will write email the type again would be string and then I will write first name that type would be string and I will add the validations here the minimum length of the first name should be 3 and the max length should be 10 okay and I will write about the last name and this would be of type string so I've also added the validation here okay so this the request body will refer to this particular model as we have written over here in the ref okay so now let's add the response so uh what the API response will look like for this particular API so this would go under the responses section okay so this would give a 200 uh 200 response and that the description of it it's okay and the and the the response will look like the same as the request okay okay in the request I'm gonna send the user detail in the response again I will send the user dto so you can send whatever you want okay for this demo I'm using this both in the request and response okay so our API design it looks fine so uh we will use this file to generate the server code that would be our controllers and we'll also uh that will also create the user dto model okay so for that we have to add the open API Maven plugin here so that takes the input as the api.yaml file the file that we have just created and we had that API definitions in there and this is the name of the plugin and we realize the generator name as spring because open API generator has so many generator name since we are developing the spring boot app so we need to have this as a generator name and the packages is where the boilerplate code will be generated all this packages name that we have given and we'll use the delicate pattern over here so let's look into that so first what we're going to do is we're gonna generate the code okay so let me remove all these things first so for that we'll use the install command and the build got succeed now if you look into the target folder go to the generator resources and here open this particular dto so under the dto we got user dto okay so I think this is a bit old let me clean and then install okay so now uh you would see in the dto we have only dto because uh inside uh this particular definition file we have one model with the name user D2 and that's been Auto generated by the open API generated so all the properties and all the boilerplate has been created by the open API and it has ordered also added the valid annotation that would be used while for validation for validating specific Fields so for an example in the first name I've added the minimum and the max length that would come over here so everything is being generated by this generator okay so here uh this particular code is generated as a non-reactive code so we will look into the controller as well so this is also generated and we will go into this user API delegate since we have used the delegate pattern so it gives the response entity and the of type user dto since we are returning the user dto in the response so it will give the same in this API and the save user we have written over here as operation ID if you can recall so the open API generator will use this operation operation ID to uh to write this particular method so operation ID is your method name for that particular API and it says that it will take user dto to the request and user dtl as a response that whatever you have written in the API yaml it will generate okay so uh now we have generated the code so let's do one thing let's start the application okay but first let me show you the dependencies so we have generated the non-deactive code for that we need the starter web dependency and this particular open API spring dot UI is for for entering the Swagger UI and this two dependencies required by the open API generator make sure to add this to okay and this spring a boot starter validation is required for uh for adding the validation so since you can see the generated code has validations uh in place it's because the generator had found this particular dependency in the class path and added all the validations for us okay make sure to add this particular dependency so let's do one thing let's start the application okay and the application has been started let's see whether we get the Swagger uirrer or not we'll go to the localhost 8080 Swagger UI Dot HTML okay we got the Swagger UI and in there we have an API called save user of type post let's try this out so it it shows that this would be the request body okay so let me do one thing uh let me add this as one of the field is required okay email is the field that is required all the time so what I'm gonna do is I'm gonna restart the application okay and now I'll again refresh it okay now you can see in the user dto model uh uh all the fields that you can see over here has been put in over here and here what I'm gonna do is I'm gonna try calling the API I don't require the ID first because we are generating the user so ID would be created by the server now what I'm going to do is I'm not going to send the email since it's required but I'm not gonna send it so we got a response saying that Phi zero one okay so one thing I have done wrong here is I have written required but then I have to again regenerate the code Forest then only the open API will add the validation on email okay that's a mistake from my side so again I will write start the server and then again re-render the UI and I'll just remove the ID now okay ID and email both now I'll get the error saying that email was required so as you can see I gave a response as a bad request and when we see the logs it shows that uh the email was required and it has been not supplied by the user okay so if you want to send the actual request we have to add the email here email for example string okay and it's give us the again bad request let's see why because I have not added comma here okay now it give five zero one so why it gives a 5 0 1 is because if you just go to the boilerplate code here inside this delicate class they have implemented the save user method and it says that uh it has not been implemented by the user so it gives the default response saying that 501 so what we have to do is we have to implement this particular interface so what we're gonna do is we're gonna create One Directory or package saying that delegate and I will add one file okay seeing that user delegate impl okay which implements the user the API delegates okay and this would be service we don't require to create the B it will be created and now I will just uh go to the generates okay it has not given me let me go to the override methods see we have to implement this save user method okay so what I'm gonna do is let's implement this particular save user method now so for that uh what I'm gonna do is uh now will not get Pi zero one since we have implemented the save user method okay so uh in that we will write the business logic so we'll go to this service we'll create one service with the name called user service okay and I'll write service here and first I'll create a mock DB here as a map I'll write a uid and user dto so I'll just write a DB and I'll write concurrent map oh so it has created no I don't want to implement anything so I'll remove this thing okay I'll just write hashmap okay so here I will write uh public a user user dto save user so I'll get a user dto and what I'm going to do is first I will generate the uuid and save it into this DB variable so uid dot random and first we will use the user dto that we got and set the ID and that would be this uid and then we will write DB dot put uid and this user dto and then return back the user dto okay then we have to write uh or in get this particular user service in the delegate impl okay so let's create a Constructor go here okay and here I'm gonna return the response we'll use the method that was used in the just a second response entity dot okay user service dot save user user dto okay so now if I just start the server and now if I go to the Swagger okay now we should get 200 okay so let's execute this so we got the response back with the ID so in the request we have not supplied the ID but the server has given the ID in the response okay so that's fine uh now let's create one get API okay for that again we have to go to this API dot yaml file and uh okay just let's start writing the design of the API okay so this particular API would uh get the ID from the user for the uh and this will be a get API and we will write a summary get user by let's say by ID okay and we will write the operation ID as get user by ID and we'll write the parameters because we're gonna Supply the ID in the request in the path so we would need that ID to find the user and it will be in the path and it is required so we'll write true and how this ID would be so we have to write under the schema this will be of type string and the format will be again your uid and also give the description of what this uid is so this uuid is nothing but the unique idea of an user okay so this looks fine and we again have to write the response of this API the response would be same as the post that will get the user dto based on the ID that the user has given us okay so we got the API design in place so what we need to do is we need to generate the server code for this particular API so again write the command to generate the boilerplate code now if you can go to the user API delicate now there are two methods one is get user by ID that we have created and there is also a you save user okay since now it's not implemented so it will give 501 so we have to go to this particular uh user service and create this particular method user dto get user by ID you will get ID and type uuid so we will return optional either there can be end user or they there won't be any user right so based on the ID we have to find so uh we will write DB dot get ID if the you if there is no uh user we will throw exception uh sorry so this would be new user not found exception so we will write user not found for ID and uh we'll Supply the ID over here okay now for this we will create a class okay under exception folder so as you can see one one package has been created exception and this would extend the runtime exception and here I have to write super here I have to write this string okay so now let's go here and finish this particular API implementation let's go to here and let's write oh first we have to override the method so I'll go to this impl and then right over right method get user by ID and now uh we will write the response that would be of response entity okay and this would be user service Dot get the user by ID and there you go so here you have implemented one more API get user by ID so let's go and start this over go to your Swagger UI and now let me first write one user into the database okay so I'll write execute so we got one ID and we will use this ID for fetching the user back okay just a second I'll just copy this and we'll go to this particular Swagger for this particular API you can write Ctrl V and then we'll see we got the user back okay we got the user information back so if I if I Supply the wrong uuid then we got the 500 internal server error right and it has given the error in this particular format so we want the error to be in the format that we defined so what we have to do is I'll create one model with the name error so I will again Ask the open API generator to generate one model for me okay so I will here write error and I will write the type as object and then properties that would have code of type integer and the format would be 32-bit okay and now my stage would be of type string okay so now I'm gonna go ahead and generate this model with the help of this command the open API generator will create the model for me with the name error so if you can go here so it has created one class for me with the name error okay so now uh we know that we will get this particular exception 500 internal uh server error when we are not giving the right user uh ID so what we're going to do is we'll create one file or the package with the name config and we will create one file with the name exception translator okay which has annotation called controller advice and this would have one exception Handler which would handle the user not found exception and I will write here public response entity this will be of type error and right hand user not found exception and all right you might be familiar with this uh controller advice that when the application sees this exception it would come under this particular method and translate the exception okay so first I will also write one method that will generate the error in a format that we need so that this would need one message and code so I will quickly write that method so the error Dot set okay we have uh added the wrong error class so let me first go and remove this sorry error would be of the one that we have generated not the default error class okay error dot set code BF core error Dot set Miss message be of message and then we'll return the error okay so what we just have to do is written response entity dot okay uh we will get generate the error with the E dot get message and uh we will also write the code AS not found not found value okay so let's again start the server and so let's get back to the Swagger and let's see whether we get the response in a way that we have defined in the controller device so here we have we have mentioned here when the user not found exception is thrown we'll get into this method and we will return the error in this format which has a code and message so let me go ahead and create one user okay I'll create a user with the name Dev problems gmail.com okay and then I will just write the first name and the last name I'll execute I'll get the user back in the post API call with the ID now I'm gonna give this ID to the get API click on this and now I'll change the last letter to from F to a now I'll get a response in a format that I have defined that I have defined code and message earlier it was giving a spring default error now it's giving the one that we have defined in the controller device so let me correct the uuid it has given the right response with the user details hope so you understood the process what I'm gonna do is I'm gonna generate the uh non uh the the reactive code so that what we have to do is first let me remove all the tabs and let's go to this palm.xml and just add the dependency as web flux okay the open API UI should be also web flux okay so uh as I told you that we're gonna see uh generating uh reactive and non-reactive code uh the boilerplate code so now uh we will create the reactive or generate the reactive code using this open API generator so if you can recall that uh in the API delegate that was response entity there is no mono of response entity so now we will create that particular reactive thing so for that go to this particular plugin and write reactive as true Okay so this all non-reactive code will give a error so let me delete this this files let's delete all the files not required okay so we will just reload this dependencies okay now let's generate the reactive code okay so that I've just added webflogs and this open API reflux and added that reactive as true so now if you can go to the delegate now you can see the mono of response entity okay it was just you know adding that reactive true and adding all the dependencies for the reactive code now we got the mono now you can write your apis in a reactive way so I think uh that's it so uh okay let me also uh render the Swagger for this Swagger UI for this so I'll just reload I'll write http localhost okay now this is a Swagger for your um reactive code it's same only but for that we just added a different dependencies saying that the open API UI would be of types web flux that we that is needed okay thank you and hope you like the video and subscribe to the channel and have a great day
Info
Channel: Dev Problems
Views: 15,877
Rating: undefined out of 5
Keywords:
Id: GJOwJrqIV-8
Channel Id: undefined
Length: 31min 0sec (1860 seconds)
Published: Thu Dec 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.