2015-11-06 54 views
2

我噴JSON支持發現隱含的價值看起來像這樣阿卡-HTTP:無法爲參數解組

object MarshallingSupport extends SprayJsonSupport { 
    implicit def json4sFormats: Formats = DefaultFormats 
} 

而在我的路線我想請求映射到DTO

object Main extends App with AppConfig with BaseService with MainActorSystem { 

    val processor = system.actorOf(Props(), "processorActor") 
    val view = system.actorOf(Props(), "processorActor") 

    override protected implicit val executor: ExecutionContext = system.dispatcher 
    override protected val log: LoggingAdapter = Logging(system, getClass) 
    override protected implicit val materializer: ActorMaterializer = ActorMaterializer() 

    Http().bindAndHandle(routes(processor, view), httpInterface, httpPort) 
} 

trait BaseServiceRoute { 
    protected implicit def executor: ExecutionContext 
    protected implicit def materializer: ActorMaterializer 
    protected def log: LoggingAdapter 
} 

trait MainActorSystem { 
    implicit val system = ActorSystem("booking") 
} 

final case class CalculatePriceForRangeDto(unitId: Int, from: Long, to: Long) 

trait PriceServiceRoute extends BaseServiceRoute { 

    implicit val timeout = Timeout(30 seconds) 

    import com.example.crudapi.utils.MarshallingSupport._ 

    def customersRoute(command: ActorRef, query: ActorRef) = pathPrefix("price") { 
    post { 
     path("calculate") { 
     decodeRequest { 
      entity(as[CalculatePriceForRangeDto]) { 
      priceForRange => onComplete((query ? CalculatePriceForRange(

但我越來越

Error:(32, 20) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[com.example.crudapi.http.routes.CalculatePriceForRangeDto] 
     entity(as[CalculatePriceForRangeDto]) { 
      ^

已經看到所有相關的SO問題,但沒有解決我的問題。奇怪的部分是,我試過Typesafe模板akka-dddd-cqrs,它的工作原理,相同的代碼。

我是否缺少隱式上下文? 任何想法可能是什麼?

回答

3

SprayJsonSupport適用於spray-json(不適用於json4s)。因此,你需要定義marshallers如下

trait JsonMarshallers extends DefaultJsonProtocol { 
    implicit val DigestItemWireFormat = jsonFormat6(DigestItemWire.apply) 

    implicit val LocalDateTimeFormat = new JsonFormat[LocalDateTime] { 

    private val iso_date_time = DateTimeFormatter.ISO_DATE_TIME 

    def write(x: LocalDateTime) = JsString(iso_date_time.format(x)) 

    def read(value: JsValue) = value match { 
     case JsString(x) => LocalDateTime.parse(x, iso_date_time) 
     case x => throw new RuntimeException(s"Unexpected type %s on parsing of LocalDateTime type".format(x.getClass.getName)) 
    } 
    } 
} 

,然後在你使用它們或將其與PriceServiceRoute混合,並在文件的開頭導入akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._範圍導入JsonMarshallers._

+0

什麼樣的類是DigestItemWire?沒有在範圍內。 – Reeebuuk

+0

@Reeebuuk這就是您想要序列化/反序列化的案例類的例子,就像您的案例中的「CalculatePriceForRangeDto」一樣。 – expert

+0

試過,但我得到錯誤:類型不匹配; found:(Int,Long,Long)=> com.example.crudapi.http.routes.CalculatePriceForRangeDto required:(?,?,?,?,?,?)=>? 注:內含價值CalculatePriceForRangeDtoFormat這裏不適用,因爲它是應用點之後,並沒有一個明確的結果類型 隱含VAL CalculatePriceForRangeDtoFormat = jsonFormat6(CalculatePriceForRangeDto.apply) – Reeebuuk