如何在Jersey中映射以分号分隔的PathParams?

mjn*_*mjn 13 java rest jax-rs jersey

有没有办法使用此参数样式:

/产品/ 123; 456; 789

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

我将GET方法声明为

public List<Product> getClichedMessage(@PathParam("ids") List<String> idList)
Run Code Online (Sandbox Code Playgroud)

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

通常,方法参数的Java类型可以是(...)4)是List,Set或SortedSet,其中T满足上面的2或3.生成的集合是只读的.(...)有时参数可能包含同一个名称的多个值.如果是这种情况,则可以使用4)中的类型来获得所有值.

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

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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

更新:Jersey 1.3更改日志包含以下信息:

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

我会在此基础上后退房StringReaderProvider http://comments.gmane.org/gmane.comp.java.jersey.user/7545

Tar*_*log 22

使用分号时,可以创建Matrix参数.您可以使用@MatrixParamPathSegment获取它们.例:

 public String get(@PathParam("param") PathSegment pathSegment)
Run Code Online (Sandbox Code Playgroud)

请注意Matrix参数是遵循原始参数的参数.因此,对于"123; 456; 789" - 123是路径参数,而456和789是矩阵参数的名称.

因此,如果您想通过ID获取产品,您可以执行以下操作:

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

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

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

public List<Product> getClichedMessage(@QueryParam("id") List<String> ids)
Run Code Online (Sandbox Code Playgroud)