08: Spring boot with Swagger tutorial

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

Q1. What is Swagger?
A1. 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“.

SwaggerConfig.java

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

HelloController.java

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

SimpleRestApiApplication.java

application.properties

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“.

print

Why wait 5-10 when you can fast-track in 1-2 ?

open all | close all
Top