Error on external REST endpoint call using RestTemplate

261 views Asked by At

My Code breaks when it reaches to this line.

String resp = restTemplate.postForObject(URL, json, String.class);

On my localhost its working fine but on dev-env server, The error is:

Error occured in java.lang.NoSuchMethodError: com.microsoft.applicationinsights.agent.internal.coresync.impl.ImplementationsCoordinator.httpMethodFinished(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IJ)V

The dev-env is Azure Application Service including Java 8 and Tomcat 9.

2

There are 2 answers

0
BetaDev On BEST ANSWER

I got the problem and solution (For others who will get exact same issue as posted): Problem: restTemplate.postForObject() was failing, there was conflict because I was using setConnectTimeout(10000); for

@Bean
public RestTemplate restTemplate(){
  return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory(){
  HttpComponentsClientHttpRequestFactory .......
  factory.setConnectTime(...);
  return factory
}

The problem was solved when I removed clientHttpRequestFactory() from restTemplate and configured default without the connect timeout. Now it's not conflicting.

@Gaurav I will mark your answer because your response gave me an idea about the dependency conflict issue.

3
Gaurav Bhardwaj On

It seems to be like a library issue: somehow your environment has two incompatible versions of the same library. Just run

mvn dependency:tree

or remove manually the invalid jar.