问题描述
是否可以记录使用ResteasyClientBuilder创建的客户端发出的传出HTTP请求?
Is there a way to log the outgoing HTTP requests that clients created using the ResteasyClientBuilder makes?
我们的一项服务对另一项服务的请求存在问题,希望查看RESTEasy/JBoss EAP 7正在创建/发送的实际HTTP请求的标头,正文等.
We're having issues with requests made by one of our services to another and want to view the headers, body etc. of the actual HTTP reqeust that RESTEasy/JBoss EAP 7 is creating/sending.
推荐答案
如果您希望在下面记录来自客户端使用的请求
If you wish to log request from client use below
Client client = ClientBuilder.newClient();
String url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + "en" + "&tl=" + "hi"
+ "&dt=t&q=" + URLEncoder.encode(msg);
client.register(new LoggingFilter());
在这里 client.register(new LoggingFilter()); 会为您记录日志,您可以将logger传递给它.
Here client.register(new LoggingFilter()); does logging for you ,you can pass logger to it .
输出
Jan 20, 2017 1:13:48 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread http-bio-127.6.45.129-8080-exec-2
https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&q=ad
Jan 20, 2017 1:13:48 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread http-bio-127.6.45.129-8080-exec-2
1 > GET https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&q=ad
Jan 20, 2017 1:13:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread http-bio-127.6.45.129-8080-exec-2
1 < 200
1 < Accept-Ranges: none
1 < Access-Control-Allow-Origin: *
1 < Alt-Svc: quic=":443"; ma=2592000; v="35,34"
1 < Cache-Control: no-cache, no-store, must-revalidate
1 < Content-Disposition: attachment; filename="f.txt"
1 < Content-Type: application/json; charset=UTF-8
1 < Date: Fri, 20 Jan 2017 07:43:51 GMT
1 < Expires: Fri, 01 Jan 1990 00:00:00 GMT
1 < Pragma: no-cache
1 < Server: HTTP server (unknown)
1 < Transfer-Encoding: chunked
1 < Vary: Accept-Encoding
1 < X-Content-Type-Options: nosniff
1 < X-Frame-Options: SAMEORIGIN
1 < X-XSS-Protection: 1; mode=block
可以通过编辑web.xml来完成如下
Same can be done by editing web.xmlas below
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.webspherenotes.rest.ContactApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
这篇关于记录发出的JBoss EAP 7/RESTEasy请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!