java - How to generate JSON from a Jersey resource? -


I am using jersey and want to output the following JSON with the listed fields only:

< Pre> [{"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 want to I can have it on {JSON: "} with Feeds": ...}, but it would be best if it was optional. I want to draw this information from a list of calendar information that is stored in the member POO, which is obtained by Hibernate. There are simplified POJOs:

  public class members {Private string username; Private string password; Private set & lt; CalendarFeed & gt; Calendar Feeds = New Hashet & lt; Calendar feed & gt; (); } Public class CalendarFeed {public enum FeedType {GCAL, EVENT}; Owner of private member; The name of the private string; Private string value; Private feed type type; }  

Currently, I have a jersey resource called calendar reassers which manually outputs JSON with calendar feeds information:

 < Code> @Path ("/ calendars") Public class calendar resources {@ Inject ("Member Service") Private Members Service Members Service; @GET @Produces (MediaType.APPLICATION_JSON) public string getCalendars () {// currently subscribe member member member member = serviceService.getCurrentMember (); Stringbuilder exits = new string builder ("["); Boolean first = true; (Calendar feed feed: member.getPerson (). GetCalendarFeeds ()) {If (! First) {out.append (","); } Out.append ("{\" "out.append (feed.getName ()); out.append (" \ ": \" "); out.append (feed.getValue ()); out.append (" \ "}"); First = Incorrect;} Out.append ("]"); Return Out.String ();}}  

But I'm not sure how to automate it I'm just starting to use jerseys and it's not clear how to use it back to JSON. It seems that there is a way to do this, but it seems that I Pozzo needs to add annotation Besides, I have read others saying that I need to use Jackson. I have been googling and can not seem to find a good and simple example of JSON back from a Jersey resource, anyone can Do you know? Or can you use Jason or Jersey for the example shown above?

I found out how to use this jackson 1.4. I like jersey-js Is based on an earlier version of I'm not using the connection because Jackson and I use Jesnwug to version requires 1.4.

Here's the annotated POS:

  Public class calendar feed {public anonymous feed type {GCAL, EVENT}; @ Jason Ignor Personal Member Owner; The name of the private string; Private string value; @JsonIgnore Personal FeedType Type; Here is the Jersey Resource:  
  @ path ("/ calendar") Public category calendar resources {@ Inject ("member service") Private subscribers Service member service; Remove the @ Private objectMapper Mapper; @GET @ Products (MediaType.APILICATION_JSON) Public String Result Calendars (Member) {Member Member = Member Service.Customer Member (); Try {return mapper.writeValueAsString (member.getCalendarFeeds ()); } Catch (JS Exposure E) {} Catch (JSMappingException E) {} Catch (IOE EXPRESS E) {} Return "{}"; }}  

Here is my spring bean:

    

Output what I really want and by using JasonViews, I can customize fields to output for different conditions.

Hope this will help someone else!


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -