In this article, we will talk about generating Java code from Swagger files, which are essentially API specification files for a Web Service. Below video discusses this with the demo. Open API In our previous article, 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 server. With time, REST Web Services have become the de-facto standard for Web Services, because of their extensibility. REST APIs are documented using the Open API specifications. Open API is a specification for machine-readable interface files for describing, producing, consuming and visualizing RESTful Web Services. These were earlier called swagger specifications when they were developed by [SmartBear Software]. 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 on the amount of boilerplate code. Let’s see an example Swagger API. Petstore Swagger API Petstore API is the example Swagger API hosted at swagger.io. The API allows you to manage pets in a store. The API can be viewed in the Swagger UI, which gives a very detailed view of the API, the available operations, the request parameters, headers, authorization, response, all of it. It becomes a handy document for reference as well. Swagger-Codegen Since, Swagger API has well-defined structure, it can be used to generate Java classes for Rest API service and get the API integration going in no time. Swagger Codegen is designed for exact same purpose. Its comes as a standalone binary as well as Maven plugin, to generate the code, and that is what we will use for the Petstore API demo. Project The initial project used in this article is available here. Here are the contents of the Project -> tree .
.
├── pom.xml
└── src
├── main
│ ├── java
│ └── resources
└── test
├── java