Results 1 to 3 of 3

Thread: JSON output using @ResponseBody

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    28

    Default JSON output using @ResponseBody

    Hi,

    I have a controller method called getAlbums() which is actually called via Ajax.

    public @ResponseBody Collection<Album> getAlbums(HttpServletRequest request,@RequestParam int start,@RequestParam int limit) {

    Collection<Album> albums = null;
    albums = albumDao.findAlbums(1,start,limit);
    return albums;
    }
    This works perfectly and returns the collection of albums as a JSON encoded string.

    However, what I actually need is to return an additional property in the JSON response which indicates the total number of albums for that user in the database, this property should be at the beginning or end of the JSON response and is used for paging purposes on the front end.

    for example: { "totalAlbums":10, albums[{ ... }] }

    How do I go about doing this as if I change the return type to String then I don't get a JSON response any longer.

    Is there a way to add attributes/fields like in the example above to a already formatted JSON response?

    Thanks,
    Paul.

  2. #2
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    I would think you would need to create a Wrapper class that will hold the size of the album collection as a Attribute and the Collection itself as a attribute.

  3. #3
    Join Date
    Jan 2010
    Posts
    28

    Default

    Hi Marty,

    Don't know why I didn't think of that. Perfect, works a treat

    Regards,
    Paul.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •