O'Reilly logo

RESTful Web Services with Scala by Jos Dirksen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required