users@jersey.java.net

[Jersey] Generic Resources

From: Artem Grebenkin <gremoz_at_googlemail.com>
Date: Fri, 30 Nov 2012 18:54:13 +0100

Hi folks,

I have some unusual use case and would be very appreciated for any help. In my particular case a generic resource, where I could set a @Path, mapping and extend it, would be very very helpful. Something like this:

@Path("/t")/*path set*/
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class BookResource<T> {

GenericDAO<Integer, T> tStore;

    public GenericDAO<Integer, T> getBook() {
return tStore;
}

    @Inject
public void setTStore(GenericDAO<Integer, T> tStore) {
this.tStore = tStore;
}
    
    @GET
    @Path("{id}")
    public T findById(@PathParam("id") long id) {
        return tStore.getById(id);
    }
    
    @GET
    @Path("{number:(/number/[^/]+?)?}{offset:(/offset/[^/]+?)?}")
    public List<T> findAll(@PathParam("number") long number, @PathParam("offset") long offset) {
        //_at_TODO If any of params is null give 0 forward
        return tStore.getAll(number, offset);
    }
    
    @POST
    public int create(T instance) {
        tStore.create(instance);
    }
    
    @PUT
    @Path("{id}")
    public int update(T instance) {
        tStore.update(instance);
    }

    @DELETE
    @Path("{id}")
    public int remove(@PathParam("id") long id) {
        tStore.delete(id);
    }
}

Do I have any chance to implement this?

Thanks,
Artem.