hseeberger / akka-http-json

Integrate some of the best JSON libs in Scala with Akka HTTP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

akka-http-jackson unable to unmarshall JSON array?

george-ball opened this issue · comments

I have a simple program (inspired by a sample from another blog) that receives data to a POST request at JSON, and works fine when I use the json4s variant. However when I run the same requests into a version of the app that uses the Hakka-http-json I see an unmarshalling exception when unmarshalling a JSON array.
I'd expect equivalent behaviour in both examples, so I suspect something is missing in either my code, documentation, or elsewhere?

Json4s:
$ curl -X POST -H 'Content-Type: application/json' -d '[{"name": "Julie-ann", "age": 21}, {"name": "George", "age": 22}]' http://localhost:8080/customers
"Added 2 customers\n
$ curl http://localhost:8080/customers
[{"name":"Julie-ann","age":21},{"name":"George","age":22}]

Jackson:
$ curl -X POST -H 'Content-Type: application/json' -d '[{"name": "Julie-ann", "age": 21}, {"name": "George", "age": 22}]' http://localhost:8080/customers
There was an internal server error

Server:
[info] Running com.example.JacksonHttpApp
Press RETURN to stop...
[INFO] [08/09/2017 15:54:40.459] [JacksonHttpApp-akka.actor.default-dispatcher-3] [akka.actor.ActorSystemImpl(JacksonHttpApp)] Server online at http://localhost:8080/
[ERROR] [08/09/2017 15:54:44.851] [JacksonHttpApp-akka.actor.default-dispatcher-6] [akka.actor.ActorSystemImpl(JacksonHttpApp)] Error during processing of request: 'scala.collection.immutable.Map$Map2 cannot be cast to com.example.JacksonHttpApp$Customer'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
java.lang.ClassCastException: scala.collection.immutable.Map$Map2 cannot be cast to com.example.JacksonHttpApp$Customer
at scala.collection.immutable.List.foreach(List.scala:389)
at com.example.JacksonHttpApp$.$anonfun$routes$4(AkkaJacksonParsing.scala:27)
at akka.http.scaladsl.server.directives.RouteDirectives.$anonfun$complete$1(RouteDirectives.scala:47)
...

Progs attached here...
progs.zip

Thanks for reporting. Could you please set up a GitHub repo with the two variants?