Now a daysRESTandMicroserviceshave got a lot of momentum. At the same time, actual REST specification does not suggest any standard way to document the REST APIs that we are going to expose [like WSDL for SOAP]. As a result, everybody is documenting their APIs in their own way, resulting a gap in common structure which all can easily follow, understand and use. We need to have a common pattern and tool.
Swagger(backed by companies like Google, IBM, Microsoft) does this same job of filling the gap of common documentation style. In this tutorial, we will learn touse Swagger to generate REST API docsusingswagger 2 annotations.
Swagger (now the “Open API Initiative”) is a specification and framework for describing REST APIs using a common language that everyone can understand. There are other available frameworks that have gained some popularity, such as RAML, Summation etc. but Swagger is most popular at this point of time considering its features and acceptance among the developer community.
It offers both human readable and machine readable format of documentation. It provides both JSON and UI support. JSON can be used as machine readable format andSwagger-UIis for visual display which is easy for humans to understand by just browsing the api documentation.
We will first create some REST APIs which will be used for demonstration of Swagger documentation capability. We will use the Spring boot style of exposing rest API for faster development time.
Create a Spring boot project fromSpring Boot initializerportal withWeb,Rest Repositories,Actuatordependencies. Give other maven GAV coordinates and download the project. This screen will look like:
Spring Boot REST Project Generation
Unzip and import the project into Eclipse as existing maven project. In this step, all necessary dependencies will be downloaded from maven repository. Perform a freshmvn clean installat this step so that all spring-boot related artifacts got downloaded properly.
Openapplication.propertiesand add below property. This will start the application in/swagger2-democontext path.
server.contextPath=/swagger2-demo
Add one REST controllerSwagger2DemoRestControllerwhich will provide basic REST based functionalities onStudententity.
Our REST APIs are ready. Now add swagger 2 support to the project.ff
Add Swagger2 Maven Dependencies
Open pom.xml file of thespring-boot-swagger2project and add below two swagger related dependencies i.e.springfox-swagger2andspringfox-swagger-ui.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
ActuallyswaggerAPI has couple of varieties and maintained in different artifacts. Today we will use thespringfoxbecause this version adapts well with any spring based configurations. We can try other configurations also easily and that should give same functionality – with no/little change in configuration.
Add Swagger2 Configuration
Add the below configuration in the code base. To help you understand the configuration, I have added inline comments.
Do maven build and Start the server. Open the linkhttp://localhost:8080/swagger2-demo/v2/api-docsand it should give the entire documentation inJSONformat. This is not that much easy to read and understand, actually Swagger has provided this to be used in other systems like API management tools now a days popular, which provides the functionality like API gateways, API caching, API documentation etc.
The default generated API docs are good but they lack detailed API level information. Swagger has provided few annotations to add this detailed information to the APIs. e.g.
@Api– We can add this Annotation to the controller to add basic information regarding the controller.
@Api(value = "Swagger2DemoRestController", description = "REST APIs related to Student Entity!!!!")
@RestController
public class Swagger2DemoRestController {
...
}
@ApiOperation and @ApiResponses– We can add these annotations to any rest method in the controller to add basic information related to that method. e.g.
@ApiOperation(value = "Get list of Students in the System ", response = Iterable.class, tags = "getStudents")
댓글