title: spring rest controller composite primary key
body: <p>I have a simple spring-data/jpa object with a composite primary key:</p>
  
  <pre><code>@Entity
  @Table(name = "position")
  public class Position implements Serializable {
  
      @EmbeddedId
      private PositionId positionId;
  
      @Column(name="heading")
      private Double heading;
  ...
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Embeddable
  public class PositionId implements Serializable {
      private String hexIdent;
      @Temporal(TemporalType.TIMESTAMP)
      private Date timestamp;
  ...
  </code></pre>
  
  <p>I'm trying to a create REST controller to create a Position object.  My controller create method looks like:</p>
  
  <pre><code>@RequestMapping(method=POST,
      consumes=APPLICATION_JSON_VALUE,
      produces=APPLICATION_JSON_VALUE)
  public ResponseEntity&lt;Position&gt; createPosition(@RequestBody Position newPosition) {
      Position position = positionService.create(newPosition);
      return ResponseEntity.ok().body(position);
  }
  </code></pre>
  
  <p>and the json I'm sending looks like:</p>
  
  <pre><code>{
  "positionId":{"hexIdent":"abc123","timestamp":"Tue Nov 10 15:20:43 MST 2015"},
  "verticalRate":3.2,
  "heading":90.2,
  "groundSpeed":123.2,
  "lon":25.2,
  "lat":25.2
  }
  </code></pre>
  
  <p>When the service is executed, it fails to resolve the PostionId (NPE) from the incoming JSON (the positionId that the REST service creates in the method call is null, it actually fails on the hashCode() call of positionId)   </p>
  
  <p>Here's the test of the service:</p>
  
  <pre><code>@Test
  public void testCreatePosition() throws Exception {
      JSONObject p003Id = new JSONObject();
      p003Id.put("hexIdent", "abc123");
      //p003Id.put("timestamp", new Date());
      JSONObject p003 = new JSONObject();
      p003.put("positionId", p003Id);
      p003.put("lat", 25.2D);
      p003.put("lon", 25.2D);
      p003.put("heading", 90.2D);
      p003.put("groundSpeed", 123.2D);
      p003.put("verticalRate", 3.2D);
      String jsonString = p003.toString();
  
      given().
          contentType("application/json").
          body(jsonString).
      when().
          post("/position").
      then().
          statusCode(HttpStatus.SC_OK).
          contentType(ContentType.JSON).
          body("hexIdent", equalTo("abc123")).
          body("lat", equalTo(25.2D)).
          body("lon", equalTo(25.2D));
  }
  </code></pre>
  
  <p>Is there a trick to defining a composite primary key in spring-data/jpa or handling that in a spring rest controller?  Is my json wrong?</p>