2017-03-08 43 views
0

我试图将JSON i/p映射到我的案例类CacheRequest。
请求是POST。
我是新来的斯卡拉和阿卡。
使用json4s处理POST请求中的JSON数据

import org.json4s.{DefaultFormats, Formats} 
implicit val formats: Formats = DefaultFormats 
val route: Route = traceContextAwareRoute { 
      pathPrefix(system.name) { 
       post { 
       path("sample") { 
        entity(as[CacheRequest]) { x => { 
        val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f) 

        onComplete(getSystemStock(cacheRequest)) { 
         (response: Try[Option[CacheResponse]]) => complete(processResponse(response)) 
        } 
        } 

        } 
       } 
       } 
      } 


我的情况下类是这样的。

case class CacheRequest(a: String, 
         b: String, 
         c: Int, 
         d: Int, 
         e: Int, 
         f: Int) 

获得这样的错误

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest] 

not enough arguments for method as: (implicit um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest])akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest] 

I'supposed做到这一点使用json4s。
有关这方面的任何帮助对我来说都很好。

回答

0

您可以使用此lib目录下:https://github.com/hseeberger/akka-http-json

代码可能会是这样的

import org.json4s.{DefaultFormats, Formats, jackson} 
import de.heikoseeberger.akkahttpjson4s.Json4sSupport 

class Route { 
    import Json4sSupport._ 
    implicit val formats: Formats = DefaultFormats 
    implicit val serialization = jackson.Serialization 

    val route: Route = traceContextAwareRoute { 
    pathPrefix(system.name) { 
     post { 
     path("sample") { 
      entity(as[CacheRequest]) { x => { 
      val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f) 

      onComplete(getSystemStock(cacheRequest)) { 
       (response: Try[Option[CacheResponse]]) => complete(processResponse(response)) 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

我补充说,依赖 “de.heikoseeberger” %% “阿卡-HTTP-瑟茜” % “1.12.0” 。但是,当导入 - >导入de.heikoseeberger.akkahttpjson4s.Json4sSupport时,我得到“无法解析akkahttpjson4s”。 –

+0

添加此:“de.heikoseeberger”%%“akka-http-json4s”%“1.12.0”, –

+0

是的,它的工作。谢谢。 –