'List<String> as input of GET method

I have a REST service. The input type of the GET method is List<String>:

@GET
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<myObject> getData(List<String> IDs) {
    ....
}

I tried to test this method using curl. I used a lot of different combinations of data:

curl -X GET --data-binary '{"IDs":["TestString1","TestString2"]}' -H "Content-Type: application/json" http://localhost:8080/myModule/rs/getData -v
                          '{"TestString1","TestString2"}'
                          '["TestString1","TestString2"]'
                           so on

But I always get the response:

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
or
Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

Is it possible to have the input as List<String>? Are my curl requests incorrect?



Solution 1:[1]

It looks like you're sending a POST request:

--data-binary < data>

(HTTP) This posts data exactly as specified with no extra processing whatsoever.

Since -X sets a custom request type, I'm guessing it's not detecting that it needs to URL encode the data:

-X, --request < command>

(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request will be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.

So try with -G instead:

-G, --get

When used, this option will make all data specified with -d, --data or --data-binary to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.

Solution 2:[2]

I found solution :)

Method signature should be

@GET
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<myObject> getData(@QueryParam("myParamName") List<String> IDs) {
    ....
}

curl request

curl -X GET -H "Content-Type: application/json" http://localhost:8080/myModule/rs/getData?myParamName={dfsf,ddsfdss,sdfsf} -v

it works :)

Solution 3:[3]

yourUrl?param1=value1&param1=value2

This will convert to

List<string>param1 = [...] 

you get the point

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Victor Mezrin
Solution 3 Suraj Rao