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.
11. import scala.io.StdIn
12.
13. import scala.concurrent.Future
14.
15. object WebServer {
16.
17. // domain model
18. final case class Item(name: String, id: Long)
19. final case class Order(items: List[Item])
20.
21. // formats for unmarshalling and marshalling
22. implicit val itemFormat = jsonFormat2(Item)
23. implicit val orderFormat = jsonFormat1(Order)
24.
25. // (fake) async database query api
26. def fetchItem(itemId: Long): Future[Option[Item]] = ???
27. def saveOrder(order: Order): Future[Done] = ???
28.
29. def main(args: Array[String]) {
30.
31. // needed to run the route
32. implicit val system = ActorSystem()
33. implicit val materializer = ActorMaterializer()
34. // needed for the future map/flatmap in the end
35. implicit val executionContext = system.dispatcher
36.
37. val route: Route =
38. get {
39. pathPrefix("item" / LongNumber) { id =>
40. // there might be no item for a given id
41. val maybeItem: Future[Option[Item]] = fetchItem(id)
42.
43. onSuccess(maybeItem) {
44. case Some(item) => complete(item)
45. case None => complete(StatusCodes.NotFound)
46. }
47. }
48. } ~
49. post {
50. path("create-order") {
51. entity(as[Order]) { order =>
52. val saved: Future[Done] = saveOrder(order)
53. onComplete(saved) { done =>
54. complete("order created")
55. }
56. }
57. }
58. }
59.
60. val bindingFuture = Http().bindAndHandle(route, "localhost",
8080)
61. println(s"Server online at http://localhost:8080/\nPress
RETURN to stop...")
62. StdIn.readLine() // let it run until user presses return
63. bindingFuture
64. .flatMap(_.unbind()) // trigger unbinding from the port
65. .onComplete(_ ⇒ system.terminate()) // and shutdown when
done
66.
67. }
68. }
69. }
How can I pass argument of type ToResponseMarshallable to complete method ?
--
>>>>>>>>>> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.