1

I have some trouble with understanding the perfect way to apply the REST pattern for objects and subobjects. Let us consider following classes:

class Foo {
    Long id;
    Bar bar;
}

class Bar {
    Long id;
    String name;
}

As far as I understand in simple cases we have:

  1. List of Foo objects - .../foos
  2. One Foo object - .../foos/{id}

Now I have problem understanding how accesing subobjects will work. What will be correct if I want to fetch Foo objects, in which Bar objects has name == baz

Whether that will be .../foos/bar/name/baz or /foos?barName=baz?

Which mapping should I use to fetch Bar objects by parameter name out of Foo?. Would that will be .../foos/bar/{name} or something different?

krzakov
  • 3,871
  • 11
  • 37
  • 52

1 Answers1

3

The main question is: which resource are you requesting? In your case, you want a list of foos, so the URI will be /foos, period. Remaining constraints must be set some other way.

Then, you want to filter the returned list so that only foos with a specific bar are retrieved. This could be done via a parameter: /foos?barName=someName.

Here are some common URLs:

  1. GET /foos: list all foos
  2. GET /foos/{id}: get specific foo
  3. GET /bars: list all bars
  4. GET /bars/{id}: get specific bar
  5. GET /foos/{id}/bars: get all bars of specific foo
  6. GET /foos/{id}/bars/{id}: get specific bar of specific foo (same as GET /bars/{id} but more restrictive)
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • `Then, you want to filter the returned list so that only foos with a specific bar are retrieved. This could be done via a parameter: /foos?barName=someName.` Since parameters are only of `Bar` type wouldn't it be better: `GET .../foos/bar?name=sample` ? – krzakov Dec 04 '15 at 09:38