Прочитайте тело ответа в Android Spring Interceptor

Я использую простой класс Spring Interceptor для регистрации всех запросов/ответов REST для объекта RestTemplate в моем приложении для Android. Пока все работает нормально.

public class LoggerInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
  ClientHttpRequestExecution execution) throws IOException {

Log.d(TAG, "Request body: " + new String(body));
//...more log statements...

ClientHttpResponse response = execution.execute(request, body);

Log.d(TAG, "Response Headers: " + response.getHeaders());

return response;
}

При инициализации звоню:

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
LoggerInterceptor loggerInterceptor = new LoggerInterceptor();
interceptors.add(loggerInterceptor);
restTemplate.setInterceptors(interceptors);

Однако я не могу зарегистрировать response.getBody()в приведенном выше методе, потому что InputStream используется один раз и выдает исключение IllegalStateException, когда он используется снова позже. Есть ли способ обойти это, чтобы я тоже мог регистрировать тело ответа?

8
задан Nachi 20 June 2012 в 12:49
поделиться