akka / akka-http

The Streaming-first HTTP server/module of Akka
https://doc.akka.io/docs/akka-http
Other
1.34k stars 594 forks source link

Describe how to use Marshaller/Unmarshaller for akka-http client (java) #3374

Open domdorn opened 4 years ago

domdorn commented 4 years ago

The documentation about marshalling / unmarshalling ( https://doc.akka.io/docs/akka-http/current/common/marshalling.html?language=java ) only looks at the server-api, leaving one clueless on how to handle simple tasks using the akka-http client, like

currently, I managed (with a lot of trial and error to deserialize a json back to a POJO like this)

  public CompletionStage<Authorization> retrieve() {
    Unmarshaller<ByteString, AccessTokenDTO> unmarshaller = Jackson
        .byteStringUnmarshaller(AccessTokenDTO.class); 
    return http
        .singleRequest(createRequest()).thenCompose(resp ->
            resp
                .entity()
                .getDataBytes()
                .fold(ByteString.emptyByteString(), (bs1, bs2) -> bs1.concat(bs2))
                .mapAsync(1, bs -> unmarshaller.unmarshal(bs, materializer)) // not very intuitive
                .map(token -> Authorization.oauth2(token.getAccessToken()))
                .runWith(Sink.head(), materializer)
        );
  }

but this is way way way way way harder than it has to be. I'm still clueless on what is the proposed way on how to serialize+send a pojo as json to another server...

jrudolph commented 4 years ago

This is worse than what you describe for #3373 because here the javadsl API to invoke marshallers seems to be missing and converting from the Scala types is somewhat hard from Java. I'll try to cook up an example.