简体   繁体   English

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

[英]RestTemplate.exchange method error: not applicable arguments

I'm seeing an error message when trying to run a sample program on Sprint boot, while using Eclipse based Spring Suite. 使用基于Eclipse的Spring Suite时,尝试在Sprint引导上运行示例程序时,看到一条错误消息。 I have JDK11 installed in the mac that I'm currently using 我在当前使用的Mac中安装了JDK11

Error Message: The method exchange(URI, HttpMethod, HttpEntity, Class) in the type RestTemplate is not applicable for the arguments (URI, Http.HttpMethod, HttpEntity, Class) 错误消息: RestTemplate类型的方法exchange(URI,HttpMethod,HttpEntity,Class)不适用于参数(URI,Http.HttpMethod,HttpEntity,Class)

Initially the exchange method was in this form: exchange(url,HttpMethod.GET, null, String.class); 最初,交换方法采用以下形式: exchange(url,HttpMethod.GET, null, String.class);

When url was a String. 当url为字符串时。 Since the method required URI object, I changed the code. 由于该方法需要URI对象,因此我更改了代码。 Also in my hunt for a solution, I also converted the null parameter to an object. 同样在寻找解决方案的过程中,我还将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();
}

Your code looks correct, if you get such an error I am pretty sure you messed up with your imports, check that your used classes come from following packages: 您的代码看起来正确,如果出现这样的错误,我很确定您搞砸了导入,请检查您使用的类是否来自以下软件包:

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

I bet your URI class comes from a wrong one. 我敢打赌,您的URI类来自错误的类。

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

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