Bookmark and Share

Quick Start

Interface

CRest instance returns proxies over any given user java REST interfaces, as long as the interface is properly annotated.

The interface's methods can contains any kind of arguments and any kind of result type, with some specials cases:

java.io.InputStream and java.io.Reader when used as return types, are both always returning the server raw response. It is the client responsability to always call the close() method on them to release network resources.

Response can also be returned as byte[] or java.lang.String when used as return types.

java.io.InputStream, java.io.Reader, java.io.File can be used as arguments for any REST parameter type.

CRest

To get a CRest instance, you can do as follow:

CRest crest = CRest.getInstance();

CRest class has more static method to get a customized CRest instance. Another way of getting a CRest instance is to do as follow:

CRest crest = new CRestBuilder().build();

CRest instances are thread-safe, and should be created only once and reused when building REST interface proxies.

The builder allows you to easily customize the resulting CRest instance, please refer to its java-doc to see all the possible options available.

Current implementation requires all user provided interface method's parameter to be annotated with any of the following:

  • @FormParam
  • @PathParam
  • @MatrixParam
  • @QueryParam
  • @HeaderParam
  • @CookieParam
  • @MultiPartParam

Once you have a CRest instance, you can pass it any java Interface that should be bind to any REST service, as follow.

Given a simple REST interface:

@EndPoint("http://ajax.googleapis.com")
@Path("ajax/services/search")
@QueryParam(value = "v", defaultValue = "1.0")
@ResponseHandler(GoogleResponseHandler.class)
public interface SearchService {

    @Path("web")
    SearchResult<Address> search(@QueryParam("q") String text);

}

(...)
CRest crest = CRest.getInstance();
SearchService searchService = crest.build(SearchService.class);