title: Mapping extra parameters with JAX-RS and Jackson
body: <p>I'm working on a project that uses JAX-RS, Jackson, and JPA. The JAX-RS resources map incoming JSON directly to the POJO (JPA) that is to ultimately be persisted.</p>
  
  <pre><code>@POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createEntity(Entity entity) {
     ...
  }
  </code></pre>
  
  <p>However, I occasionally find that the information that a resource needs from the client doesn't cleanly map 1:1 to a POJO. There are extra fields that provide some metadata on how to handle the request. For example, a callback URL or a plaintext password that doesn't get persisted.</p>
  
  <p>Is there an elegant way to perserve this information while still mapping directly to the JPA entity?</p>
  
  <p>I have some ideas, but I'm not thrilled with any of them:</p>
  
  <ol>
  <li><strong>First map to a <code>Map&lt;String, Object&gt;</code></strong>: Then use the ObjectMapper to map to an entity that is configured to ignore certain properties. This results in some extra boilerplate code for certain resources (possibly all resources that consume JSON for consistency's sake)</li>
  <li><strong>Use @Transient fields for extra values</strong>: This allows Jackson to map cleanly to POJOs, but tends to clutter the data model with business logic instead of just concerning itself with the state and behavior of entities.</li>
  <li><strong>Use @QueryParam for extra values</strong>: Seems to complicate the interface for the resource, and seems kind of arbitrary from a client perspective.</li>
  </ol>
  
  <p>Any ideas? It would be nice if it were possible to rig a JAX-RS MessageBodyReader or some kind of context provider to pass a Map of the extra parameters as an additional argument to the method, but I don't know how much work this would be.</p>