Akka Http is a module of Akka developed based on akka-actor and akka-stream. We can use Akka Http on both sever and client-side. However, it shoud be noted that Akka Http is not a web-framework like Angular JS or Spring. It is just a toolkit to develop or to use HTTP-based services. Therefore, we don’t use Akka Http to build Web Application, but rather, we use it to integrate with HTTP (For example: we use ReactJS to design User Interface and use Akka Http to create Restful APIs that read data from Database and send to UI).

Akka Http provides DSL (Domain Specific Language) to describe HTTP Routes and how to use these Routes. Each Route is divided into smaller parts called Directive to handle a certain request. For example: A Route can start with finding the corresponding path of a request (Eg: localhost/getInfo), then narrow down to just handling HTTP GET request or POST request.

Sending Akka Http’s requests and reponses are carried by Marshallers only and it has nothing to do with Route. Therefore the returned result can be in any format as long as Marshallers supports that format. The default format of Marshaller is String or ByteString.  However, users can define their own format with JSON (using spray-json library).

In this tutorial, we will write a program using Akka Http to create a Resful API that processes GET and POST request to read and write data into Cassandra Database (more details about Cassandra Database can be found on the tutorial series of Cassandra at here).

First, we add the following dependencies to the pom.xml file:

As Akka Http uses akka-actor and akka-stream, we need to define actors as follows (see more at akka-actor and akka-stream)

Next, we connect to Cassandra Database to read and write data into emp table of testkeyspace (emp table was created in the tutorial Interacting with Cassandra using its API).

The Emp table has 5 columns, so we define Employee class with corresponding 5 variables and use akka-http-spray-json to marshal and unmarshal data as follows:

In the main function, we define a Route with GET and POST as follows:

– GET: has its path being “/getAll” with the returned result is the information of all employees. Wewrite cassandraReader(query: String) function to read data from the emp table

– POST: has its path being “/insertData” to receive the data sent from users and then save it in the database (emp table)

Finally, we specify the host and port of the server and allow users to press Enter to stop running Akka server as follows:

To check the operation of the REST API created above, we run the program and then a go to http://localhost:8080/getAll using any web browser, we will get the information of all employees stored in the emp table as follows:

We check the POST request by opening a terminal and use the curl command as follows (If the POST request is successfully handled, a message “Data inserted !!!” will be displayed):

Visit the link http://localhost:8080/getAll again to re-check the returned result, we can see that the data sent by the user using POST request has been successfully inserted into the emp table.

So, we have finished writing a program using Akka HTTP to create a Restful API that processes GET and POST request to read and write data into Cassandra Database, The full code of this tutorial is provided below:

March 10, 2019