Converting JSON array into Java List<String> with Spring MVC

I have a list of string in my JavaScript code which I send via jQuery to a REST based service as follows:

var ids = []; 
$("input:checked").each(function() {
    ids.push(this.id);
});
var selectedIds = JSON.stringify(ids);
$.post("/todonotes/tasks/removeTask", selectedIds,function(e) {
}, "json");

As you can see, I convert a JavaScript array into a JSON array.

Now, in the server side, I use Spring MVC and Jackson to receive and parse the input JSON:

@RequestMapping(value="/tasks/removeTask", method = RequestMethod.POST)
public @ResponseBody String removeTask(@RequestBody List<String> selectedIds) {
    ...
}

But I always get:

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.

I tried to remove the quotes from the JSON object, and also tried using @RequestParam without success.

Update as per OQJF suggestion:

I modified my post request as follows:

$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds},function(e) {  
}, "json");

Now my controller method gets called, but each List element is populated with the double quotes and brackets. This the List argument:

[["15", "21"]]

So e.g. the first element looks like:

["15"

I would prefer not to parse each String element of the List.

I also tried removing the JSON.stringify conversion but my controller didn't even get called. With this I got:

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.

Update

I tried to create an object to hold my ids, as follows:

public class TaskIdHolder {
    
    private String[] selectedIds;

    public String[] getSelectedIds() {
        return selectedIds;
    }

    public void setSelectedIds(String[] selectedIds) {
        this.selectedIds = selectedIds;
    }
}

Then I modified my controller method signature:

public @ResponseBody String removeTask(@RequestBody TaskIdHolder selectedIds) {

And my JSON request looks like this:

{"selectedIds":["15"]}

I also modified my jQuery code as per Andrei's suggestion:

var data = JSON.stringify({selectedIds:selectedIds});
$.post("/todonotes/tasks/removeTask", data, function(e) {

Where selectedIds is a JavaScript array.

But now I get:

POST http://localhost:8080/todonotes/tasks/removeTask 415 (Unsupported Media Type) 

Upvotes: 4

Views: 21314

Answers (3)

user2608369
user2608369

Reputation: 31

Try the following, no need to change your controller's method signature or create an object to hold your ids.

$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds.join(',')},function(e) {  
}, "json");

That way you'll be sending your ids like selectedIds: id1,id2,id3 which is how the Controller is expecting them to be if you declare your request param to be String[] or List<String> (works for both scenarios).

Upvotes: 3

OQJF
OQJF

Reputation: 1350

As I know that you don't need to use JSON.stringify(), your code can be like this:

var ids = []; 
$("input:checked").each(function() {
    ids.push(this.id);
});

$.post("/todonotes/tasks/removeTask", $.param({'id': ids}, true),function(e) {
}, "json");

And on the server side, you should specify the value of:

public methodName(@RequestParam("id") List<String> selectedIds)

Upvotes: 4

Andrei
Andrei

Reputation: 56688

Try giving the the request parameter an explicit name:

$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds},...

Update. You can also try to stringify the whole request data object to JSON:

var data = {selectedIds: selectedIds};
$.post("/todonotes/tasks/removeTask", JSON.stringify(data),...

Upvotes: 2

Related Questions