-
In this guide, we will demonstrate the process of integrating
SwaggerUIwith yourSpringBoot Application. -
The main objective of establishing this connection is to generate documentation for your project.
By integrating SwaggerUI with your SpringBoot Application, you can easily create comprehensive documentation that describes your API endpoints, request/response models, and other relevant details. Also, we will describe annotations to be used that follow SAIB standards.
Note that we'll use SpringFox to generate SwaggerUI
- Add Maven Dependencies in pom.xml file:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- Create seperate package and name it "swagger-config", and create a class inside that package and name it "SwaggerConfig"
- Inside SwaggerConfig class you should create dockets
- Docket represent the endpoints that you want to show in SwaggerUI.
- Notes:
- documentationType should be SWAGGER_2 that represent the version.
- basePackage represent the package that you want to read the endpoints from.
- pathSelector represent the exact endpoints that you want to represent from this package.
- Add path matcher to application.properties "to enable the visual representation for swagger ui"
In the previous section, we described how to activate swagger ui to your spring boot application
a. @SwaggerDefinition: annotation at Swagger Configuration layer:
@SwaggerDefinitionthat represent MetaData.- MetaData represent the project information such as (Title, Description ...etc).
b. @ApiResponses: At method layer inside controller:
- This annotation represent possible responses generated by endpoints.
Note: You can find the responses you should documented in your api's for each http methods in shared wiki "Sub Specific Standards".
c. @ApiParam: At method layer inside controller:
- This annotation represent variables that we provided to our endpoints such as (Path variable, Query Parameter).
d. @ApiOperation: At method layer inside controller:
- That represent description for each endpoint that we create in our project "what exactly should do"