Search code examples
scalaakkafutureakka-http

Error Type mismatch error in Akka scaladsl in Akka-Http code


Im new to Akka framework and i get the below error message when im trying to run my code.

The method which is called through the api code returns a Future[List[HashMap[String,Object]]]

The api code(akka-http route) which calls this method:

@GET
  @Path("getList/{name}")
  @Authorization("basicAuth")
  @Operation(summary = "Get list", description = "Returns a list",
    parameters = Array(new Parameter(name = "name", in = ParameterIn.PATH, description = "name", required=true)),
    security = Array(new SecurityRequirement(name="basicAuth")),
  )
    def getList: Route = {
      path("getList" / Segment) { name =>
        get {
          complete(getListService(name))
        }
      }
    }

I get the below error message

type mismatch;
 found   : scala.concurrent.Future[List[scala.collection.immutable.HashMap[String,Object]]]
 required: akka.http.scaladsl.marshalling.ToResponseMarshallable
          complete(getList(param))

returning a future should not be giving this error, i'm a bit confused as im not able to pinpoint where the mistake is. Any suggestions would greatly help.Kindly let me know if any more info is required.


Solution

  • It requires an entity that it can serialize to JSON. HashMap[String, Object] isn't something that Akka is able to serialize out of the box. You have to either change the return type to something other than Object or define a custom marshaller. I doubt that you need an Object serializer, you probably want to have some class here instead. Here you have examples of serialization using spray: https://doc.akka.io/docs/akka-http/current/common/json-support.html