New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Building a RestTemplate using RestTemplateBuilder fails with a ClassNotFoundException when OkHttp 2 is on the classpath #13314
Comments
Thanks for the report. Which version of Spring Boot were you using before you upgraded? |
I used the version 2.0.1 |
Thanks. That's rather surprising as support for OkHttp 2 was dropped from Spring Framework in 5.0.0.RC1. We've missed aligning |
I just hit this same issue myself. I have a project that is using Spring Boot 2.0.1.RELEASE in my case. The project is very simple but pulls in an internal dependency that transitively depends on OkHttp2. As best as I can tell the problem is in: https://github.com/spring-projects/spring-boot/blob/v2.0.1.RELEASE/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java In particular: static {
Map<String, String> candidates = new LinkedHashMap<>();
candidates.put("org.apache.http.client.HttpClient",
"org.springframework.http.client.HttpComponentsClientHttpRequestFactory");
candidates.put("okhttp3.OkHttpClient",
"org.springframework.http.client.OkHttp3ClientHttpRequestFactory");
candidates.put("com.squareup.okhttp.OkHttpClient",
"org.springframework.http.client.OkHttpClientHttpRequestFactory");
REQUEST_FACTORY_CANDIDATES = Collections.unmodifiableMap(candidates);
} Since Spring Framework 5 doesn't support OkHttp2 it would seem to me that private ClientHttpRequestFactory detectRequestFactory() {
for (Map.Entry<String, String> candidate : REQUEST_FACTORY_CANDIDATES
.entrySet()) {
ClassLoader classLoader = getClass().getClassLoader();
if (ClassUtils.isPresent(candidate.getKey(), classLoader)) {
Class<?> factoryClass = ClassUtils.resolveClassName(candidate.getValue(),
classLoader);
return (ClientHttpRequestFactory) BeanUtils
.instantiateClass(factoryClass);
}
}
return new SimpleClientHttpRequestFactory();
} While debugging I can see The resulting stacktrace looks like:
|
Thanks, @chrismathews. Your analysis matches my own which is why I was surprised that @jmirc had only seen the problem when upgrading from 2.0.1 to 2.0.2. |
Thanks for the quick turnaround Andy! |
Hi Andy, sorry for my late reply. I checked why with the Spring Boot 2.0.1.RELEASE I didn't get the issue. I didn't get the issue with the previous version because of the |
I just updated a project with Spring Boot 2.0.2 and I am getting the following exception
The
RestTemplateBuilder
is trying to validate if some HTTP client factories exist and theorg.springframework.http.client.OkHttpClientHttpRequestFactory
doesn't exist anymore in Spring Framework.The text was updated successfully, but these errors were encountered: