将列表传递给 RESTful Web 服务
有没有办法将列表传递给 Jersey 中的 RESTFul Web 服务方法?像 @PathParam("list") 列表列表之类的东西?
Is there a way to pass a list to RESTFul web service method in Jersey? Something like @PathParam("list") List list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
希望这将帮助您
Java 代码
使用 AJAX 从 javascript 传递值
Ajax 调用 url :
/customers?orderBy=name&orderBy=address&orderBy=...
Hope that this will help you
Java code
Passing value from javascript using AJAX
Ajax call url :
/customers?orderBy=name&orderBy=address&orderBy=...
我发现通过 POST 从客户端向 REST 服务发送列表的最佳方法是使用
@FormParam
。如果您向表单添加参数两次或更多次,则会在服务器端生成一个列表。
使用
@FormParam
意味着您在客户端生成< code>com.sun.jersey.api.representation.Form 并添加一些表单参数,如下所示。然后将填写的表单添加到帖子中,如下所示:service.path(..) ... .post(X.class, form)
(请参阅示例代码)。客户端的示例代码:
服务器端的示例代码:
返回字符串将是:
注意:
@Consumes
和客户端的.type()
的MediaTypeside 必须相同,
@Produces
和.accept()
也必须相同。@FormParam
。如果是对象,则必须将其转换为 XML或 JSON 字符串并在服务器端重新转换它。有关如何转换,请参阅此处。
form.add(someList)
,但这将导致在服务器端生成一个包含列表条目的字符串。它看起来像:[第一个字符串,第二个字符串,第三个字符串]
。您必须在服务器端以“,”分割字符串,并切断方括号以从中提取单个条目。I found out that the best way to send a list via POST from the client to a REST service is by using the
@FormParam
.If you add a parameter twice or more times to the form, it will result into a list on the server's side.
Using the
@FormParam
means on client side you generate acom.sun.jersey.api.representation.Form
and add some form parameters like shown below. Then you add the filled form to the post like that:service.path(..) ... .post(X.class, form)
(see example code).Example-code for the client side:
Example-Code for server side:
The return String will be:
Note:
@Consumes
on server side and.type()
on clientside have to be identical as well as
@Produces
and.accept()
.@FormParam
. In case of an object you'll have to convert it to XMLor JSON String and re-convert it on the server side. For how to convert see here.
form.add(someList)
, but this will result in a String containing the list's entries on server side. It will look like:[first String, second String, third String]
. You'd have to split the String on server side at "," and cut off the square brackets for extracting the single entiries from it.如果我理解您要做什么,您可以序列化 List 对象并将其作为字符串传递。
If I'm understanding what you're trying to do, you could serialize the List object and pass it as a string.