2

Currently I have set up a RESTful API to return 1 or more Book(s) that belongsTo an Author. Although this is what I want, it is not in the format I like it to be.

When I give an ID of 2 for the Author, I get the following response:

<author id="2">
    <author_name>Stephen</author_name>
    <books>
        <book id="2"/>
        <book id="3"/>
    </books>
</author>

Instead I would like a response that shows the contents of the books instead of a reference:

<list>
    <book id="2">
        <book_name>BookA</book_name>
        <book_year>2001</book_year>
    </book>
    <book id="3">
        <book_name>BookB</book_name>
        <book_year>2005</book_year>
    </book>
</list>

My contents of the controller that produces the first response is:

@Override
protected Book queryForResource(Serializable id) {
    def p = Author.id == params.AuthorId

    Book.where {
        id == id && p
    }.find()
}

Also is it possible to sort by book_year descending? Any help will be much appreciated.

tim_yates
  • 167,322
  • 27
  • 342
  • 338

1 Answers1

0

This is because you are using default marshellers. Here is the answer for changing JSON response. You have to repeat this for XML. Everything else is same.

Community
  • 1
  • 1
Ejaz Ahmed
  • 654
  • 4
  • 17