简体   繁体   English

使用 RestTemplate 调用外部 REST 端点时出错

[英]Error on external REST endpoint call using RestTemplate

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:在我的本地主机上它工作正常,但在 dev-env 服务器上,错误是:

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 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. dev-env 是 Azure 应用程序服务,包括 Java 8 和 Tomcat 9。

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.或手动删除无效的 jar。

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);我得到了问题和解决方案(对于其他会遇到与发布的问题完全相同的问题的人): 问题: restTemplate.postForObject()失败,因为我使用了setConnectTimeout(10000); ,所以发生了冲突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.当我从 restTemplate 中删除clientHttpRequestFactory()并在没有连接超时的情况下配置默认值时,问题得到了解决。 Now it's not conflicting.现在不冲突了。

@Gaurav I will mark your answer because your response gave me an idea about the dependency conflict issue. @Gaurav 我会标记你的答案,因为你的回复让我对依赖冲突问题有了一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM