Java 如何使用 Apache Camel 转储通过 HTTP 组件发送的 HTTP 正文和标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19976812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:01:17  来源:igfitidea点击:

How to dump HTTP body and headers sent with HTTP component with Apache Camel

javagroovyapache-camel

提问by Archer

How to dump HTTP body and headers sent with Apache Camel HTTP component using this route:

如何使用此路由转储使用 Apache Camel HTTP 组件发送的 HTTP 正文和标头:

   from('direct:abc').
   setHeader(Exchange.HTTP_URI, constant("${config.gnutch.solr.coreUrl}/select")).
   setHeader(Exchange.HTTP_QUERY, constant("q=${q}&wt=xml")).
   setHeader(Exchange.CONTENT_TYPE, constant('application/xml')).
   setHeader(Exchange.HTTP_METHOD, constant('GET')).
   setBody(constant(null)).
   to("http://null")

This is Camel DSL code in groovy. Is that possible?

这是 groovy 中的 Camel DSL 代码。那可能吗?

采纳答案by Kalpak Gadre

Have you tried something like

你有没有尝试过类似的东西

from("direct:abc")
 .to("http://domain.com/")
 .to("log:DEBUG?showBody=true&showHeaders=true")

Also the HTTP Component Documentationsuggests that you can extract the HttpServletRequestfrom the exchange like,

此外,HTTP 组件文档建议您可以HttpServletRequest从交换中提取,例如,

HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

You can then alternatively do,

然后你也可以这样做,

from("direct:abc").to("http://domain.com").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
        // Log request parameters
    }
});

回答by Rafa? Spryszyński

Try logging headers and content using one of HttpClient loggers.

尝试使用 HttpClient 记录器之一记录标头和内容。

Is is described in Logging Practices(version 3.x in this case).

是在日志记录实践(在这种情况下为 3.x 版)中进行了描述。

I am using loggers with names:

我正在使用带有名称的记录器:

  • httpclient.wire
  • org.apache.commons.httpclient.HttpConnection
  • httpclient.wire
  • org.apache.commons.httpclient.HttpConnection

which gives me output like:

这给了我这样的输出:

o.a.c.httpclient.HttpConnection - Open connection to 0.0.0.0:12454
httpclient.wire.header - >> "POST /some/path HTTP/1.1[\r][\n]"
httpclient.wire.header - >> "breadcrumbId: ID-localhost-55077[\r][\n]"
httpclient.wire.header - >> "path: http://0.0.0.0:65432/some/other/path[\r][\n]"
httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
httpclient.wire.header - >> "Host: 0.0.0.0:12454[\r][\n]"
httpclient.wire.header - >> "Content-Length: 117[\r][\n]"
httpclient.wire.header - >> "[\r][\n]"
httpclient.wire.content - >> "{"a":"afeaafe","b":{"c":"53413"},"d":{"e":"vsegefawawewr"}}"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Type: application/octet-stream[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Length: 7[\r][\n]"
httpclient.wire.header - << "Server: Jetty(9.2.10.v20150310)[\r][\n]"
httpclient.wire.header - << "[\r][\n]"
httpclient.wire.content - << "Success"
o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager.

回答by gnanagurus

This will help , use this in log message :

这将有所帮助,在日志消息中使用它:

${headers}

${headers}

Or

或者

${in.headers}

${in.headers}

This will print any incoming headers .

这将打印任何传入的标题。

Checkout here : http://camel.apache.org/simple.html

在这里结帐:http: //camel.apache.org/simple.html

Example :

例子 :