Build Rest Api Using Akka HTTP

Reading Time: 2 minutes

In this blog, I will discuss how we can create rest API using akka HTTP. So lets begin with the introduction of the akka HTTP.

What is Akka HTTP ?

As we know that Akka is a popular actor-based toolkit for building concurrent and distributed applications in the JVM. And these applications mostly use Scala or Java.It has several modules that help to build such applications, and Akka HTTP is one of them. Basically Akka HTTP offers a general toolkit for providing and consuming HTTP-based services. Also the Akka HTTP modules implement a full server- and client-side HTTP stack on top of akka-actor and akka-stream.

How to create Rest Api?

So before creating an Api and the routes of Api, First we need an actor system and the actor materializer and execution context.

implicit val system = ActorSystem("system")
implicit val executor: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer()

An ActorSystem is use to manage the actors. So we use actor system here for creating and managing the actors.

The ExecutionContext is in charge of executing Future. It knows where and how it should execute them, for example in a thread pool.And finally, an ActorMaterializer is use for running streams.

Create the route

To create our route, we are going to use routing dsl of akka http. Below is the example to create the route.

valrouteOne = path("akka") {
 get{
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "welcome to akka-http"))
}

val routeTwo = path("akka"/Segment) { name => 
get { 
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,"my name is"+name)) } 
}

In the above code we define two routes, One is the simple route with path /akka so when we hit this route it will return the content welcome to akka-http. And another is the path matcher DSL which is use to match incoming URL’s and extract values from them. So when we hit this route with path /akka/Abhinav then it will return the content my name is Abhinav.

Now lets bind the above routes with the server address and the port so we can see the content on the server address of the specify port. So to bind the port we use bind and handle method of the akka HTTP.

val bindingRoute = Http().bindAndHandle(routeOne ~ routeTwo, "localhost", 8080)

After binding route with the server we can see the content by hitting the below URL.

http://localhost:8080/akka
http://localhost:8080/akka/Abhinav

Testing the Route

To test the above routes akka http provides the routing test kit. So the basic structure to test the route is

REQUEST ~> ROUTE ~> check {
  ASSERTIONS
}

Now let’s test the above routes

"The service" should {

    "return a string for GET requests to the root path" in {
      // tests:
      Get("/akka") ~> routeOne ~> check {
        responseAs[String] shouldEqual "welcome to akka-http"
      }
    }

    "return a string response for GET requests to " in {
      // tests:
      Get("/akka/Abhinav") ~> routeTwo ~> check {
        responseAs[String] shouldEqual "my name is Abhinav"
      }
    }
    "leave GET requests to other paths unhandled" in {
      // tests:
      Get("/random") ~> routeOne ~> check {
        handled shouldBe false
      }
    }
}

I hope this blog will gives you the basic knowledge that how you can create rest api using akka http.

References-

https://doc.akka.io/docs/akka-http/current/introduction.html

knoldus