Skip to content
Mathias edited this page Mar 31, 2011 · 43 revisions

spray

spray is a lightweight Scala framework for building RESTful web services on top of Akka actors and Akka Mist. It sports the following main features:

  • Completely asynchronous, non-blocking, actor-based request processing for efficiently handling very high numbers of concurrent connections
  • Powerful, flexible and extensible internal Scala DSL for declaratively defining your web service behavior
  • Immutable model of the HTTP protocol, decoupled from the underlying servlet container
  • Full testability of your REST services, without the need to fire up containers or actors

Minimal Example

import cc.spray._

trait HelloServiceBuilder extends ServiceBuilder {
  val helloService = {
    path("hello") {
      get { _.complete(<h1>Say hello to Spray</h1>) }
    }
  }
}

Latest Blog Posts

Support, Feedback, General Discussion

Please use the http://groups.google.com/group/spray-user/ mailing list!

Longer Example

val service = {
  path("orders") {
    get {
      _.complete(getOrdersFromDB) // marshal with in-scope marshaller
    } ~
    post {
      contentAs[Order] { order => ctx => // unmarshal with in-scope unmarshaller           
        // ... write order to DB
        ctx.complete("Order received")
      }
    }
  } ~
  pathPrefix("order" / "\\d+".r) { orderId =>
    path("") {
      (put | parameter('method ! "put") { // method tunneling via query param
        contentAs[Order] { order => ctx => // unmarshal with in-scope unmarshaller           
          // ... write order to DB
          ctx.complete("Order updated")
        }
      } ~
      get {
        _.complete(getOrderFromDB(orderId)) // marshal with in-scope marshaller
      } ~                
    } ~
    path("items") {
      parameters('size, 'color ?, 'dangerous ? "no") { (size, color, dangerous) =>
        // ... route using required and optional query parameters  
      }
    }
  } ~
  path("documentation") {
    // serve up static content from a JAR resource
    getFromResourceDirectory("docs")
  }    
}

License

spray is released under the Apache License 2.0

Clone this wiki locally