Spray is an Open-Source toolkit for building RESTful Services on top of Scala & Akka Framework.
Being asynchronous, actor-based, fast, lightweight, modular and testable it’s a great way to connect your Scala applications to the world.
Including library file of spary to scala project:
In your scala project open build.sbt and add below lines
The above lines of code is to include spray library to your build.sbt. Here Akka 2.3.10 and spray 1.3.3 is used. Please change the version if needed.
Create HTTP Actor
Create Http actor to handle all the http request. In spray all the incoming request is handled by one Akka actor and then the work is distributed to multiple actor.
Create a scala class and name it as HttpServiceActor and extends HttpService with Actor, like below
After this you should override two methods
Here runRoute is a function in HttpService class. Should pass the path reference variable. Here sample variable is defined as
Here ends the implementation of HttpServiceActor. Now it’s time to implement the main part, where we will define the interface, port to which spray will listen.
Create a scala object and name it as ScalaSpray.scala and then extends with App
And define the main method
Now run the Scala Project. Then open your browser and enter below url
Now you should see Hello World.
I’ll write the next tutorial in some time.