Skip to content

Commit db252cd

Browse files
committed
SPR-6303 - Add more logging to RestTemplate
1 parent f1075c7 commit db252cd

File tree

1 file changed

+30
-2
lines changed
  • org.springframework.web/src/main/java/org/springframework/web/client

1 file changed

+30
-2
lines changed

org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,11 @@ protected <T> T doExecute(URI url,
382382
requestCallback.doWithRequest(request);
383383
}
384384
response = request.execute();
385-
if (getErrorHandler().hasError(response)) {
386-
getErrorHandler().handleError(response);
385+
if (!getErrorHandler().hasError(response)) {
386+
logResponseStatus(method, url, response);
387+
}
388+
else {
389+
handleResponseError(method, url, response);
387390
}
388391
if (responseExtractor != null) {
389392
return responseExtractor.extractData(response);
@@ -419,6 +422,31 @@ private void checkForSupportedMessageConverter(Class type) {
419422
throw new IllegalArgumentException("Could not resolve HttpMessageConverter for [" + type.getName() + "]");
420423
}
421424

425+
private void logResponseStatus(HttpMethod method, URI url, ClientHttpResponse response) {
426+
if (logger.isDebugEnabled()) {
427+
try {
428+
logger.debug(method.name() + " request for \"" + url + "\" resulted in " + response.getStatusCode() +
429+
" (" + response.getStatusText() + ")");
430+
}
431+
catch (IOException e) {
432+
// ignore
433+
}
434+
}
435+
}
436+
437+
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException {
438+
if (logger.isWarnEnabled()) {
439+
try {
440+
logger.warn(method.name() + " request for \"" + url + "\" resulted in " + response.getStatusCode() +
441+
" (" + response.getStatusText() + "); invoking error handler");
442+
}
443+
catch (IOException e) {
444+
// ignore
445+
}
446+
}
447+
getErrorHandler().handleError(response);
448+
}
449+
422450
/** Request callback implementation that prepares the request's accept headers. */
423451
private class AcceptHeaderRequestCallback<T> implements RequestCallback {
424452

0 commit comments

Comments
 (0)