rest - Managing a nested resource in Jax-RS/Jersey that sometimes should behave as a non-nested resource -


i developing simple apis jax-rs jersey. let's considering domain of items sold stores in country.

my design includes these 2 calls :

  • /webapi/items
  • /webapi/store/1/items

both of them should return list of items, first 1 should return items sold in country, second 1 should return items sold store number 1.

i have of course 2 resources, itemresource handles requests regarding items, , storeresource handles requests regarding stores.

@path("items") class itemresource {  @get public list<item> getallitems(){ } 

.

@path("stores") class storeresource  @get @path("/{storeid}/items") public list<item> getitemssoldbystore(@pathparam("storeid") long storeid) { } 

what pass second request itemresource, in order avoid coupling between storeresource , model class item or database interfaces (like daos) created manage items. know can consider itemresource sub-resource, or nested resource of storeresource, point not true, since call itemresource without passing store id, items (this case of first request http://foo.com/webapi/items ). i'd keep @path("items") annotation on itemresource handles every request /items endpoint.

what correct design in situation? help.

you can create nested resource returning resource parent resource.

// parent resource @path("stores") class storeresource {    @get   @path("/{storeid}/items")   public list<item> getitemssoldbystore(@pathparam("storeid") long storeid) {      return itemresource(storeid);   } }  // nested resource  class itemresource {    @get   @path("{storeid}")   public list<item> getallitems(@pathparam("storeid")){      // return items   } } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -