将列表传递给 RESTful Web 服务

发布于 2024-09-08 14:16:09 字数 77 浏览 0 评论 0原文

有没有办法将列表传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-09-15 14:16:10

希望这将帮助您

Java 代码

import java.util.List;

@Path("/customers")
public class CustomerResource {
    @GET
    @Produces("application/xml")
    public String getCustomers(
            @QueryParam("start") int start,
            @QueryParam("size") int size,
            @QueryParam("orderBy") List<String> orderBy) {
        // ...
    }
}

使用 AJAX 从 javascript 传递值

Ajax 调用 url : /customers?orderBy=name&orderBy=address&orderBy=...

Hope that this will help you

Java code

import java.util.List;

@Path("/customers")
public class CustomerResource {
    @GET
    @Produces("application/xml")
    public String getCustomers(
            @QueryParam("start") int start,
            @QueryParam("size") int size,
            @QueryParam("orderBy") List<String> orderBy) {
        // ...
    }
}

Passing value from javascript using AJAX

Ajax call url : /customers?orderBy=name&orderBy=address&orderBy=...

幻梦 2024-09-15 14:16:10

我发现通过 POST 从客户端向 REST 服务发送列表的最佳方法是使用 @FormParam
如果您向表单添加参数两次或更多次,则会在服务器端生成一个列表。

使用@FormParam意味着您在客户端生成< code>com.sun.jersey.api.representation.Form 并添加一些表单参数,如下所示。然后将填写的表单添加到帖子中,如下所示:service.path(..) ... .post(X.class, form)(请参阅示例代码)。

客户端的示例代码:

public String testMethodForList() {

    Form form = new Form();
    form.add("list", "first String");
    form.add("list", "second String");
    form.add("list", "third String");

    return service
            .path("bestellung")
            .path("add")
            .type(MediaType.APPLICATION_FORM_URLENCODED)
            .accept(MediaType.TEXT_XML)
            .post(String.class, form);
}

服务器端的示例代码:

@POST
@Path("/test")
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String testMethodForList(       
    @FormParam("list") List<String> list {

    return "The list has " + list.size() + " entries: " 
           + list.get(0) + ", " + list.get(1) + ", " + list.get(2) +".";
}

返回字符串将是:

该列表有 3 个条目:第一个字符串、第二个字符串、第三个字符串。

注意:

  • 服务器端的@Consumes和客户端的.type()的MediaType
    side 必须相同,@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 @FormParammeans on client side you generate a com.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:

public String testMethodForList() {

    Form form = new Form();
    form.add("list", "first String");
    form.add("list", "second String");
    form.add("list", "third String");

    return service
            .path("bestellung")
            .path("add")
            .type(MediaType.APPLICATION_FORM_URLENCODED)
            .accept(MediaType.TEXT_XML)
            .post(String.class, form);
}

Example-Code for server side:

@POST
@Path("/test")
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String testMethodForList(       
    @FormParam("list") List<String> list {

    return "The list has " + list.size() + " entries: " 
           + list.get(0) + ", " + list.get(1) + ", " + list.get(2) +".";
}

The return String will be:

The list has 3 entries: first String, second String, third String.

Note:

  • The MediaTypes of @Consumes on server side and .type() on client
    side have to be identical as well as @Produces and .accept().

  • You can NOT send objects other than String, Integer etc. via
    @FormParam. In case of an object you'll have to convert it to XML
    or JSON String and re-convert it on the server side. For how to convert see here.

  • You can also pass a List to the form like 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.
自我难过 2024-09-15 14:16:10

如果我理解您要做什么,您可以序列化 List 对象并将其作为字符串传递。

If I'm understanding what you're trying to do, you could serialize the List object and pass it as a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文