In this post, we will show how to add swagger documentation to Spring Boot rest API. We learned how to create a Spring Boot REST API. In Microservices’ world, these days documenting your API is a standard norm. Swagger provides a handy interface and a simple way to build these documentations that any client can test at any moment. They don’t need to have all the services in their environment.
What is Swagger?
Swagger was intended to provide a standard, language-agnostic interface to REST APIs which allow anyone to understand the capabilities of a service without any source code, documentation of source code. You can find more details about Swagger here.
How to add swagger documentation?
In our previous post, we added Spring boot REST API. We will add swagger to the same REST API.
- Add Maven dependencies
To start with, let’s add maven dependencies for swagger jars.
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> <scope>compile</scope> </dependency>
2. Add Swagger bean in configuration
In our main starting Application class, we will add a few configurations for setting up a bean that will handle swagger. In the below code, I show what I have added in Application.java
the file. Basically, we have created a new bean of the type Docket
which takes care of the swagger configuration.
@EnableSwagger2 @SpringBootApplication(scanBasePackages = { "com.betterjavacode.benefits" }) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public Docket benefitsApi() { return new Docket(DocumentationType.SWAGGER_2).groupName("Benefits") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .pathMapping("/"); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Benefits REST Service") .description(" A simple REST service for Benefits software ") .contact(new Contact("Yogesh Mali", "https://betterjavacode.com/", "")) .version("1.0") .build(); } }
3. Show Swagger documentation
Now once we have added the configuration, we can build our project with maven clean install. After a successful build, run the project from eclipse as a Java application. We will access swagger documentation from URL http://localhost:8080/swagger-ui.html
. This will look like below :
Source code for this post is available at Spring-boot-rest-api-with-swagger.
Conclusion
In this post, I showed how you can add Swagger Documentation for your Spring Boot based REST API .
Pingback: Error Handling and Logging in Spring Boot REST API – Part III | Code Junkie
Pingback: Consuming a RESTful Webservice – Part IV | Code Junkie
Pingback: Producing and Consuming SOAP Webservice with Spring Boot – Part V | Code Junkie
Pingback: How To: AngularJS User Interface to CRUD Spring Boot REST API | Code Complete
Pingback: How to use Spring Boot Security in web application – Part VIII | Code Complete