2016-12-04 68 views
1

我有以下情況下類:找不到隱格式參數

case class Customer(name: String) 

也可以像這樣編碼:

class ServiceJsonProtocol extends DefaultJsonProtocol { 
    implicit val customerProtocol = jsonFormat1(Customer) 
} 

的問題是,這種代碼:

val route = 
    path("customer") { 
    post { 
     entity(as[Customer]) { 
     customer => complete { 
      customers.add(customer) 
      s"got customer with name ${customer.name}" 
     } 
     } 
    } 

我得到這個:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[Customer] 
[error]   entity(as[Customer]) { 

問題是什麼?

+1

如果您使用'spray-json',請在您的問題中提及它。另外,看看http://stackoverflow.com/questions/33574176/akka-http-could-not-find-implicit-value-for-parameter-unmarshalling – YoungSpice

回答

4

您必須擴展SprayJsonSupport。本編譯:

import spray.json._ 
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport 

case class Customer(name: String) 

object ServiceJsonProtocol extends DefaultJsonProtocol { 
    implicit val customerProtocol = jsonFormat1(Customer) 
} 

class RateRoutes extends SprayJsonSupport { 

    import ServiceJsonProtocol._ 

    val route = 
    path("customer") { 
     post { 
     entity(as[Customer]) { 
      customer => complete { 
      customers.add(customer) 
      s"got customer with name ${customer.name}" 
      } 
     } 
     } 
    } 
    } 

我想你使用akka http。使用Spray,這個錯誤不應該發生。