Announcement Announcement Module
Collapse
No announcement yet.
JSON output using @ResponseBody Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Hi Marty,

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

      Regards,
      Paul.

      Comment

      Working...
      X