Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well you have to create and register your custom serializer.</p> <p>It goes like this:</p> <pre><code>//You create your builder that registers your custom serializer with the class you want to serialize GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Standard.class, new StandardSerializer()); //Then you create your Gson object Gson gson = builder.create(); //Then you pass your object to the gson like Standard s = DAOFactory.getInstance().getStandardDAO().findById(id); gson.toJson(s); </code></pre> <p>Your serializer looks like this:</p> <pre><code>public class StandardSerializer implements JsonSerializer&lt;Standard&gt;{ @Override public JsonElement serialize(Standard src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); //You put your simple objects in like this obj.add("id",new JsonPrimitive(src.getId())); //You put your complex objects in like this JsonObject interpretations = new JsonObject(); //Here you need to parse your LinkedHashSet object and set up the values. //For the sake of simplicity I just access the properties (even though I know this would not compile) interpretations.add("title", src.getInterpretation().getTitle()); obj.add("interpretations", interpretations); return obj; } } </code></pre> <p>In this case your Json would look something like:</p> <pre><code>{"id":"idValue", "title":"titleValue", "description":"descriptionValue", "interpretations":["id":"interpretationIdValue"]} </code></pre> <p>Now, you can access your data with <code>jQuery</code> like this:</p> <pre><code>function newStandard() { $.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) { alert(data.interpretations.title); }); } </code></pre> <p>I hope this helps.</p> <p><strong>EDIT:</strong> I see that your response gets converted to the declared method argument type which is <code>String</code> (as stated <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html" rel="nofollow">here</a>: 16.3.3.2 Supported method return types). But what you really want is your <code>Standrad</code> POJO converted to JSON. I am not very familiar with Spring but as I have read <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html" rel="nofollow">here</a> (16.3.2.6 Producible Media Types) there is another, maybe easier solution. If you want to return a JSON object, then change the return type of the <code>getStandard</code> method to <code>Standard</code> instead of <code>String</code> and add <code>produces="application/json"</code> to your <code>@RequestMapping</code> annotation. As far as I have read this should tell <code>Spring</code> that the return type should be converted to JSON. In this case you do not need to use <code>Gson</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload