Tuesday, April 15, 2014

Simple Scala REST example using Spray

http://sysgears.com/articles/building-rest-service-with-scala/
https://github.com/oermolaev/simple-scala-rest-example

Enterprise application might be developed using a variety of architectural styles. And REST is one of the most powerful of them. It allows us to build simple, scalable, highly productive APIs with independent components on the basis of widespread standards like HTTP, MIME etc, engaging their true potential.
Let's discuss how to create a lightweight, but full-featured RESTful service from scratch.
Consider building REST [1] service that doesn't contain any complicated functionality, but provide basic CRUD operations and have following HTTP endpoints as an API:
  • POST /customer/ to create Customer.
  • GET /customer// to retrieve specific Customer.
  • PUT /customer// to update specific Customer.
  • DELETE /customer// to delete specific Customer.
  • GET /customer/ to search for Customers with specific parameters.

Technology stack

Scala was chosen as the foundation for the REST service we are going to implement. On the Scala website [2] it is described as a "general purpose programming language designed to express common programming patterns in a concise, elegant and type-safe way. Also, it smoothly integrates features of object-oriented and functional languages."
Despite the fact that Scala is a relatively new language and may have some drawbacks, there are several attractive elements, that made it not just an "another new programming language":
  • Operating on the JVM. It is known fact that Java is de facto the most popular programming language for enterprise. Many libraries are written in Java. A variety of tools are designed for JVM. Environments have been stable for years. It can be rather risky to change the entire programming stack even if this step may provide apparent benefits. But Scala has a great interoperability with existing Java code because it still operates on the JVM, has compatible byte code and allows Scala applications to use most of the JVM libraries. Furthermore, developers are able to apply their existing Java skills after migration. Therefore, Scala could be integrated into enterprise infrastructure quite smoothly.
  • Functional programming. Except that Scala is a pure object-oriented language it provides syntactic sugar for functional programming. Like a pattern matching, anonymous and higher-order functions, carrying, immutable collections, etc.
  • Concise and powerful syntax. Code size is reduced significantly compared to an equivalent application written in Java. This may improve development performance: less key-strokes to make, easier code review and testing. Moreover, many features like function passing and type inference can reduce syntactic overhead.
  • Static typing. Scala is equipped with a rich and balanced type system. It provides compile time constraints that could help to avoid certain erroneous scenarios. On the other hand, a local type inference mechanism allows developers not to annotate the program with redundant type information.
The following software was also used:
  •  SBT (Simple Build Tool) - Build tool for Scala and Java projects. Maven or Gradle with appropriate Scala plug-ins can be used as well, however, SBT became de facto the number one build tool for Scala. It is easy-to-use but quite powerful utility. [3]
  •  Akka - Asynchronous event-driven middleware framework implemented in Scala, for building high performance and reliable distributed applications. Akka decouples business logic from low-level mechanisms such as threads, locks and non-blocking IO. [4]
  •  Spray - Scala framework for building RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, modular, testable. [5]
  •  Slick - Database query and access library for Scala. It provides a toolkit to work with stored data almost as using Scala collections. Features an extensible query compiler which can generate code for different backends. [6]
  •  MySQL - Well-known open-source RDBMS. [7]
  •  Lift-json - Parsing and formatting utilities library for JSON. [8]
  •  Logback - Fast and stable logging utility. Considered as a successor to the log4j project. Natively implements the SLF4J API. [9]

No comments: