Swagger UI with Akka Http

Reading Time: 3 minutes

In this blog, we will see how Swagger UI can be generated with Akka HTTP.

Before digging into it further, you can read more about Swagger UI from here and Akka HTTP from here.

Swagger allows developers to effectively interact and try out each and every route that your application exposes and it automatically generates UI from the Swagger specification. The visual documentation makes it easy for developers and clients to interact with the application.

Let’s look at how the setup will look like.

Add Dependency:

Firstly, we need to add swagger-akkahttp dependency in our application’s build.sbt.

"com.github.swagger-akka-http" % "swagger-akka-http_2.11" % "0.14.0"
Add Route Property  :
You need to add routes property in your application that can be added to other routes: Swagger.scala.
The Swagger.scala contains a routes property which can be concatenated along with existing akka-http routes.
This will expose an endpoint at <baseUrl>/<specPath>/<resourcePath> with the specified apiVersionswaggerVersion and resource listing.
Add Akka HTTP route using the Route DSL:
Akka-Http routing provides a way to concatenate various HTTP routes which are composed of one or more level of directives that narrows down to handle each specific request.
Add Swagger Annotations:
Add Swagger UI:

Adding Swagger UI to your site is quite easy, you just need to drop the static site files into the resources directory of your project.

The following trait will expose a swagger route hosting file from the resources/swagger directory.

getFromResourceDirectory("swagger-ui")

Add swagger routes with other routes and bind the routes to a port to start:

Start your application then you can see the swagger ui on the browser :
 

You can find the source code here.

Hope you find this blog helpful, Happy Blogging!

knoldus-advt-sticker

Written by 

Teena is a Senior Software Consultant at Knoldus Software LLP having more than 3 year experience working in Scala, Akka, Play Framework. She is very enthusiastic towards her work and good at working in team. She is very much keen to learn new technologies. She is familiar with programming languages such as Scala, Play Framework, Akka, Javascript, HTML, Java.