java - List<String> 作为 GET 方法的输入

标签 java string list jersey

我有 REST 服务。 GET方法的输入类型为List<String> :

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

我尝试使用curl来测试这个方法。我使用了很多不同的数据组合:

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

但我总是得到回应:

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

是否可以输入List<String> ?我的curl请求不正确吗?

最佳答案

看起来您是sending a POST request :

--data-binary < data>

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

-X sets a custom request type ,我猜它没有检测到需要对数据进行 URL 编码:

-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.

所以尝试使用 -G相反:

-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.

关于java - List<String> 作为 GET 方法的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573874/

相关文章:

JavaFX : Supply arguments to Task

java - 返回包含另一个链表中的键的链表

c++ - 用查询中的变量替换表名

android - 使用游标获取列内容

python通过列表创建一个包含一行的数据框

r - 将向量列表转换为数据框

java - Android 6.0 文件写入权限被拒绝

java - 使用 Java Microsoft graph SDK 在 Outlook 中添加成员

string - 我不能像我想的那样返回字符串 slice 。只通过最后一个

java - String.format - 用点填充字符串