我一直在使用Spring RestTemplate一段时间,当我试图调试它的请求和响应时,我一直碰壁.我基本上希望看到当我使用curl并打开"详细"选项时看到的相同内容.例如 :
curl -v http://twitter.com/statuses/public_timeline.rss
Run Code Online (Sandbox Code Playgroud)
将显示发送的数据和接收的数据(包括标题,cookie等).
我检查了一些相关的帖子,如: 我如何在Spring RestTemplate中记录响应? 但我没有设法解决这个问题.
实现此目的的一种方法是实际更改RestTemplate源代码并在那里添加一些额外的日志记录语句,但我会发现这种方法确实是最后的手段.应该有一些方法告诉Spring Web Client/RestTemplate以更友好的方式记录所有内容.
我的目标是能够使用以下代码执行此操作:
restTemplate.put("http://someurl", objectToPut, urlPathValues);
Run Code Online (Sandbox Code Playgroud)
然后在日志文件或控制台中获取相同类型的调试信息(我使用curl).我相信这对使用Spring RestTemplate且有问题的任何人都非常有用.使用curl调试RestTemplate问题不起作用(在某些情况下).
我正在使用SonarLint向我显示以下行中的问题.
LOGGER.debug("Comparing objects: " + object1 + " and " + object2);
Run Code Online (Sandbox Code Playgroud)
附注:包含此行的方法可能会经常调用.
这个问题的描述是
"先决条件"和记录参数不应该要求评估(鱿鱼:S2629)
将需要进一步评估的消息参数传递到Guava com.google.common.base.Preconditions检查可能会导致性能下降.这是因为无论是否需要它们,必须在实际调用方法之前解析每个参数.
类似地,将连接的字符串传递给日志记录方法也会导致不必要的性能损失,因为每次调用该方法时都会执行连接,无论日志级别是否足够低以显示消息.
相反,您应该构造代码以将静态或预先计算的值传递到Preconditions条件检查和记录调用.
具体来说,应该使用内置字符串格式而不是字符串连接,如果消息是方法调用的结果,那么应该跳过前提条件,并且应该有条件地抛出相关的异常.
不合规的代码示例
Run Code Online (Sandbox Code Playgroud)logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages LOG.error("Unable to open file " + csvPath, e); // Noncompliant Preconditions.checkState(a > 0, "Arg must be positive, but got " + a); // Noncompliant. String concatenation performed even when a > 0 Preconditions.checkState(condition, formatMessage()); //Noncompliant. …