RestTemplate.exchange方法错误:不适用的参数

时间:2019-01-25 06:55:03

标签: java spring-boot resttemplate

使用基于Eclipse的Spring Suite时,尝试在Sprint引导上运行示例程序时看到错误消息。我目前正在使用

的Mac中安装了JDK11

错误消息: RestTemplate类型的方法exchange(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();
}

1 个答案:

答案 0 :(得分:0)

您的代码看起来正确,如果出现这样的错误,我很确定您搞砸了导入,请检查您使用的类是否来自以下软件包:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import java.net.URI;

我敢打赌,您的URI类来自错误的类。

相关问题