Getting java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode

Spring's RestTemplate and Spring Boot's TestRestTemplate will on JDK's internal HttpURLConnection implementation by default, which fails to access the body of an HTTP-response with status 401 "Unauthorized".

A magical solution is to include Apache's HTTP Client into the classpath — e.g. with Maven:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.6</version>
  <scope>test</scope>
</dependency>

In this case, Spring (or Spring Boot) will use Apache's HTTP Client which doesn't face the problem.

See also: https://github.com/spring-projects/spring-security-oauth/issues/441#issuecomment-92033542


P.S. I've just fought the same problem :)