DefaultMarshallers missing with scala and spray-ro

2019-08-04 03:00发布

问题:

I'm new to Scala, and trying to write a little REST API.

Here is my route definition :

package com.example

import akka.actor.Actor
import com.example.core.control.CrudController
import spray.routing._


class ServiceActor extends Actor with Service {
  def actorRefFactory = context
  def receive = runRoute(routes)
}

trait Service extends HttpService {

  val crudController = new CrudController()
  val routes = {
      path("ads" / IntNumber) { id =>
      get {
         complete(
                  crudController.getFromElasticSearch(id)
              )
      }
      }
  }
}

and here is my controller

package com.example.core.control
import com.example._
import org.elasticsearch.action.search.SearchResponse
import scala.concurrent._
import ExecutionContext.Implicits.global

class CrudController extends elastic4s
{
    def getFromElasticSearch (id:Integer) : Future[String] = {
    val result: Future[SearchResponse] = get
    result onFailure {
        case t: Throwable => println("An error has occured: " + t)
    }
    result map { response =>
        response.toString
    }
    }
}

When I try to run this code, I got the following exception :

Error:(22, 58) could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[scala.concurrent.Future[String]]
                  crudController.getFromElasticSearch(id)

I understand quite well this error, spray needs an implicit marshaller in order to marshall my Future[String] Object. But I'm a little bit confused because in the documentation we can read

Scala compiler will look for an in-scope implicit Marshaller for your type to do the job of converting your custom object to a representation accepted by the client. spray comes with the following marshallers already defined (as implicit objects in the DefaultMarshallers trait) Source https://github.com/spray/spray/wiki/Marshalling-and-Unmarshalling

The required marshallers in my case belongs to DefaultMarshallers, I should not have to implicit him by myself.. Should I ?

标签: scala spray