Book Image

RESTful Web Services with Scala

By : Jos Dirksen
Book Image

RESTful Web Services with Scala

By: Jos Dirksen

Overview of this book

<p>RESTful web services are built to work best on the web. Scala provides a rich set of language constructs and advanced frameworks that you can use to create REST services. However, using Scala and these tools can be a complex task. There are many frameworks available and choosing the wrong framework or approach can cost a lot of time and lead to much frustration. By exploring the most popular Scala REST frameworks, you can make sure you choose the right tool.</p> <p>RESTful Web Services with Scala begins with a brief explanation of the REST architecture and its implementation in Scala, as well as the impact that REST architecture has on Scala applications. You will understand the advantages of building Scala web services and how existing Scala applications can take advantage of REST. This book will teach developers about the different programming paradigms available in the Scala world to create RESTful services by exploring the most popular Scala-oriented REST frameworks. It discusses the various facets of RESTful web services such as building scalable APIs, working with standards like HTTP and MIME, designing the architecture, securing the web service, and more.</p> <p>With this book, you will be able to build RESTful web services with various Scala frameworks such as Finch, Unfiltered, Scalatra, Akka-HTTP, and Play. You will create basic REST services using frameworks and then extend the REST services with custom functionality. By the end of the book, you'll be able to decide which framework is best suited for your requirements. We finish by looking at how we can use a number of advanced features provided by these frameworks, such as security, creating HTTP clients, working with HATEOAS, and more.</p>
Table of Contents (14 chapters)
RESTful Web Services with Scala
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Working with paths and directives


The first example we'll look at is the first simple implementation of our API. We won't be returning real objects or JSON yet, but a couple of strings. The code for this step looks like this:

  val route =
    // handle the /tasks part of the request
    path("tasks") {
      get {
        complete { "Return all the tasks" }
      } ~
      post {
        complete { s"Create a new task" }
      } // any other request is also rejected.
    } ~ { // we handle the "/tasks/id separately"
      path("tasks" / IntNumber) {
        task => {
          entity(as[String]) { body => {
            put { complete { 
        s"Update an existing task with id: $task and body: $body" } }
          } ~
            get { complete { 
        s"Get an existing task with id : $task and body: $body" } }
          } ~ {
            // We can manually add this rejection.
            reject(MethodRejection(HttpMethods.GET),
                   MethodRejection(HttpMethods.PUT...