1

I'd like to use a customized DomainClassMarshaller/ObjectMarshaller class in grails to get a nice paginated list.

I'd like the full like to appear something like this:

> GET /api/company/1/bookings
< 200 OK
{
  "url": "/api/company/1/bookings",
  "size": 42,
  "pageSize": 10,
  "nextPage": "/api/company/1/bookings/?page=2,
  "prevPage": null,
  "items": [
    { ... // booking 1
    },
    ...
    { ... // booking 10
    },
  ]
}

Whereas if there's has_a relationship between domain objects, and you get the parent object you'll just get the URL, which you can then dive into if necessary.

> GET /api/company/1
< 200 OK

{
  "url": "/api/company/1"
  ...
  "bookings": "/api/company/1/bookings"
}

However, if you're faced with a list of simple types, just go ahead and output them:

> GET /api/user/1
< 200 OK
{
  "url": "/api/user/1",
  "name" "Bob,
  "favoriteThings": [
    "Raindrops on roses",
    "Whiskers on kittens",
    "Bright copper kettles",
    "Warm woolen mittens"
  ]
}

DomainClassMarshaller has a very nice little method called asShortObject which seems like it would do the trick.

My first thought would be to register a DomainClassMarshaller that only accepts Lists of domain classes, and another one that accepts simple lists. Is that the best approach?

Are there any resources out there on doing something similar to this? I can't believe I'm the first one to have this requirement.

Thanks for all your help, David

4

0 に答える 0