title: Spring MVC, REST, Jackson, Dojo dojox.data.jsonRestStore, how to make dojo-style references?
body: <p>Pretty simple question, hard to find an answer:</p>
  
  <p><strong>I want to Serialize in JSON my POJO JPA entities with references being Dojo-like to use it with dojox.data.JsonRestStore.</strong></p>
  
  <p>The server part is made with Spring MVC and HttpConverters to make it a RESTfull web-app. I'm using JacksonJson to convert JPA entities to JSON.</p>
  
  <p>As a lot of people have, I do have problems with multiple references. @JsonBackReference and @JsonManagedReference do not do the trick!</p>
  
  <p>Considering this basic example:</p>
  
  <pre><code>public class A {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(nullable=false)
      private String field;
  
      @OneToMany(mappedBy = "b")
      private List&lt;B&gt; bs;
  }
  
  public class B {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      @ManyToOne
      private A a;
  }
  </code></pre>
  
  <p>With the basic Jackson Json converter you obtain something like this (considering you have put the @JsonBackReference and @JsonManagedReference):</p>
  
  <pre><code>{ id:1, fieldB:"bbbbb", a: { id:1, fieldA: "aaaaaa" }}
  </code></pre>
  
  <p>In a concrete application, having the list of bs from the A objects is pointless in most cases. But having knowing what kind of A object is in the B.a field is important (at least to me).</p>
  
  <p>Now, the problem lies in the references which are copied and you can end up having some serious big JSON with not much data in it.</p>
  
  <p>The Dojo Framework provides a reference standard explained in here : <a href="http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/" rel="nofollow">http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/</a> and
  <a href="http://dojotoolkit.org/reference-guide/dojox/data/JsonRestStore.html" rel="nofollow">http://dojotoolkit.org/reference-guide/dojox/data/JsonRestStore.html</a>.</p>
  
  <p>So what I'm searching is a way to define this as the following:</p>
  
  <pre><code>{ id:1, fieldB:"bbbbb", a: { $ref: "getA/1"}}
  </code></pre>
  
  <p>First problem: Jackson obviously can't know what url you will use for the Reference.</p>
  
  <p>To resolve that problem I made very simple interface like this:</p>
  
  <pre><code>public interface EntityWithId {
      public Long getId();
      public void setId(Long id);
  }
  </code></pre>
  
  <p>This interface in then implemented by all my entities and I made this very very simple JsonSerializer:</p>
  
  <pre><code>public class EntityIdSerializer extends JsonSerializer&lt;EntityWithId&gt; {
  
      @Override
      public void serialize(EntityWithId value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                  jgen.writeStartObject();
                  jgen.writeStringField("$ref", "db/" + value.getClass().getSimpleName() + "/" + value.getId().toString());
                  jgen.writeEndObject();
      }
  }
  </code></pre>
  
  <p>With those two I got the desired JSON, adding <strong>@JsonSerialize(using = EntityIdSerializer.class)</strong> to the <strong>@ManyToOne</strong> fields.</p>
  
  <p>Is there no more standard approach? The Dojo dojox.data.JsonRestStore sounds really promising as you can combine it with lots of Dijits widgets, but is it really a nice thing to do and try? Won't my REST app be to much Dojo-restrictive?</p>
  
  <p>Thanks for your answers!</p>