2016-12-04 64 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,这个错误不应该发生。