将数组作为参数传递给 JAX-RS 资源
我有很多参数要使用 JAX-RS 传递到服务器。
有没有办法通过 URL 或 AarryList 来传递?
I have many parameters to pass to the server using JAX-RS.
Is there a way to pass or AarryList with the URL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里有几个选择。
选项 1:具有多个值的查询参数
您可以为单个查询参数提供多个简单值。例如,您的查询字符串可能如下所示:
PUT /path/to/my/resource?param1=value1¶m1=value2¶m1=value3
这里是请求参数
param1
具有三个值,容器将允许您以数组的形式访问所有三个值(请参阅 查询字符串结构)。选项 2:在
PUT
正文中提供复杂数据如果您需要在
PUT
请求中提交复杂数据,通常可以通过在请求正文中提供该内容来完成。当然,此有效负载可以是 xml(并通过 JAXB 绑定)。请记住 URI 的要点是标识资源 (RFC 3986, 3.4),如果这个值数组是识别资源所需的数据,那么 URI 是一个很好的地方。另一方面,如果此数据数组构成在此
PUT
请求中提交的新表示的一部分,则它属于请求正文。话虽如此,除非您确实只需要一组简单值,否则我建议选择选项 2。我想不出在 URL 中使用 URL 编码的 XML 的好理由,但我很感兴趣详细了解这些数据到底是什么。
You have a few options here.
Option 1: A query parameter with multiple values
You can supply multiple simple values for a single query parameter. For example, your query string might look like:
PUT /path/to/my/resource?param1=value1¶m1=value2¶m1=value3
Here the request parameter
param1
has three values, and the container will give you access to all three values as an array (See Query string structure).Option 2: Supply complex data in the
PUT
bodyIf you need to submit complex data in a
PUT
request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).Remember the point of the URI is to identify a resource (RFC 3986, 3.4), and if this array of values is data that is needed to identify a resource then the URI is a good place for this. If on the other hand this array of data forms part of the new representation that is being submitted in this
PUT
request, then it belongs in the request body.Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.
我们可以以Map的形式获取Query参数和对应的值,
We can get the Query parameters and corresponding values as a Map,