如何在泽西岛映射分号分隔的 PathParams?

2022-01-21 00:00:00 rest java jax-rs jersey

有没有办法使用这个参数样式:

Is there a way to use this parameter style:

/products/123;456;789

/products/123;456;789

在 JAX-RS 和泽西岛?如果我使用 PathParam,则只返回列表中的第一个参数.我试图转义分号,但随后 Jersey 只返回123;456;789"作为第一个参数列表条目的值

in JAX-RS with Jersey? If I use PathParam, only the first parameter in the list is returned. I tried to escape the semicolon but then Jersey returns only "123;456;789" as the value of the first parameter list entry

我将 GET 方法声明为

I declared the GET method as

public List<Product> getClichedMessage(@PathParam("ids") List<String> idList)

更新:我指的是 Jersey 用户指南 泽西岛 1.1.5:

Update: I am referring to the Jersey user guide for Jersey 1.1.5:

一般来说方法的Java类型参数可能 (...) 4) 是列表,Set 或 SortedSet,其中 T满足以上 2 或 3 条.所结果的集合是只读的.(...)有时参数可能包含更多比同名的一个值.如果就是这种情况,然后输入 4) 可能用于获取所有值.

In general the Java type of the method parameter may (...) 4) be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only. (...) Sometimes parameters may contain more than one value for the same name. If this is the case then types in 4) may be used to obtain all values.

更新:这是我的测试代码:

Update: here is my test code:

package de.betabeans.resources;

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/test")
public class TestResource {

    @GET
    @Path("/{ids}")
    @Produces({"text/plain"})
    public String getClichedMessage(@PathParam("ids") List<String> idList) {
        return "size=" + idList.size();
    }

}

用分号转义的测试 URL:http://localhost:8080/resources/test/1%3B2%3B3

Test URL with semicolon escaped: http://localhost:8080/resources/test/1%3B2%3B3

更新:泽西岛变更日志1.3 包含以下信息:

Update: the changelog for Jersey 1.3 include this information:

已修复问题 540
http://java.net/jira/browse/JERSEY-540参数化类型支持 List/Set/SortedSet参数,例如@QueryParam("d") 列表>,如果有 StringReaderProvider注册支持该类型列表.

Fixed issue 540
http://java.net/jira/browse/JERSEY-540 Parameterized types of List/Set/SortedSet are supported for parameters, for example @QueryParam("d") List>, if there is a StringReaderProvider registered that supports the type List.

我将根据这篇文章查看 StringReaderProvider http://comments.gmane.org/gmane.comp.java.jersey.user/7545

I'll check out StringReaderProvider based on this post http://comments.gmane.org/gmane.comp.java.jersey.user/7545

推荐答案

使用分号时,会创建 矩阵参数.您可以使用 @MatrixParamPathSegment 来获取它们.示例:

When you use semicolon, you create Matrix parameters. You can use either @MatrixParam or PathSegment to get them. Example:

 public String get(@PathParam("param") PathSegment pathSegment)

注意矩阵参数是跟在原始参数后面的.所以在 "123;456;789" 的情况下 - 123 是路径参数,而 456 和 789 是矩阵参数的名称.

Pay attention that Matrix parameters are these that follow the original parameter. So in case of "123;456;789" - 123 is path parameter, while 456 and 789 are the names of matrix parameters.

所以如果你想通过 ids 获取产品,你可以这样做:

So if you want to get products by ids, you can do something like this:

public List<Product> getClichedMessage(@PathParam("ids") PathSegment pathSegment) {
    Set<String> ids = pathSegment.getMatrixParameters().keySet();
    // continue coding
}

注意你的网址应该是/products/ids;123;456;789

实际上,IMO 这不是一个很好的设计:您使用矩阵参数名称作为值.我认为使用查询参数更好:/products?id=123&id=456&id=789,因此您可以轻松地在方法中获取它们:

Actually, IMO it is not a very good design: you use matrix parameter name as a value. I think using query parameters is better: /products?id=123&id=456&id=789, so you can easily get them in method:

public List<Product> getClichedMessage(@QueryParam("id") List<String> ids)

相关文章