Spring Boot Swagger2
Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.
To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
To enable the Swagger 2 we use the annotation @EnableSwagger2 in our SpringBoot Applivation class.
A Docket bean is defined and using its select() method we get an instance of ApiSelectorBuilder. ApiSelectorBuilder we configure the endpoints exposed by Swagger.
After the Docket bean is defined, its select() method returns an instance of ApiSelectorBuilder, which provides a way to control the endpoints exposed by Swagger.
Using the RequestHandlerSelectors and PathSelectors we configure the predicates for selection of RequestHandlers.
These are the only changes required. Now go to http://localhost:8080/swagger-ui.html.
We will see the documentation for the exposed API as follows-
Source Code: Spring Boot Swagger
Reference: https://www.javainuse.com/spring/boot_swagger_annotations
No comments:
Post a Comment