{title:'Dual-purpose (end-to-end) interfaces', created:'8.0.0'}

A common coding practice is to use the same Java interface to define both your server and client side REST interfaces. The advantage to this approach is that changes that you make to your REST interface can be reflected in both places at the same time, reducing the chances for compatibility mistakes.

What makes this possible is that method-level annotations such as @RestOp and parameter-level annotations such as @Query are inherited from parent classes. This normally isn't possible, but the framework will spider up the parent hierarchy of classes to find method and parameter level annotations defined on overridden methods.

The general approach is to define your {@link oaj.http.remote.Remote @Remote}-annotated interface first. The following example is pulled from the PetStore app:

| @Remote(path="/petstore") | public interface PetStore { | | @RemoteGet("/pet") | public Collection<Pet> getPets() throws NotAcceptable; | | @RemotDelete("/pet/{petId}") | public Ok deletePet( | @Header( | name="api_key", | description="Security API key", | required=true, | example="foobar" | ) | String apiKey, | @Path( | name="petId", | description="Pet id to delete", | example="123" | ) | long petId | ) throws IdNotFound, NotAcceptable; | | ...

Next you define the implementation of your interface as a normal Juneau REST resource:

| @Rest( | path="/petstore", | title="Petstore application", | ... | ) | public class PetStoreResource extends BasicRestServlet implements BasicUniversalJenaConfig, PetStore { | | ... | | @Override /* PetStore */ | @RestOp( | method=GET, | path="/pet", | summary="All pets in the store", | ... | ) | public Collection<Pet> getPets() throws NotAcceptable { | return store.getPets(); | } | | @Override /* PetStore */ | @RestOp( | method=DELETE, | path="/pet/{petId}", | summary="Deletes a pet", | ... | ) | public Ok deletePet(String apiKey, long petId) throws IdNotFound, NotAcceptable { | store.removePet(petId); | return OK; | }

Then use the interface as a remote resource like so:

| RestClient client = RestClient.create().json().rootUrl("http://localhost:10000").build(); | PetStore store = client.getRemote(PetStore.class); | | for (Pet pet : store.getPets()) { | store.deletePet("my-special-key", pet.getId()); | System.err.println("Deleted pet: id=" + pet.getId()); | }

In the example above, we chose to add the @RestOp annotation to the implementation class. However, they could have been added to the interface instead. It's personal preference where you want to place the annotations.

Note how we didn't need to use the @Header and @Path annotations in our implementation since the annotations were inherited from the interface.