Type mismatch, expected: ToResponseMarshallable, actual: WebServer.Item

2,765 views
Skip to first unread message

Ajinkya Shukla

unread,
Jul 26, 2016, 3:14:31 PM7/26/16
to Akka User List
The following example at http://doc.akka.io/docs/akka/2.4/scala/http/common/json-support.html gives an error at line no. 43 and 44.

Error:(43, 41) type mismatch;
 found   : WebServer.Item
 required: akka.http.scaladsl.marshalling.ToResponseMarshallable
            case Some(item) => complete(item)

  1.  import akka.actor.ActorSystem
  2.    import akka.http.scaladsl.Http
  3.    import akka.stream.ActorMaterializer
  4.    import akka.Done
  5.    import akka.http.scaladsl.server.Route
  6.    import akka.http.scaladsl.server.Directives._
  7.    import akka.http.scaladsl.model.StatusCodes
  8.    import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
  9.    import spray.json.DefaultJsonProtocol._
  10.     import scala.io.StdIn
  11.     import scala.concurrent.Future
  12.     object WebServer {
  13.       // domain model
  14.      final case class Item(name: String, id: Long)
  15.      final case class Order(items: List[Item])
  16.       // formats for unmarshalling and marshalling
  17.      implicit val itemFormat = jsonFormat2(Item)
  18.      implicit val orderFormat = jsonFormat1(Order)
  19.       // (fake) async database query api
  20.      def fetchItem(itemId: Long): Future[Option[Item]] = ???
  21.      def saveOrder(order: Order): Future[Done] = ???
  22.       def main(args: Array[String]) {
  23.         // needed to run the route
  24.        implicit val system = ActorSystem()
  25.        implicit val materializer = ActorMaterializer()
  26.        // needed for the future map/flatmap in the end
  27.        implicit val executionContext = system.dispatcher
  28.         val route: Route =
  29.          get {
  30.            pathPrefix("item" / LongNumber) { id =>
  31.              // there might be no item for a given id
  32.              val maybeItem: Future[Option[Item]] = fetchItem(id)
  33.               onSuccess(maybeItem) {
  34.                case Some(item) => complete(item)
  35.           case None       => complete(StatusCodes.NotFound)
  36.              }
  37.            }
  38.          } ~
  39.            post {
  40.              path("create-order") {
  41.                entity(as[Order]) { order =>
  42.                  val saved: Future[Done] = saveOrder(order)
  43.                  onComplete(saved) { done =>
  44.                    complete("order created")
  45.                  }
  46.                }
  47.              }
  48.            }
  49.         val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
  50.        println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
  51.        StdIn.readLine() // let it run until user presses return
  52.        bindingFuture
  53.          .flatMap(_.unbind()) // trigger unbinding from the port
  54.          .onComplete(_ system.terminate()) // and shutdown when done
  55.       }
  56.    }
  57.  }

How can I pass argument of type ToResponseMarshallable to complete method ?

Akka Team

unread,
Jul 27, 2016, 10:45:07 AM7/27/16
to Akka User List
Hi Ajinkya,

The sample works copied verbatim from your mail. Additionally the example sources you copied it from is part of our Akka build and does indeed also compile.

Something must be wrong with your local environment, have you added the akka-http-spray-experimental module as a dependency?

--
Johan

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages