title: Spring MVC RestTemplate POST using JSON
body: <p>I have a Spring MVC Rest Controller that simply adds a customer record. My Rest Service passes around JSON and works fine when I call it from a web client or a test bed like Soap-UI. However, when I try to POST using RestTemplate I get a JacksonMappingException: Can not construct instance of CustomerObject, problem: no suitable creator method found.</p>
  
  <p>It's clearly something to do with the way I am creating the JSON request so to add some meat to the bones here is what I'm doing:</p>
  
  <p>My Test posts using RestTemplate</p>
  
  <pre><code> @Test
   public void postCustomerIntegrationTest() throws Exception{
  
      String JSONInput = ("{" +
            "  \"firstName\": \"Anouska\"," +
            "  \"lastName\": \"Williams\"," +
            "  \"email\": \"Anouska@place.com\"," +
            "  \"cardBin1\": 123456768," +
            "  \"cardBin2\": 123456789," +
            "  \"language\": \"Spanish\"," +
            "  \"country\": \"Mexico\"," +
            "  \"product\": \"shit\"," +
            "  \"telephoneNumber\": 447869995262," +
            "  \"termsAndConditions\": \"true\"" +
            "}");
  
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
  
    HttpEntity request= new HttpEntity(JSONInput, headers);
  
  Registration output = restTemplate.postForObject(BASE_URL, request, CustomerObject.class);
  
  assertNotNull("no person",output);
  assertNotNull(output.getId());
  assertEquals("Bob", output.getFirstName());
  
  }
  </code></pre>
  
  <p>My RestTemplate is wired through Spring as follows:</p>
  
  <pre><code>&lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
  &lt;property name="messageConverters"&gt;
      &lt;list&gt;
          &lt;bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;
              &lt;property name="supportedMediaTypes" value="application/json" /&gt;
          &lt;/bean&gt;
      &lt;/list&gt;
  &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The test JSON I'm using here is the same as the test JSON I use in Soap-UI. Having debuged the code it looks like it is treating my JSON as a String and Jackson is trying to convert the entire thing because it can't find an assiciated field (which of course it can't).</p>
  
  <p>I guess I need to know how to pass JSON to my RestTemplate in such a way that the request will be picked up in the correct JSON format. I've spent all morning trying to find an example and can't find one anywhere.</p>
  
  <p>Many Thanks for your help.</p>