title: Converting JSON array into java List<String> with Spring MVC
body: <p>I have a list of string in my javascript code which I send via JQuery to a REST based service as follows:</p>
  
  <pre><code>var ids = []; 
  $("input:checked").each(function() {
      ids.push(this.id);
  });
  var selectedIds = JSON.stringify(ids);
  $.post("/todonotes/tasks/removeTask", selectedIds,function(e) {
  }, "json");
  </code></pre>
  
  <p>As you can see, I convert a javascript array into a JSON array.</p>
  
  <p>Now, in the server side, I use Spring MVC and Jackson to receive and parse the input JSON:</p>
  
  <pre><code>@RequestMapping(value="/tasks/removeTask", method = RequestMethod.POST)
  public @ResponseBody String removeTask(@RequestBody List&lt;String&gt; selectedIds) {
      ...
  }
  </code></pre>
  
  <p>But I always get:</p>
  
  <pre><code>HTTP Status 415 - 
  
  --------------------------------------------------------------------------------
  
  type Status report message 
  
  description 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>I tried to remove the quotes from the JSON object, and also tried using <code>@RequestParam</code> without success.</p>
  
  <p>Update as per OQJF suggestion:</p>
  
  <p>I modified my post request as follows:</p>
  
  <pre><code>$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds},function(e) {  
  }, "json");
  </code></pre>
  
  <p>Now my controller method gets called, but each <code>List</code> element is populated with the double quotes and brackets. This the List argument:</p>
  
  <pre><code>[["15", "21"]]
  </code></pre>
  
  <p>So e.g. the first element looks like:</p>
  
  <pre><code>["15"
  </code></pre>
  
  <p>I would prefer not to parse each <code>String</code> element of the <code>List</code>.</p>
  
  <p>I also tried removing the <code>JSON.stringify</code> conversion but my controller didn't even get called. With this I got:</p>
  
  <pre><code>HTTP Status 400 - Required List parameter 'selectedIds' is not present
  
  --------------------------------------------------------------------------------
  
  type Status report
  
  message Required List parameter 'selectedIds' is not present
  
  description The request sent by the client was syntactically incorrect.
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I tried to create an object to hold my ids, as follows:</p>
  
  <pre><code>public class TaskIdHolder {
  
      private String[] selectedIds;
  
      public String[] getSelectedIds() {
          return selectedIds;
      }
  
      public void setSelectedIds(String[] selectedIds) {
          this.selectedIds = selectedIds;
      }
  }
  </code></pre>
  
  <p>Then I modified my controller method signature:</p>
  
  <pre><code>public @ResponseBody String removeTask(@RequestBody TaskIdHolder selectedIds) {
  </code></pre>
  
  <p>And my JSON request looks like this:</p>
  
  <pre><code>{"selectedIds":["15"]}
  </code></pre>
  
  <p>I also modified my Jquery code as per Andrei's suggestion:</p>
  
  <pre><code>var data = JSON.stringify({selectedIds:selectedIds});
  $.post("/todonotes/tasks/removeTask", data, function(e) {
  </code></pre>
  
  <p>Where selectedIds is a JavaScript array.</p>
  
  <p>But now I get:</p>
  
  <pre><code>POST http://localhost:8080/todonotes/tasks/removeTask 415 (Unsupported Media Type) 
  </code></pre>