我一直在使用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问题不起作用(在某些情况下).
我有私钥和服务器证书的pem证书.我可以使用curl执行它,一切正常.
curl -O -k --cert-type pem --cert mypem.pem url
Run Code Online (Sandbox Code Playgroud)
但是我希望将它与java一起使用,最好从Spring开始使用RestTemplate.
我试图拦截并记录所有请求 - 响应.提出我正在使用的请求RestTemplate.exchange().
当我发出GET请求并得到4**错误时,我可以调用ClientHttpResponse.getBody()并且可以访问响应主体,但for PUT和POSTrequests ClientHttpResponse.getBody()方法会抛出异常.
可能导致这种情况的原因以及我如何获得响应主体POST和PUT请求?
这是我提出请求的地方:
apiResponse = restTemplate.exchange(url, vCloudRequest.getHttpMethod(), entity, responseType);
Run Code Online (Sandbox Code Playgroud)
这是获取异常的拦截器的一部分:
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
String requestString = new String(body);
String responseString = new
// Below line throws exception
String(ByteStreams.toByteArray(response.getBody()), Charset.forName("UTF-8"));
Run Code Online (Sandbox Code Playgroud)
这是堆栈.
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://176.235.57.11/api/admin/org/bd154aaf-2e7c-446d-91be-f0a45138476b/users
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at …Run Code Online (Sandbox Code Playgroud)