What is the right way to organize Jersey resources using inheritance and generics?

I am developing an application with Jersey where I have many resources. Although the basic functionality of these resources is different, they use many common methods (for example, list, read, update, etc.). The application runs on the Google App Engine and uses Guice for dependency injection.

My first approach was to have a common AbstactResource that contains all the common logic and, accordingly, is expanded by all other resources that add their required user methods.

public class AbstractResource<T> {

@GET
public ListPage<T> list(@QueryParam("limit") Integer limit,
    @QueryParam("start") Integer start) {
    // ... implementation
}

@GET
@Path("/{id}")
public T get(@PathParam("id") Long id) {
    // ... implementation
}

And an example resource looks like this:

public class TenantResource extends AbstractResource<Tenant> {
    // custom resource related methods here
}

. , . , . , AbstractResource AudiatableResource, .

public abstract class AuditableResource<T extends AuditableModel> 
    extends AbstractResource {
        // here I override update and create methods to save changelogs
}

, ( AuditableModel).

:

public class PropertyResource extends AuditableResource<Tenant> {
    // custom resource related methods here
}

, :

WARNING: Return type T of method public T com.pkg.AbstractResource.get(java.lang.Long) is not resolvable to a concrete type
WARNING: Return type T of method public T com.pkg.AbstractResource.getNew() is not resolvable to a concrete type
WARNING: Return type com.pkg.data.ListPage<T> of method public com.pkg.ListPage<T> com.pkg.AbstractResource.list(java.lang.Integer,java.lang.Integer) is not resolvable to a concrete type

, , . , , .

+5
1

- .

  • , , . , API, , . , ,
  • - , .

, , , . , . "" REST , .

public class Facade {
  private final PropertyResource propertyResource;
  public Facade() {
    propertyResource = new PropertyResource();
  }
  @GET
  @Path("somepath")
  public Tenant something() {
    return propertyResource.something();
  }
}
+4

All Articles