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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5503272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:26:07  来源:igfitidea点击:

How can I map semicolon-separated PathParams in Jersey?

javarestjerseyjax-rs

提问by mjn

Is there a way to use this parameter style:

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

/products/123;456;789

/产品/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

在 JAX-RS 和 Jersey 中?如果我使用 PathParam,则只返回列表中的第一个参数。我试图转义分号,但 Jersey 仅返回“123;456;789”作为第一个参数列表条目的值

I declared the GET method as

我将 GET 方法声明为

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

Update: I am referring to the Jersey user guidefor Jersey 1.1.5:

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

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.

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

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

}

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

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

Update: the changelog for Jersey 1.3include this information:

更新:Jersey 1.3 的更新日志包括以下信息:

Fixed issue 540
http://java.net/jira/browse/JERSEY-540Parameterized 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.

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

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

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

回答by Tarlog

When you use semicolon, you create Matrix parameters. You can use either @MatrixParamor PathSegmentto get them. Example:

当您使用分号时,您将创建Matrix 参数。您可以使用@MatrixParamPathSegment来获取它们。例子:

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

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.

注意 Matrix 参数是那些遵循原始参数的参数。所以在“123;456;789”的情况下 - 123 是路径参数,而 456 和 789 是矩阵参数的名称。

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

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

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

Pay attention that your url should be /products/ids;123;456;789

注意你的网址应该是 /products/ids;123;456;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:

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

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