Search code examples
javaspring-bootresttemplate

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();
}

Solution

  • 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.