【问题标题】:Apache Camel HTTP display request and responseApache Camel HTTP 显示请求和响应
【发布时间】:2016-09-23 03:03:56
【问题描述】:

我正在使用 Apache Camel 将数据从 CSV 文件加载到 Web 服务。无论如何我可以显示请求和响应。下面是路由配置..

我从数组中拆分和聚合 100 个项目以作为 POST 正文发送。

from(fileLocation)
.unmarshal().csv().bean(new CSVConverter(), "process")

.split(body())
.aggregate(constant(true), new GroupedBodyAggregationStrategy())
.completionSize(100)
.completionTimeout(1000)
.marshal().json(JsonLibrary.Jackson)

.setHeader("Authorization", simple(apiKEY))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.HTTP_URI, simple(apiURL))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("https://serivceurl.com/abc");

请告诉我如何使用上述路由显示请求和响应?

【问题讨论】:

    标签: https apache-camel


    【解决方案1】:

    您可以使用骆驼日志组件来记录标题;属性和主体

    例如:

    .to("log:DEBUG?showBody=true&showHeaders=true")
    .to("https://serivceurl.com/abc");
    .to("log:DEBUG?showBody=true&showHeaders=true")
    

    更多选项请参考:https://camel.apache.org/log.html

    如果您打算使用 CXF 调用 Web 服务,可以使用开箱即用的日志记录功能,如下所示,

    <cxf:bus>
      <cxf:features>
        <cxf:logging/>
      </cxf:features>
    </cxf:bus>
    

    【讨论】:

      【解决方案2】:

      如果您查看 org.apache.camel.component.http.HttpProducer 类,您会发现实现了一些日志记录。

              try {
              if (LOG.isDebugEnabled()) {
                  LOG.debug("Executing http {} method: {}", method.getName(), method.getURI());
              }
              int responseCode = executeMethod(method);
              LOG.debug("Http responseCode: {}", responseCode);
      

      因此,如果您将日志框架(如 logback)配置为正确的 LoggingLevel,您将看到 HTTP 组件的确切作用。 如果您想自己记录,可以尝试使用 log 组件log dsl,就像其他答案中提到的那样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-01
        • 2016-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多