Swagger UI Setup with Spring Boot Application


What is Swagger UI?

As per Swagger UI official site –

Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place.

By – https://swagger.io/tools/swagger-ui/

How to do Swagger UI setup with Spring Boot?

You can do Swagger UI setup by following these simple steps in a Spring Boot application.

Step 1

  • Head to Spring Initializer.
  • Add “Spring Web” in the dependency.
  • Fill up the project meta-data
  • Click on “Generate” button

Step 2

  • Import the project in Eclipse or your favorite IDE
  • Open pom.xml and add these dependencies –
<!-- Test with Swagger -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>
  • Create a class with @Configuration and @Swagger2 annotations and add a Bean which returns a new Swagger Docket. Refer to the code below:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class WebfuseSpringPocApplicationConfig {

	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
	}
}

That’s It. done! Run the Spring Boot application main class and hit the below URL in the browser.

http://localhost:8080/swagger-ui.html

Still Facing any issues? Let us know in the comments section. We can try to help you. Also the code has been uploaded to our git hub. Feel free to download and test it out.

Code available on Github – https://github.com/webfuse2017/wf-spring-poc/

Also check this out – https://petstore.swagger.io/ . It’s a Swagger Petstore demo which gives a fair idea on how to implement and expose the REST APIs following best practices. Enjoy!

Update – Check out our new blog if you are working on Swagger 3 version

Spread the word!
0Shares

Leave a comment

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

2 thoughts on “Swagger UI Setup with Spring Boot Application”