@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.
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(); }
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(); }
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); } }
AA will automatically detect and implement some methods in Rest
annotated interface. These methods will let you dynamically customize the
RestClient.
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(); }
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); }
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 :
getRootUrl()
and
setRootUrl()
getRestTemplate()
and
setRestTemplate()
getHeader()
,
setHeader()
, getCookie()
, setCookie()
,
setAuthentication()
and setHttpBasicAuth()
Modifier and Type | Required Element and Description |
---|---|
java.lang.Class<?>[] |
converters |
Modifier and Type | Optional Element and Description |
---|---|
java.lang.Class<?>[] |
interceptors |
java.lang.Class<?> |
requestFactory |
java.lang.String |
rootUrl |
public abstract java.lang.Class<?>[] converters
public abstract java.lang.String rootUrl
public abstract java.lang.Class<?>[] interceptors
public abstract java.lang.Class<?> requestFactory
Copyright © 2010-2014. All Rights Reserved.