08: Spring boot with OpenAPI (Swagger) tutorial

This tutorial extends Create a simple REST API with Spring Boot.

Q1. What is Swagger?
A1. The OpenAPI (used to be called Swagger) automates the documentation of REST APIs. Documentation of any REST Service API is a must because

1) Consumers of the services need to know which all services are available, the signatures, and the expected input.

2) There must be a simple way to test if the service is up.

3) The exposed services are bound to change, and simultaneously the documentation must be updated. Doing this manually is a very tedious process, especially when the number of REST services are in 100s to 1000s.

This is where Swagger is very useful as it not only automates the documentation, but also simultaneously updates the reference documentation with every change in the API.

Swagger comes with a UI to visualise the REST APIs. Swagger is a specification, and in this tutorial let’s use Springfox as the implementation.

Step 1: Open the Create a simple REST API with Spring Boot tutorial in your IDE – E.g. “Visual Studio Code“.

Step 2: Open the pom.xml file and add the swagger maven dependencies. Also, comment out out the “spring-boot-starter-hateoas” as there is a current incompatibility issue with hateoas + swagger + spring boot 2.2.0.

Step 3: Add the “SwaggerConfig.java” class in package “com.example.simplerestapi“.


Step 4: The REST API class “HelloController.java” – no change.


Step 5: The main Spring boot class “SimpleRestApiApplication.java”.



in “src/main/resources”

Step 6: Open a terminal window, and build and run the application.

Step 7: Open a browser and type:

URL: http://localhost:8888/swagger-ui.html

Springboot with Swagger

Click on the “hello-controller

URL: http://localhost:8888/swagger-ui.html#/hello-controller

Swagger ui – hello-controller Hello Controller

You can test your APIs via Swagger UI with “Try it out” and “Execute“.

Java Developer Interview Q&As

800+ Java Interview Q&As