I have a API endpoint method like this:
我有一个像这样的API端点方法:
@ApiMethod(name = "test")
public TestModel test(@Named("testArray") ArrayList<String> myTest) {
for (String i:myTest){
log.warning("i=="+i);
}
A request is made as such:
请求是这样的:
{
"testArray":[
"ID1",
"ID2"
]
}
However, when I check my log (and also the problems this caused in my app), I see
但是,当我检查我的日志(以及我的应用程序中导致的问题)时,我明白了
i=="ID1,ID2"
not how i expected to see:
不是我期望看到的:
i=="ID1"
i=="ID2"
as the output. It is putting both elements of the array into myTest(0). How do I populate the Array correctly?
作为输出。它将数组的两个元素放入myTest(0)。如何正确填充数组?
2 个解决方案
#1
2
This issue doesn't appear to occur on the latest SDK 1.9.22.
最新的SDK 1.9.22似乎没有出现此问题。
I'm wondering how you're creating the request, and if it's possible that you're just joining the strings with commas as a single element in the array that you send. While I know it sounds crazy to do this deliberately and I don't think you're doing that, it's possible that some layer of abstraction below your code does it?
我想知道你是如何创建请求的,如果你可能只是用逗号作为你发送的数组中的单个元素加入字符串。虽然我知道故意这样做听起来很疯狂,但我不认为你这样做,你的代码下面的某些抽象层可能会这样做吗?
Another possibility is that you're not using the generated client library for your Endpoints API (you really should, it does a lot of work that you'd end up replicating on your own), and you're forming raw JSON to send over? In that case, I can see how maybe the request that ultimately gets sent might be different from what you expect, collapsing all the values inside the array that "testArray" is a key to into a comma-separated list.
另一种可能性是你没有为你的Endpoints API使用生成的客户端库(你真的应该,它做了很多你自己最终复制的工作),并且你正在形成原始的JSON来发送?在这种情况下,我可以看到最终发送的请求可能与您期望的不同,将“testArray”中的所有值折叠为逗号分隔列表中的键。
So, my best advice is to examine how you form the request, and if possible, get access to the raw request itself inside your endpoints method to see what was sent.
因此,我最好的建议是检查您如何形成请求,如果可能的话,在端点方法中访问原始请求本身以查看发送的内容。
#2
1
The easiest way, since this seems like a bug, is to not use a named parameter. Create a new request class that has a list field and use that as an unnamed (aka resource) parameter, instead.
最简单的方法,因为这似乎是一个错误,是不使用命名参数。创建一个具有列表字段的新请求类,并将其用作未命名(也称为资源)参数。
#1
2
This issue doesn't appear to occur on the latest SDK 1.9.22.
最新的SDK 1.9.22似乎没有出现此问题。
I'm wondering how you're creating the request, and if it's possible that you're just joining the strings with commas as a single element in the array that you send. While I know it sounds crazy to do this deliberately and I don't think you're doing that, it's possible that some layer of abstraction below your code does it?
我想知道你是如何创建请求的,如果你可能只是用逗号作为你发送的数组中的单个元素加入字符串。虽然我知道故意这样做听起来很疯狂,但我不认为你这样做,你的代码下面的某些抽象层可能会这样做吗?
Another possibility is that you're not using the generated client library for your Endpoints API (you really should, it does a lot of work that you'd end up replicating on your own), and you're forming raw JSON to send over? In that case, I can see how maybe the request that ultimately gets sent might be different from what you expect, collapsing all the values inside the array that "testArray" is a key to into a comma-separated list.
另一种可能性是你没有为你的Endpoints API使用生成的客户端库(你真的应该,它做了很多你自己最终复制的工作),并且你正在形成原始的JSON来发送?在这种情况下,我可以看到最终发送的请求可能与您期望的不同,将“testArray”中的所有值折叠为逗号分隔列表中的键。
So, my best advice is to examine how you form the request, and if possible, get access to the raw request itself inside your endpoints method to see what was sent.
因此,我最好的建议是检查您如何形成请求,如果可能的话,在端点方法中访问原始请求本身以查看发送的内容。
#2
1
The easiest way, since this seems like a bug, is to not use a named parameter. Create a new request class that has a list field and use that as an unnamed (aka resource) parameter, instead.
最简单的方法,因为这似乎是一个错误,是不使用命名参数。创建一个具有列表字段的新请求类,并将其用作未命名(也称为资源)参数。