【问题标题】:Is it possible to print HTTP Response inside addBodyEndHandler()?是否可以在 addBodyEndHandler() 中打印 HTTP 响应?
【发布时间】:2020-04-17 12:41:56
【问题描述】:

我正在尝试在启动期间添加日志条目,并在请求得到响应后立即添加(使用ctx.addBodyEndHandler)。

下面的函数foo应该是什么aaa

  private void foo(io.vertx.reactivex.ext.web.RoutingContext ctx) {
    long start = System.currentTimeMillis();
    String uuid = ctx.get("RequestIdentifier");
    String clientAddress = ctx.request().remoteAddress().host();
    LOG.info("Request ID: {}. Client IP: {}. Application type: {}. HTTP Method: {}. Body {}. Processing route: {}", uuid, clientAddress, this.getClientDescription(ctx.request()), ctx.request().method(), this.getRequestBody(ctx), ctx.request().uri());

    ctx.addBodyEndHandler(ign -> {
      LOG.info("Request ID: {} completed in {} ms. Response {}. Response code {}.", uuid, System.currentTimeMillis() - start, aaa, ctx.response().getStatusCode());
    });

    // more logic
  }

【问题讨论】:

    标签: http url-routing vert.x


    【解决方案1】:

    您无法从RoutingContext 获取响应正文。有些响应可能非常大,因此 Vert.x 不会存储它们。

    如果您确信它不会损害您的生产系统,您可以在发送之前将正文有效负载存储为上下文数据:

    routingContext.put("responseBody", content);
    routingContext.response.end(content);
    

    然后在bodyEnd处理程序中检索它:

    String aaa = routingContext.get("responseBody");
    

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2020-03-09
      • 2015-05-10
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多