title: Jackson object mapping - map incoming JSON field to protected property in base class
body: <p>We use Jersey/Jackson for our REST application. Incoming JSON strings get mapped to the @Entity objects in the backend by Jackson to be persisted. </p>
  
  <p>The problem arises from the base class that we use for all entities. It has a protected id property, which we want to exchange via REST as well so that when we send an object that has dependencies, hibernate will automatically fetch these dependencies by their ids. </p>
  
  <p>Howevery, Jackson does not access the setter, even if we override it in the subclass to be public. We also tried using @JsonSetter but to no avail. Probably Jackson just looks at the base class and sees ID is not accessible so it skips setting it...</p>
  
  <pre><code>@MappedSuperclass
  public abstract class AbstractPersistable&lt;PK extends Serializable&gt; implements Persistable&lt;PK&gt; {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private PK id;
  
      public PK getId() {
          return id;
      }
  
      protected void setId(final PK id) {
          this.id = id;
      }
  </code></pre>
  
  <p>Subclasses:</p>
  
  <pre><code>public class A extends AbstractPersistable&lt;Long&gt; {
      private String name;
  }
  
  public class B extends AbstractPersistable&lt;Long&gt; {
      private A a;
  
      private int value;
  
      // getter, setter
  
      // make base class setter accessible
      @Override
      @JsonSetter("id")
      public void setId(Long id) {
          super.setId(id);
      }
  }
  </code></pre>
  
  <p>Now if there are some As in our database and we want to create a new B via the REST resource:</p>
  
  <pre><code>@POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Transactional
  public Response create(B b) {
      if (b.getA().getId() == null) 
          cry();
  }
  </code></pre>
  
  <p>with a JSON String like this <code>{"a":{"id":"1","name":"foo"},"value":"123"}</code>.</p>
  
  <p><strong>The incoming B will have the A reference but without an ID.</strong></p>
  
  <hr>
  
  <p>Is there any way to tell Jackson to either ignore the base class setter or tell it to use the subclass setter instead?</p>
  
  <p>I've just found out about <code>@JsonTypeInfo</code> but I'm not sure this is what I need or how to use it.</p>
  
  <p>Thanks for any help!</p>
  
  <hr>
  
  <p><strong>edit</strong></p>
  
  <p>StaxMan has me worrying I am missing something here, so I'll add my servlet configuration as this is the only other point I can think of where anything might go wrong, if my described behavior should actually be possible without adding an XML annotation to the setter (as described in my answer):</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;
          com.sun.jersey.spi.spring.container.servlet.SpringServlet
      &lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
          &lt;param-value&gt;path.to.rest.resources&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>