Scala Mongo example with akka-http and akka-stream

Introduction

In this tutorial we shall create and CRUD web application in Scala. We will use MongoDB Scala Driver to communicate to the mongo database. It is officially supported Scala driver for MongoDB. Along with that we shall create a Rest API using akka-http module and akka-streams.

Prerequisites

  • Scala version: 2.13.1
  • Database: Mongo:3.4.23-xenial

Other dependencies

About the service/project

Application should be able to do CRUD operations on the in mongodb document. This application should also provide Rest apis to access and modify the data. So it is an employee management. We have employee details in a document called employee. We will create reset services to create, update and search the employee data.

Document Class

Employee document contains employee name and date of birth.

Mongo Configuration

Codec Registry

Mongo documents required the codec information for marshal and un-marshal the data. This codecs are already written and available here. This repository have codec implementations for most of the available types. So first create the the java codec as below -

In the above code Employee is our mongo document class with the following details.

Document Class

Employee document contains employee name and date of birth.

Database Details

MongoClientSettings is the companion object to configure the mongodb settings.

In the above snippet; A Mongo client settings object is created that contains the configuration details for the connecting mongo database.

Collection Details

Collection details can be obtained from the database representation we get the previous step

This above step required for every collection to get the data.

JSON Support

We need formatter to format the date type to and from JSON format. We used Spray JSON here for this. Spray JSON providing automatic to and from JSON marshalling/un-marshalling using an in-scope spray-json protocol.

Repository

Repository contains the create, update, delete and search commands for the employee document.

Actors to get the user data Employee actor received messages to perform different operation on the employee document. Actor shall call the actual service and then send the responds to the sender.

Routing Configuration

Path Details
GET /api/employee/create To create employee data
POST /api/employee/search To search employee data
PUT /api/employee/update To update employee data
DELETE /api/employee/delete To delete user datail

The routing code should look like this-

Web Server

This class is used to create http server and bind the endpoints with the http server. You can get more information in my previous blog.

Run Application

Start the mongodb docker container

docker-compose up -d

Compile the code using below command

sbt compile

Run the application

sbt run 

Now go to terminal, and run below httpie commands

Create Employee Data

create employee

Search Employee Data search employee

Update Employee Data

Check current data in mongodb

data in database

Update the data by id

update data by id

Search data after update

search data in database

Delete Employee data

Search employee data

search data in database

Delete employee data using id

delete data in database

Search data after delete

search data in database

Github links

Full code is available here to to explore and fork. Feel free to do whatever you want. ;)

Conclusion

We have seen here how to use akka http and streams to create rest services in scala. You can get more information here -