0

I'm trying to map JSON i/p to my case class CacheRequest.
request is POST.
I'm new to Scala and Akka.

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))
                    }
                  }

                  }
                }
              }
            }


My case class is like this.

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

Getting an Error Like

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 to do this using json4s.
Any help regarding this is fine for me.

Raj Parekh
  • 192
  • 1
  • 9

1 Answers1

0

You can use this lib: https://github.com/hseeberger/akka-http-json

code may something like this

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))
            }
          }
        }
      }
    }
  }
}
  • I added that dependency "de.heikoseeberger" %% "akka-http-circe" % "1.12.0" . But while importing -> import de.heikoseeberger.akkahttpjson4s.Json4sSupport , I'm getting "cannot resolve akkahttpjson4s". – Raj Parekh Mar 09 '17 at 06:53
  • add this: "de.heikoseeberger" %% "akka-http-json4s" % "1.12.0", – wu zhonglin Mar 09 '17 at 07:42