I'm using android annotation within Android Studio and I encounter an issue when implementing a rest client. My code below.
Anyone can help on this?
i try to create a simple restClient
here is my RestClient Code :+1:
@Rest(rootUrl = "https://www.azerty.com/soa/stock/service/stock",converters = { ProductMessageConverter.class, StringHttpMessageConverter.class })
public interface MyRestClient {
@Get("/all")
ProductList getAll();
@Delete("/id/{id}")
void deleteById(Integer id);
}
When building the project I got this error :
Error:(40, 25) error: reference to exchange is ambiguous both method exchange(String,HttpMethod,HttpEntity,Class,Map) in RestTemplate and method exchange(String,HttpMethod,HttpEntity,ParameterizedTypeReference,Map) in RestTemplate match where T#1,T#2 are type-variables: T#1 extends Object declared in method exchange(String,HttpMethod,HttpEntity,Class,Map) T#2 extends Object declared in method exchange(String,HttpMethod,HttpEntity,ParameterizedTypeReference,Map)
And I see this class is generated by Android-annotation :
public final class MyRestClient_
implements MyRestClient
{
private String rootUrl;
private RestTemplate restTemplate;
public MyRestClient_(Context context) {
rootUrl = "https://www.azerty.com/soa/stock/service/stock";
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ProductMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
}
@Override
public ProductList getAll() {`enter code here`
return restTemplate.exchange(rootUrl.concat("/all"), HttpMethod.GET, null, ProductList.class).getBody();
}
@Override
public void deleteById(Integer id) {
{
HashMap<String, Object> urlVariables = new HashMap<String, Object>();
urlVariables.put("id", id);
/*ERROR IS BECAUSE OF THE NEXT LINE*/
restTemplate.exchange(rootUrl.concat("/id/{id}"), HttpMethod.DELETE, null, null, urlVariables);
}
}
}
I also posting the answer here for convenience.
The problem here is Spring for Android 2.0 introduced a new overload of the
exchange
method, for the new generics support. This made the calls generated by AA ambigous. The problem is solved from AndroidAnnotations version 3.3. But you should upgrade to 3.3.1, which is the current latest version. See this for more info.