title: Reading JSON to map with JAX-RS
body: <p>I'm writing a Java EE REST application (it runs under Glassfish 4.1) and I'm having problems reading a simple key-value JSON list. I'm trying to produce and consume a JSON using a POJO class, producing works fine, but consuming is not working. 
  Let's get to the code. The POJO class I use for produce and consume JSON is like this:</p>
  
  <pre><code>public class Transport {
    protected Integer id;
    protected String name;
    protected Map&lt;String, String&gt; fields;
    // ...getters and setters...
  }
  </code></pre>
  
  <p>The REST resource import those libraries (I've put here the relevant ones):</p>
  
  <pre><code>import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.PUT;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.UriInfo;
  import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
  import org.glassfish.jersey.media.multipart.FormDataParam;
  </code></pre>
  
  <p>The method who produces the JSON works fine, here it is:</p>
  
  <pre><code>@GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("test")
  public Response testGet() {
    Transport sct = new Transport();
    sct.setId(1);
    sct.setName("test");
    HashMap&lt;String, String&gt; t1 = new HashMap();
    t1.put("aaa", "bbb");
    t1.put("bbb", "ccc");
    t1.put("ccc", "ddd");
    sct.setFields(t1);
    // createOkResponse is a class method that return Response type
    return createOkResponse(sct);
  }
  </code></pre>
  
  <p>so I correctly obtain this output:</p>
  
  <pre><code>{
    "id": 1,
    "name": "test",
    "fields": {
       "aaa": "bbb",
       "ccc": "ddd",
       "bbb": "ccc"
    }
  }
  </code></pre>
  
  <p>The method who consume the JSON, using the same POJO:</p>
  
  <pre><code>@PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Path("test")
  public Response testPut(Transport sct) {
    Map&lt;String, String&gt; fields = sct.getFields();
    // this code throws a NullPointerException at fields.entrySet() 
    // because fields is always null
    //for (Entry&lt;String, String&gt; field : fields.entrySet()) {
    //  LOG.log(Level.INFO, "*** field: {0} = {1}", new Object[]{field.getKey(), field.getValue()});
    //}
    return createOkResponse(sct);
  }
  </code></pre>
  
  <p>Consuming the service with the same JSON above will result in an sct object with id and name properties set, but with fields property null, so the response of the method is:</p>
  
  <pre><code>{
    "id": 1,
    "name": "test",
    "fields": null
  }
  </code></pre>
  
  <p>it's like the <code>{"aaa": "bbb", "ccc": "ddd", "bbb": "ccc"}</code> JSON object is being ignored. I've tried googling around but I don't understand what is wrong with my code, and I've seen no errors or warnings in the glassfish log.
  Any idea? Thanks.</p>