org.androidannotations.annotations.rest
Annotation Type Rest


@Retention(value=CLASS)
@Target(value=TYPE)
public @interface Rest

Apply @Rest on an interface to create a RestService class that will contain implementation of rest calls related to the methods you define in the interface.

You should then inject your RestService class by using RestService annotation in any enhanced classes.

Note: Implementation is based on Spring Android Rest-template library. So you MUST have the library in your classpath and we highly recommend you to take some time to read this document and understand how the library works.

Converters

Every Rest annotated interface MUST define at least one converters() to tell the library how to convert received data into Java objects.

converters() value MAY contain one or several HttpMessageConverter sub-classes

Example : The following RestClient will use Jackson to deserialize received data as Java objects.
 @Rest(converters = MappingJackson2HttpMessageConverter.class)
 public interface RestClient {
 
        @Get("http://myserver/events")
        EventList getEvents();
 }
 

Root url

If you don't wan't to repeat the root URL in each method, you MAY like the rootUrl() field. It let you define a common root URL which will be prefixed on every method of your RestClient.

Example :
 @Rest(rootUrl = "http://myserver", converters = MappingJackson2HttpMessageConverter.class)
 public interface RestClient {
 
        @Get("/events")
        EventList getEvents();
 
        @Get("/lastevent")
        Event getLastEvent();
 }
 

Interceptors

Sometimes you may want to do extra processing right before or after requests. interceptors() field let you define one or several org.springframework.http.client.ClientHttpRequestInterceptor.

An interceptor allow the developer to customize the execution flow of requests. It may be useful to handle custom authentication, automatically log each requests, and so on.

Example :
 @Rest(converters = MappingJacksonHttpMessageConverter.class, interceptors = HttpBasicAuthenticatorInterceptor.class)
 public interface MyRestClient {
 
        @Get("/events")
        EventList getEvents();
 }
 
 public class HttpBasicAuthenticatorInterceptor implements ClientHttpRequestInterceptor {
 
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
                // do something before sending request
                return execution.execute(request, data);
        }
 }
 

Magic methods

AA will automatically detect and implement some methods in Rest annotated interface. These methods will let you dynamically customize the RestClient.

RootUrl

We seen earlier that root url can be set via rootUrl() annotation field, but it only takes a constant. If you want to dynamically inject or retrieve the root url, you can add the following code :

 @Rest(converters = MappingJacksonHttpMessageConverter.class)
 public interface MyRestClient {
 
        void setRootUrl(String rootUrl);
 
        String getRootUrl();
 }
 

RestTemplate

If you want to configure the injected RestTemplate used internally, AA will also detect getter and setter for this object.

 @Rest(converters = MappingJacksonHttpMessageConverter.class)
 public interface MyRestClient {
 
        RestTemplate getRestTemplate();
 
        void setRestTemplate(RestTemplate restTemplate);
 }
 

Bundle interfaces

Since 3.0, we're also providing some bundle interface your RestClient can extends of. Each of them provide handled methods subset and let you clean your code by using extends composition instead of writing methods.

Available bundle interfaces :

See Also:
RestService, RestClientSupport, RestClientRootUrl, RestClientHeaders

Required Element Summary
 Class<?>[] converters
           
 
Optional Element Summary
 Class<?>[] interceptors
           
 String rootUrl
           
 

Element Detail

converters

public abstract Class<?>[] converters

rootUrl

public abstract String rootUrl
Default:
""

interceptors

public abstract Class<?>[] interceptors
Default:
{}


Copyright © 2010-2014. All Rights Reserved.