I m using Jersey and want to output the following JSON with only the fields listed:
[
{
"name": "Holidays",
"value": "http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic"
},
{
"name": "Personal",
"value": "http://www.google.com/calendar/feeds/myprivatefeed/basic"
}
]
If I must, I can surround that JSON with {"feeds": ... }, but having this be optional would be best. I want to pull this information from a list of CalendarFeeds that are stored in a Member POJO that is retrieved via Hibernate. Here are the simplified POJOs:
public class Member {
private String username;
private String password;
private Set<CalendarFeed> calendarFeeds = new HashSet<CalendarFeed>();
}
public class CalendarFeed {
public enum FeedType { GCAL, EVENT };
private Member owner;
private String name;
private String value;
private FeedType type;
}
Currently, I ve got a Jersey resource called CalendarResource that manually outputs JSON with the calendar feeds information:
@Path("/calendars")
public class CalendarResource {
@Inject("memberService")
private MemberService memberService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getCalendars() {
// Get currently logged in member
Member member = memberService.getCurrentMember();
StringBuilder out = new StringBuilder("[");
boolean first = true;
for (CalendarFeed feed : member.getPerson().getCalendarFeeds()) {
if (!first) {
out.append(",");
}
out.append("{"");
out.append(feed.getName());
out.append("":"");
out.append(feed.getValue());
out.append(""}");
first = false;
}
out.append("]");
return out.toString();
}
}
But I m not sure how to go about automating this. I m just starting to use Jersey and am not clear on how to use it to return JSON. It sounds like it has a way to do this built in, but it looks like I need to add annotations to my POJOs. Also, I read others saying that I need to use Jackson. I ve been googling and can t seem to locate a good and simple example of returning JSON from a Jersey resource. Anyone know of any? Or can you show me how to use Jackson or Jersey to create JSON for for the above example?