简体   繁体   中英

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. I have JDK11 installed in the mac that I'm currently using

Error Message: The method exchange(URI, HttpMethod, HttpEntity, Class) in the type RestTemplate is not applicable for the arguments (URI, Http.HttpMethod, HttpEntity, Class)

Initially the exchange method was in this form: exchange(url,HttpMethod.GET, null, String.class);

When url was a String. Since the method required URI object, I changed the code. Also in my hunt for a solution, I also converted the null parameter to an object.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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