Last Updated: February 25, 2016
·
444
· fmancinelli

Declaring JAX-RS Resources

If you are using JAX-RS, always declare your resources by using interfaces:

@Path("/myresource")
public interface MyResource {
  @GET
  Response get(@QueryParam("param") String param);
  ...
}

This has several advantages:

  • You can put your resources in public packages of a module (or even in a separate "api" module) that are available to other modules, without exposing any implementation.

  • You can still use helper functions like UriBuilder.fromResource(...) to get the URI of your resources. This is useful if you want to send redirect headers. These interfaces, in fact, will be the authoritative source for resource's URIs.

  • Finally the package (and its subpackages) where you decide to put your resource declaration will contain a nice, concise and up-to-date specification of your HTTP API.