Mur*_*mbi 2 java resttemplate spring-boot
在使用基于 Eclipse 的 Spring Suite 时,尝试在 Sprint 启动上运行示例程序时,我看到一条错误消息。我当前使用的 mac 上安装了 JDK11
错误消息: RestTemplate 类型中的方法交换(URI、HttpMethod、HttpEntity、Class)不适用于参数(URI、Http.HttpMethod、HttpEntity、Class)
最初的交换方法是这样的:exchange(url,HttpMethod.GET, null, String.class);
当 url 是字符串时。由于该方法需要 URI 对象,因此我更改了代码。另外,在寻找解决方案时,我还将 null 参数转换为对象。
public String secondWayOfCalling() {
RestTemplate template = builder.build();
List<ServiceInstance> instances= clientOnly.getInstances("client-service-name");
URI uri = instances.get(0).getUri();
ResponseEntity<String> entity = template.exchange(uri, HttpMethod.GET,
new HttpEntity<String>("parameters"), String.class);
return entity.getBody();
}
Run Code Online (Sandbox Code Playgroud)
您的代码看起来是正确的,如果您收到这样的错误,我很确定您搞砸了导入,请检查您使用的类是否来自以下包:
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import java.net.URI;
Run Code Online (Sandbox Code Playgroud)
我敢打赌你的 URI 类来自错误的类。
| 归档时间: |
|
| 查看次数: |
6787 次 |
| 最近记录: |