Best way of creating a JSON RequestEntity for the client http call

I’d like to make a client http call providing JSON RequestEntity with some serialized java object

I assume that doing this serialization explicitly in a blocking way is not the best option, is it?

ObjectMapper om = ...;
HttpRequest.create()
   .withMethod(POST)
   .withUri(...)
   .withEntity(HttpEntities.create(APPLICATION_JSON, om.writeValueAsString(obj)));

So I wonder what is the recommended way of doing this?
Can Materializer be used somehow?

Using jackson for example, to transform an in memory datastructure (objects) to another (string with json) , and other CPU bound operations like it, are in general not something you’d call blocking (even if it does not return until it is complete). Transforming it like that is fine. You could also use the akka-http-jackson marshalling module (https://doc.akka.io/docs/akka-http/current/common/json-support.html) but I’m afraid the documentation both for Java and using the API client side is somewhat lacking.

When blocking calls would become problematic is if that operation was fetching the source data from disk or over network, in those cases you’d rather want an async and possibly streaming solution than to block the thread until all data is read.

You can read more about blocking in the Akka docs here: https://doc.akka.io/docs/akka/current/typed/dispatchers.html#blocking-management

1 Like