Swagger API documentation with Spring Boot

How to create Swagger API documentation for RestAPI in spring boot? In this blog, we will see end-to-end steps to create a RestApi doc in spring boot with the swagger API documentation step by step.

Required Maven dependencies

Below are the required maven dependencies that you need to add to the pom.xml of your project.

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>


<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

Swagger API documenatation Configuration 

Open the spring security configuration class and add the annotation @EnableWebMvc and enable permission for the following URLs.

.antMatchers("/v3/api-docs").permitAll()
.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/webjars/**").permitAll()

Rebuild or rerun the project and open access to the below URL from your browsers. 9091 is the port number that ou need to replace with your port number and “/api” is the context path that also you need to replace with your context path. If you not using any context path just remove it.

http://localhost:9091/api/swagger-ui/index.html

Leave a Comment

Your email address will not be published. Required fields are marked *