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.

case class Swagger(system: ActorSystem) extends SwaggerHttpService {
  val config = ConfigFactory.load()
  val API_URL = config.getString("swagger.api.url")
  val BASE_PATH = config.getString("swagger.api.base.path")
  val PROTOCOL = config.getString("swagger.api.scheme.protocol")

  override def apiClasses: Set[Class[_]] = Set(classOf[Base])

  override val host = API_URL

  override val basePath = BASE_PATH

  override def schemes: List[Scheme] = List(Scheme.forValue(PROTOCOL))

  override def apiDocsPath: String = "api-docs"

  val apiKey = new ApiKeyAuthDefinition("api_key", QUERY)

  override val securitySchemeDefinitions: Map[String, ApiKeyAuthDefinition] = Map("apiKey" -&amp;amp;amp;amp;amp;gt; apiKey)

  override def info: Info =
    new Info(
      "Swagger Akka http demo application....",
      "Swagger API",
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.
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._

case class Example(system: ActorSystem) {
  val routes = path("ping") {
    get {
      complete(HttpResponse(OK, entity = "pong"))
Add Swagger Annotations:
import akka.http.scaladsl.server.Route
import io.swagger.annotations._

@Api(value = "/ping")
@SwaggerDefinition(tags = Array(new Tag(name = "hello", description = "operations useful for debugging")))
trait Base {
  @ApiOperation(value = "ping", tags = Array("ping"), httpMethod = "GET", notes = "This route will return a output pong")
    new ApiResponse(code = 200, message = "OK"),
    new ApiResponse(code = 500, message = "There was an internal server error.")
  def pingSwagger: Option[Route] = None
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.


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

object Boot {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("my-actor-system")
implicit val materializer = ActorMaterializer()

implicit val executionContext = system.dispatcher

val routes = Example(system).routes ~ Swagger(system).routes ~

val bindingFuture = Http().bindAndHandle(routes, "", 8080)

println("App has started....")

StdIn.readLine() // let it run until user presses return
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ =&amp;amp;amp;amp;amp;gt; system.terminate()) // and shutdown when done
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!


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.