我正在使用泽西,并希望仅输出列出的以下
JSON输出:
[ { "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" } ]
如果我必须,我可以用{“Feed”:…}包围该JSON,但是这是可选的将是最好的.我想从存储在通过Hibernate检索的成员POJO中的CalendarFeed列表中提取这些信息.以下是简化的POJO:
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; }
目前,我有一个名为CalendarResource的泽西资源,手动输出JSON与日历Feed信息:
@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(); } }
但我不知道如何自动化这一点.我刚开始使用泽西,不清楚如何使用它来返回JSON.这听起来像是有一种内置的方法,但是看起来我需要添加注释到我的POJO.另外,我读了别人说我需要使用杰克逊.我一直在谷歌搜索,似乎找不到从泽西资源返回JSON的一个很好的简单例子.有人知道吗或者你可以告诉我如何使用Jackson或Jersey为上面的例子创建JSON?
解决方法@H_502_16@
我想出了如何使用Jackson 1.4做到这一点.我不使用jersey-json,因为它是基于旧版本的Jackson,我需要1.4版本来使用JsonViews.
这是注释的pojo:
public class CalendarFeed {
public enum FeedType { GCAL,EVENT };
@JsonIgnore
private Member owner;
private String name;
private String value;
@JsonIgnore
private FeedType type;
}
这是泽西资源:
@Path("/calendar")
public class CalendarResource {
@Inject("memberService")
private MemberService memberService;
@Inject
private ObjectMapper mapper;
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getCalendars() {
Member member = memberService.getCurrentMember();
try {
return mapper.writeValueAsString(member.getCalendarFeeds());
} catch (JsonGenerationException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
return "{}";
}
}
这是我的春豆
<!-- Jackson JSON ObjectMapper -->
<bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
输出正是我需要的.并使用JsonViews,我可以自定义哪些字段可以为不同的情况输出.
希望这会帮助别人!
这是注释的pojo:
public class CalendarFeed { public enum FeedType { GCAL,EVENT }; @JsonIgnore private Member owner; private String name; private String value; @JsonIgnore private FeedType type; }
这是泽西资源:
@Path("/calendar") public class CalendarResource { @Inject("memberService") private MemberService memberService; @Inject private ObjectMapper mapper; @GET @Produces(MediaType.APPLICATION_JSON) public String getCalendars() { Member member = memberService.getCurrentMember(); try { return mapper.writeValueAsString(member.getCalendarFeeds()); } catch (JsonGenerationException e) { } catch (JsonMappingException e) { } catch (IOException e) { } return "{}"; } }
这是我的春豆
<!-- Jackson JSON ObjectMapper --> <bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
输出正是我需要的.并使用JsonViews,我可以自定义哪些字段可以为不同的情况输出.
希望这会帮助别人!