【问题标题】:How can I apply Unmarshaller to HttpRequest->HttpResponse flow如何将 Unmarshaller 应用于 HttpRequest->HttpResponse 流
【发布时间】:2016-09-09 08:32:54
【问题描述】:

我在我的 java 项目中使用 akka 框架来处理 HTTP 服务,这就是为什么我使用 javaDSL 来处理 akka 流。所以,下面我的故事。 我创建了 http 缓存池(在我的演员中):

val pool = Http.get(getContext().system()).cachedHostConnectionPool(
            "https://somehost.com",
            ActorMaterializer.create(getContext().system())
    )

然后我根据HttpRequest创建了一个源:

val source = Source.single(HttpRequest.GET("/resources/123/"))

然后我创建了 unmarshaller 用于将 HttpReponse 转换为精确的实体:

val unmarshaller = Jackson.unmarshaller(objectMapper, ResourceEntity.class)

下一步是通过池传递源以处理我的actor中的响应:

source.via(pool)
      // TODO: unmarshalling
      .to(Sink.actorRef(getSelf(), new FlowCompletedMessage()))
      .run(ActorMaterializer.create(getContext().system()));

问题是如何将 unmarshaller 应用于此流程?我没有找到任何例子,我觉得自己就像一个试图将方形人物推入圆孔的孩子......

【问题讨论】:

    标签: java akka akka-stream akka-http


    【解决方案1】:

    类似的东西:

    source.via(pool)
          .mapAsync(response => Unmarshal(response).to[T])
          .to(Sink.actorRef(getSelf(), new FlowCompletedMessage()))
          .run(ActorMaterializer.create(getContext().system()));
    

    【讨论】:

    • JavaDSL 中没有 Unmarshal 类。即使,如果我将使用 scaladsl,Unmarshal.to 方法还需要 Marashler 的 ExecutionContexts 和 Meterializer。为什么建议使用 mapAsync?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多