Finatra-swagger: Making the api documentation awesome and easy

Apart from speed, high-performance, fault-tolerance and concurrency, one more most important feature for an api is a clean, precise, interactive and user friendly documentation. Documentation plays a very important role in a rest api. The interactive documentation makes api more easy and understandable. Finatra is fast, testable, Scala services built on Twitter-Server and Finagle, similarly Swagger is a well known api documentation framework. In this blog we are going to discuss how to integrate Swagger UI (Api documentation) with Finatra to achieve awesome and easy documentation with high performance and concurrency.

Getting started:

There are many libraries available for swagger finatra support. For this example we are using swagger-finatra.

Let’s start implementing finatra-swagger sample application:

Step 1: Dependencies:

Here are the dependencies for finatra-swagger sample:

Scala: 2.12.0
Finatra-http: 2.10.0
Finatra-swagger: 2.9.0

Step 2: Build.sbt:

Here is the build.sbt for this sample project:

name := "finatra-swagger-sample"

version := "1.0"
scalaVersion := "2.12.0"
lazy val versions = new {
  val finatra = "2.2.0"
  val swagger = "0.7.2"
resolvers ++= Seq(
  "Twitter Maven" at ""
resolvers += "Sonatype OSS Snapshots" at ""
libraryDependencies += "com.twitter" %% "finatra-http" % "2.10.0" % "compile"
libraryDependencies += "com.jakehschwartz" % "finatra-swagger_2.12" % "2.9.0" % "compile"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.3" % "test"

Step 3: Create a subclass of a SwaggerModule:

First of all, we need to create an object of swagger module to define basic information regarding api.
Here we need to create an object SampleSwaggerModule extending SwaggerModule which is defined under finatra-swagger library.
We can also define the api root information inside Swagger object using the Info object. The basic information defined can be description, version and title of  the api. You can see this information on top of the swagger document after running the application.
The security definition is used to add basic authentication inside swagger documentation if the api requires basic authentication.

object SampleSwaggerModule extends SwaggerModule {
  val swaggerUI = new Swagger()
  def swagger: Swagger = {
    val info = new Info()
      .description("The Knoldus / Knolder management API, this is a sample for swagger document generation")
      .title("Knoldus / Knolder Management API")
      .addSecurityDefinition("sampleBasic", {
        val d = new BasicAuthDefinition()

Step 4: Add DocsController into server:

DocsController is a class defined in finatra-swagger library which works as a glue between finatra server and swagger.
If you have used finatra earlier, you must be familiar with finatra HttpServer and how to configure http routes inside finatra servers.
Here we are adding DocsController with SampleController. SampleController include the actual application routes with swagger related descriptions.

object SampleApp extends HttpServer {

  override def configureHttp(router: HttpRouter) {
  override protected def modules = Seq(SampleSwaggerModule)

Step 5: Configure the endpoints using the SwaggerRouteDSL:

Finally, we have to configure the endpoints using SwaggerRoteDSL. To do that we have to extends SwaggerController which is defined in the finatra-swagger library and inject Swagger as a dependency.
To define a route instead of using get we are using getWithDoc method and passing route information like route summary, tag, parameter type and response type etc.

class SampleController@Inject()(s: Swagger) extends SwaggerController {
  implicit protected val swagger = s

  getWithDoc("/knolder/:id") { id =>
    id.summary("Read the detail information about the knolder")
      .routeParam[String]("id", "the knolder id")
      .responseWith[Knolder](200, "the knolder details")
      .responseWith(404, "the knolder not found")
  } { (request: Request) =>
    val knolderId: Int = request.getParam("id").toInt
    Knolder(Some(knolderId), "girish", "Consultant")

All the options for http calls like get, post, delete and put are available. You can find a full http CRUD application sample on the git repo:


Step 6: Running the application:

After finishing above steps we are ready to run the application and play with swagger document.

A. Clone git repo:

To run the app you can clone the git repo using the following command:

git clone

B. Running application:

You can run the  application using the following command:

sbt run

C. Hitting rest end points on the browser:

Once the application is running you can go to browser and hit the url:


After hitting the end point you will see a sample response on the browser.

D. Analyzing swagger document:

Once the routes are working as expected you can find the swagger document by navigating to url:


Here is a screen shot of swagger doc generated for this sample application:


To explore more, please go through the application here.




About Girish Bharti

Girish is a Software Consultant in Knoldus Software LLP. He is a scala developer and very passionate about his interest towards Scala Eco-system. He has also done many projects in different languages like Java and He is self motivated, dedicated and focused towards his work. He believes in developing quality products. He wants to work on different projects and different domains. He is curious to gain knowledge of different domains and try to provide solutions that can utilize resources and improve performance. His personal interests include reading books, video games, cricket and social networking. He has done Masters in Computer Applications from Lal Bahadur Shastri Institute of Management, New Delhi.
This entry was posted in Scala, Swagger, Web, web application, Web Services. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s