final RestTemplate restTemplate = restTemplateBuilder
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build();
Apache库具有:
public class ManagedHttpClientConnectionFactory
...
private final Log log = LogFactory.getLog(DefaultManagedHttpClientConnection.class);
private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
private final Log wireLog = LogFactory.getLog("org.apache.http.wire");
6条答案
按热度按时间s2j5cfk01#
根据您使用的HTTP连接方法,您可以查看在实际的HTTP连接类中打开日志记录。
例如,如果您正在使用commons HttpClient,则可以设置
commons-httpclient项目有an entire page in the documentation on their logging practices。
q5lcpyga2#
按如下方式配置日志记录:
然后使用curl命令查看输出,例如
默认情况下,restTemplate使用HttpURlConnection(通过SimpleClientHttpRequest),因此您可能需要切换到jakarta httpclient来查看log语句。否则,上面的日志配置将显示响应
nbysray53#
您可以使用spring-rest-template-logger记录
RestTemplate
HTTP流量。在Maven项目中添加依赖项:
然后按如下方式自定义您的
RestTemplate
:确保在
application.properties
中启用了调试日志记录:现在,所有RestTemplate HTTP流量都将在调试级别记录到
org.hobsoft.spring.resttemplatelogger.LoggingCustomizer
。作者:我写了这个图书馆。
3df52oht4#
如果你使用swagger来生成基于RestTemplate的客户端,那么在ApiClient中有一个公共方法setDebugging,你可以设置为true或false,然后我就可以看到发生了什么。希望这对你有帮助。我用最新的swager发电机CLI我认为它的2.9或东西
piztneat5#
对于Apache HttpClient v4工厂:
Apache库具有:
所以在DEBUG级别定义记录器
org.apache.http.headers
和org.apache.http.wire
!lnlaulya6#
你不需要写一行代码,你只需要在application.properties文件中添加以下属性
使用这个,它将在调试模式下记录rest模板调用的请求体、请求头、请求URL和响应体。