16-Generating Code - Swagger API to Java using CodeGen Plugin | Maven for Beginners | Code Journal

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to the video series maven for beginners in this video we will talk about generating java code from swagger files which are essentially api specification files for a web service so let's get started in our last video we talked about soap services the challenge with soap services is that they tend to be heavy because of xml data usage and there is a very tight coupling between the client and the server with time rest web services have evolved and have become the de facto standard for web services because of their extensibility rest apis are documented using the open api specification open api is a specification for machine readable interface files for describing reducing consuming and visualizing restful web services because of a standard specification apis created with this have a common structure and it allows to build tooling around the apis to cut down the amount of boilerplate code let's see an example api let's head over to the home page of for swagger documentation which is the swagger dot io and it is supported by smart smart as it was earlier developed by them and here you will find all the tooling all the resources to build your restful web services using the open api specifications you have link for open web specifications you have open source tools which are available and they have a swagger hub where you can online manage different apis and then scale them and also deploy them on the swagger platform so uh we'll not go into the specification as such not not on the swagger hub but we'll look at the open source tools which they provide and there are three different tools one is the swagger editor which allows you to basically uh create the apis on on an editor and if you see that there is an example which is already available which is the pet store api and this is like a real-time editor where if you change something it will automatically be reflected on the api on the right side so let's say if i change something that stores server here let's say samples of a pet store server using an api and you can see that it automatically reflects on the api and then this is like a real-time uh uh api so like if it is configured correctly let's say if you are developing for some of your own servers then you can actually test them with the from the api editor itself so let's say if i have to test this api which is already replied on the swagger i o uh let's say we can check for let's say uh we want to get a pet and you can see that it takes uh that's the end point which you need to call and then it takes a parameter which is of type integer and it returns a response of you can either check the xml format or you can check the json format and in this case it has a id category it's an example value but you can also check the model itself which defines like how it is laid out so you have id which is of string integer category which is of type category and then category itself is of type id and name so i mean if you could see like this is like a very well documented it can also be read by humans they can understand it very well and also this can be directly used by the tooling so that they can generate the code out of it directly so let's say if you want to let's say create a new pet and we can say you can try it out let's say give it an ids 7654321 we won't give any category to this and we'll say our we won't have any photo urls and neither we will have tags and the status will be say available and then we can say execute and it returns the bed that we just created so they can return it in the response of xml because that's what we selected it but it doesn't mean that we cannot retrieve it in a different format so let's say if we use another endpoint which is getting the pet bytes id we put the id over here and then we say we want it in json format and then we execute it we get the response buddy as what we so i mean it this it allows also to just test out the api endpoints with the values which you want to provide is the author there is an authorization section also where you can provide the client id and other sections so these all has covered by the open api specification uh the other set of tool is uh the ui itself which is basically the so so this editor allows you to create a specification but this ui which is generated is coming out of the swagger ui and that is what you see on the editor itself and then if you want to let's say deploy it on your own server you can actually download it and then deploy it on one of your servers and then you can also see the same set of documentation for your apis and it like kind of gives you a very well presented information like you have the resource groups where you can specify okay a different set of resources are available what endpoints do they have what methods do they accept what are the different parameters so that's it gets very easy for anybody to just read this documentation and then be able to implement them when they are building it and the last section is the code generation which allows you to take this apis and then generate a code out of it and then you can generate a code in a lot of different uh languages and for different sdks and platforms and frameworks and this is the one which we will be using in our demo today to take this sample pet store api and then generate the code out of it using maven plugin so let's go to its documentation and if you can see like on the swagger hub you get a lot more features but then if you want to download it you can you can just the default things like you get community support you can you add automated server stub generation and automated sdk generation and let's actually go to the code gen plug-in so that's the entire tool kit and for maven plug-in there is a different module altogether and that's the one which we need to use because we need we will be configuring in our maven plugin so that it automatically generates the classes which we need for making the calls to the api so let's take this section and let's head over to our project where we want to do it and this is again the same project we don't have any files we just have the default uh coordinates and then property we defined the compiler version of java 11 so let's generate the code for the api using the swagger code gen maven plugin and i think we are on the version 2 let's go to the newer version and there is a separate branch for it i guess yeah so there's a new version is uh hosted at iowa.swagger and then the new version three is at the v3 which is supports the open specification open api specification three let's go to 3.0 branch go to modules and even plug in yeah there you will see like the specific things for maven three so let's copy this i still think this is not the latest plugin let's configure it first so let's add build section in the build section we'll have plugins and then we'll configure our plugin section and yep format it so and let's see what's the latest version of this plugin so if we go to m repository question 3 and latest version is actually 3.0.24 so let's use that and uh if let's look at the documentation for this we'll get rid of the editor we don't need the ui actually the ui but let's look at the documentation first so what all different things are there you can specify the inputs spec file path and this is where we need to specify our api file so let's create a so we'll take this so either you can take it in json or you can take it in as a yaml file both of them work so let's take the json file and if you click here that's the entire json file which is basically there are different things you have different paths and then what methods we suppose so we'll take the raw data here copy it come here in our resources folder we will create a file this effect store dot json and we will paste this format json and that's the entire i mean we won't get into details of this json uh but assuming that you already have an api file which is a swagger document a sagar api documentation you can just paste it anywhere and then we can just directly configure it in our input specifications so in our src main resources we'll get a file called pet store dot json which should be fine the default language is already selected as java and where do we need to generate it we will actually generate it src java that's the folder okay let's see what are some of the other options for this um language as i said which language we want to generate in uh output directory where we want to create template directly we can keep them all of it and then there are different configuration options so like how what what all different dependencies you need to use for these so doesn't give a specification actually you should go to coaching page this should list the configuration section yeah so i said the api clients can be in a lot of different languages and if you're looking specifically for java you can have just c one point x 1.0 default i think is ok http uh retrofit fane you can do it in rest template rest easy vertex google api client library rest assured so all of these are supported and then for kotlin as well then there are different some other uh frameworks in other languages on scholar you can have it at akka http 4s and let's look at some of the other configuration sections so what all do we think so by default uh as i said uh for java configuration it uses the ok http client but i mean a lot of applications are using springboard these days and then they come by default with this template so let's use let we'll use the rest template version of this particular thing so for that we need to specify the language version which so specified the language and we also have to specify the library yeah so you see the configuration options here so you can specify like where the model packages needs to go where the api needs to go and for library as i said like for java you have different options here as i said an okay http and json is the default implementation we will be using the rest template version so so let's configure the rest template first so we'll come here we'll say library is our rest template and then apart from that uh let's segregate out different packages so for the api package we will generate it in a separate folder which is going to be io code journal dot maven dot tracker to java slash api and let's copy this actually we can just keep it actually this should be fine and then for model like all the object classes we will put it in a different package called model and the handler classes which are basically the invoker package where which has classes which basically makes the call to the api itself directly uh we'll put it in a handler package and yeah i think that should be it will save it and then let's do a clean compile and this is going to fail initially because it needs some other libraries which we don't have but we'll first look at the code and then we can figure out what all packages we might be needing so let's refresh it here and go to target generated sources swagger and here we have our code base which is added and let's actually and it won't build path so that we can see the code yeah so you'll you'll see that uh we have the different apis classes and it is generated so it generated a client for the pet uh if we go to here yeah so for the pet api uh this group yeah so there if you can see that there are three different groups one is pet group which contains uh methods to manage the pets and there's another one which is to for the store and then another one is for users so all those three are created as a separate api interfaces and then that's what you can use to make the calls directly and the handler are the one which basically performs the actual operation to make this call so it's also kind of hidden from you so all you have to do is you have to just make call to the pet api and then internally make use the handler to make the calls and the authentication is also generated classes are also generated for you and then these are the models which basically are represent the objects which we have with which we are going to perform the operations so we see that there's a lot of things which are not available to us so let's import them one by one so as you can see it is complaining for jackson so let's import the jackson library jackson data bind 2.12.1 so we'll take that copy it go to our maven let's add a dependency section and this one that should take care of our data bind classes what else do we have um it needs the swagger annotations because that's what it is using it here in the let's define schemas schema so we need this tracker and notations yeah version three so slack annotation is again we have it here what else do we need um we need the spring framework so let's oh so this is part of spring web and that should also take care of http so this web yeah this one is 5.3.4 and we should also need the spring context which contains all the annotations for stereotypes and component beans so yeah that's all we have and yeah this one java x annotation these ones so if so my app project is using java 11 and uh in java 11 all these java x generated annotations are removed but if you are using java 10 or below you will automatically have it as part of your jdk so since i don't have it i will have to import them let's say java x and notations this one and that should be it and if we there's still something which is being complained over here uh okay so now if you see this is using an offset date time from a different library and actually you can use the one which is available from i think it's something to do with date library let's search for it remember there is a configuration for date library let's see if we can get it over here let's check it on the maven plug-in actually we should take a look at the branch 3 version that should have it still not there modules code coaching plugin source folder let's see the example configuration yeah there you have it so there is an option to specify which date you want to use it's in the config options so we'll come to the config options we'll say update library and what are the values it can take just surprised it's not documented over here let's search it here don't worry yeah so this is the default value of java 8 so let's take the java 8 value over here and let's compile it now so now it works so if we go back to our project now all the compilation errors should go away yep and you can see that there is no more errors on the order.java and now it's using the one which is from the java.time which is the newer api for the java update okay so now that all our classes are generated now we can actually dynamically make the use to make the calls so let's create a new package dot code journal not maven dot swagger java and let's create a new class we'll say pet store runner and let's add a main method and set api basically api and from the pet api we can say find bets by status we can say collection singleton list and you can starting import that one and i think there's this status in um yeah that one so this is coming from the pet status you know which specifies which all type of statuses are available and we'll take the available one dot available dot get value and let's start with this one as well and let's assign to a variable available bits and let's just print them all dot for each system that should be it so let's run the application yeah and there you have it like we are getting a list of all the available pets there's quite a quite a huge list actually and then we can actually make a call for a single one as well if you want i mean it should be pretty straightforward so you just take this id lot of fun which you created yeah just take this one of these ids and then what you can do is simply come here api dot get fed by id and then you can say this or maybe we can just get rid of these for now and if we run now you should be able to get that just one pet which we want yep and yeah that's pretty much it so now the code is automatically getting generated and you can automatically just plug take the api and start making calls and then it's using the spring implementation of handler so if you go to api client as i was showing to you earlier it's making all the use of the spring http client and web packages for all its make call making so it uses the rest template which is available from spring and that's how it's making those calls and yeah that's pretty much it and if let's say you want to generate uh just the packages or let's say you don't want to generate you want you have your own implementation of it what you can also do is you can just get ignore uh different like let's say you don't want the api to be generated you can just put it as false you can also say generate supporting files as false which are basically just the handler classes and then it will just generate the model files for you so if let's say if i come here and then now if i do a compilation and we're getting a compilation fill here yeah because of i'll show you why because they are filling so if you see that now we just have the model classes and then you can directly use them directly in your code and the pet store is filling because it doesn't have a pet api so that's why but it's fine it's actually about these should be fine and then we can compile again and we have our classes back again yep that's pretty much it and that's it for the video today thank you so much for watching the video if you like this video please give it a big fat thumbs up and share it with your friends family and colleagues if you are new to the channel do subscribe to the channel and click on the bell icon so that next time when we come up with the new video you will get to know first if you have any questions or comments or feedback do let me know in the comment section below see you all in the next video
Info
Channel: Code Journal
Views: 18,925
Rating: undefined out of 5
Keywords:
Id: C3myXuX-l8Y
Channel Id: undefined
Length: 27min 13sec (1633 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.