title: Posting JSON to REST API
body: <p>I'm creating a REST API that will accept JSON requests.</p>
  
  <p>I'm testing it out using CURL:</p>
  
  <pre><code>curl -i -POST -H 'Accept: application/json' -d '{"id":1,"pan":11111}' http://localhost:8080/PurchaseAPIServer/api/purchase
  </code></pre>
  
  <p><br />
  But getting the following error:</p>
  
  <pre><code>HTTP/1.1 415 Unsupported Media Type
  Server: Apache-Coyote/1.1
  Content-Type: text/html;charset=utf-8
  Content-Length: 1051
  Date: Wed, 25 Apr 2012 21:36:14 GMT
  
  The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
  </code></pre>
  
  <p><br /><br />
  When debugging it never even gets into my create action in the controller.</p>
  
  <pre><code>import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.bind.annotation.ResponseStatus;
  
  import com.app.model.Purchase;
  import com.app.service.IPurchaseService;
  
  @Controller
  public class PurchaseController {
  
      @Autowired
      private IPurchaseService purchaseService;
  
      @RequestMapping(value = "purchase", method = RequestMethod.GET)
      @ResponseBody
      public final List&lt;Purchase&gt; getAll() {
          return purchaseService.getAll();
      }
  
      @RequestMapping(value = "purchase", method = RequestMethod.POST)
      @ResponseStatus( HttpStatus.CREATED )
      public void create(@RequestBody final Purchase entity) {
          purchaseService.addPurchase(entity);
      }
  }
  </code></pre>
  
  <p><br /><br /></p>
  
  <h2>UPDATE</h2>
  
  <p>I added Jackson config to AppConfig.java:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "com.app")
  public class AppConfig {
  
      @Bean
      public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter()
      {
          final AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter();
          final MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJacksonHttpMessageConverter();
  
          HttpMessageConverter&lt;?&gt;[] httpMessageConverter = { mappingJacksonHttpMessageConverter };
  
          String[] supportedHttpMethods = { "POST", "GET", "HEAD" };
  
          annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
          annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);
  
          return annotationMethodHandlerAdapter;
      }
  }
  </code></pre>
  
  <p><br /><br />
  My GETs are working correctly now:</p>
  
  <pre><code>curl -i -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/PurchaseAPIServer/api/purchase
  
  HTTP/1.1 200 OK
  Server: Apache-Coyote/1.1
  Content-Type: application/json
  Transfer-Encoding: chunked
  Date: Thu, 26 Apr 2012 21:19:55 GMT
  
  [{"id":1,"pan":111}]
  </code></pre>
  
  <p><br /><br />
  But I get the following when attempting a POST:</p>
  
  <pre><code>curl -i -X POST -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/PurchaseAPIServer/api/purchaseMe -d "{"id":2,"pan":122}"
  
  HTTP/1.1 400 Bad Request
  Server: Apache-Coyote/1.1
  Content-Type: text/html;charset=utf-8
  Content-Length: 971
  Date: Thu, 26 Apr 2012 21:29:56 GMT
  Connection: close
  
  The request sent by the client was syntactically incorrect ().
  </code></pre>
  
  <p><br /><br /></p>
  
  <p>My Model:</p>
  
  <pre><code>@Entity
  @XmlRootElement
  public class Purchase implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 6603477834338392140L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private Long pan;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Long getPan() {
          return pan;
      }
  
      public void setPan(Long pan) {
          this.pan = pan;
      }
  
  }
  </code></pre>
  
  <p><br /><br />
  Any ideas where I'm going wrong?</p>
  
  <p>Thanks</p>