Enhance your RESTful service with Swagger UI

In the previous post we described how to build a very simple RESTful service – basically a Java application built to be run inside a Beanstalk application. We promised you that we’ll come with several tips to make your service configurable and resilient and to help you during the development.
Today we are going to show you how to integrate Swagger UI in your service. Swagger UI is a module from Swagger toolkit that helps you exploring and interacting with the API. We invite you to explore deeper the Swagger framework to understand how it could become useful for your application.
Let’s see what steps we have to complete in order to integrate Swagger UI in our application:
1. Add dependencies.
In your pom.xml, add the following 2 dependencies:

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

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.4.0</version>
</dependency>

2. Annotate main class

@SpringBootApplication
@EnableSwagger2
public class CirrusCloudDemo {

   private static final Logger LOG = LoggerFactory.getLogger(CirrusCloudDemo.class);

   public static void main(String[] args) {

       LOG.info("Starting application... ");
       SpringApplication.run(CirrusCloudDemo.class, args);
   }

   @Bean
   public Docket createDocketForSwaggerIntegration() {

       return new Docket(DocumentationType.SWAGGER_2)
               .select().apis(RequestHandlerSelectors.any())
               .paths(getPaths()).build()
               .useDefaultResponseMessages(false);
   }
  
   public Predicate<String> getPaths() {

       return or(regex("/hello*"));
   }
}

Now, let’s build and restart the application and, after that, open in your favorite browser the http://localhost:8080/swagger-ui.html page. It should look like in the below image:
Swagger UI Page
You can find the codebase for download here.
We hope you see value in adding this extension to your application. Don’t hesitate to add your comments below and to share this blog with anyone interested in our topics!