1
votes

I'm learning spray.io, and I'm stuck in a problem.. I'm trying to test the case class extraction of several GET parameters. My code is inspired by the examples from the documentation page :

package com.example

import akka.actor.Actor
import spray.routing._
import spray.http._
import MediaTypes._

class ServiceActor extends Actor
with ServiceHello {
    def actorRefFactory = context
    def receive = runRoute(testRoute)

trait ServiceHello extends HttpService with Controls {

case class Color(keyword: String, sort_order: Int, sort_key: String)

val route =
    path("test") {
        parameters('keyword.as[String], 'sort_order.as[Int], 'sort_key.as[String]).as(Color) { color =>
            handleTestRoute(color) // route working with the Color instance
        }
    }
}

This code should be ok, but when I try to run it, I got the following errors :

Cannot resolve symbol as (on top of "as(Color)" )

Missing parameter type: color (on top of "{ color =>")

I understand these errors, but i do not understand why they come...

I'm running Scala 2.10.3

Am I doing something wrong ?

1
I haven't worked with spray routing, but shouldn't as method be called with type parameter? Something like parameters(...).as[Color] instead of parameters(...).as(Color) (note the difference in braces)? - Vladimir Matveev
I don't think so Vladimir, what I'm trying to do is to send an explicit deserializer to the method "as", not a type parameter. FYI spray.io/documentation/1.1-SNAPSHOT/spray-routing/… - ylos

1 Answers

0
votes

Thats working for me (scala 2.10.4, spray 1.3.1):

import akka.actor.Actor
import spray.routing._
import spray.http._
import MediaTypes._

class ServiceActor extends Actor with ServiceHello {
  def actorRefFactory = context

  def receive = runRoute(testRoute)
}

trait ServiceHello extends HttpService {

  case class Color(keyword: String, sort_order: Int, sort_key: String)

  val testRoute =
    path("test") {
      parameters('keyword.as[String], 'sort_order.as[Int], 'sort_key.as[String]).as(Color) { color =>
        //handleTestRoute(color) // route working with the Color instance
        complete {
          <h1>test route</h1>
        } 
      }
    }
}

Don't know what Controls is, so I just commented it.