2014-11-24 58 views

回答

2

這是一個非常簡單的例子。你需要做的只是創建具體的課程,並將其作爲資源添加到你的球衣中。除非你想,否則你不需要重寫方法。

public abstract class GenericResource<T extends GenericObject> { 

    protected HashMap<UUID, T> database = new HashMap<>(); 

    @GET 
    public Collection<T> list() { 
    return database.values(); 
    } 

    @GET 
    @Path("/{id}") 
    public T get(@PathParam("id") UUID id) { 
    return database.get(id); 
    } 

    @POST 
    public T save(T t) throws Exception { 
    if (database.containsKey(t.getId())) { 
     throw new Exception("an item already exists with given id " + t.getId()); 
    } 

    database.put(t.getId(), t); 

    return t; 
    } 

    @PUT 
    public T update(T t) throws Exception { 
    if (!database.containsKey(t.getId())) { 
     throw new Exception("an item does not exist with given id " + t.getId()); 
    } 

    database.put(t.getId(), t); 

    return t; 
    } 


    @DELETE 
    @Path("/{id}") 
    public void delete(@PathParam("id") UUID id) throws Exception { 
    if (database.containsKey(id)) { 
     throw new Exception("an item already exists with given id " + id); 
    } 

    database.remove(id); 
    } 
}