- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有没有办法使用这个参数样式:
/products/123;456;789
在带有 Jersey 的 JAX-RS 中?如果我使用 PathParam,则只返回列表中的第一个参数。我试图转义分号,但 Jersey 仅返回“123;456;789”作为第一个参数列表条目的值
我将 GET 方法声明为
public List<Product> getClichedMessage(@PathParam("ids") List<String> idList)
更新:我指的是 Jersey 1.1.5 的 Jersey user guide:
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.
更新:这是我的测试代码:
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
更新:changelog for Jersey 1.3 包含以下信息:
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.
我会根据这篇文章 http://comments.gmane.org/gmane.comp.java.jersey.user/7545 查看 StringReaderProvider
最佳答案
当你使用分号时,你创建了 Matrix parameters 。您可以使用 @MatrixParam
或 PathSegment
来获取它们。示例:
public String get(@PathParam("param") PathSegment pathSegment)
请注意,Matrix 参数是那些跟在原始参数之后的参数。所以在 "123;456;789"的情况下 - 123 是路径参数,而 456 和 789 是矩阵参数的名称。
所以如果你想通过id获取产品,你可以这样做:
public List<Product> getClichedMessage(@PathParam("ids") PathSegment pathSegment) {
Set<String> ids = pathSegment.getMatrixParameters().keySet();
// continue coding
}
注意你的url应该是/products/ids;123;456;789
实际上,IMO 这不是一个很好的设计:您使用矩阵参数名称作为值。我认为使用查询参数更好:/products?id=123&id=456&id=789
,因此您可以轻松地在方法中获取它们:
public List<Product> getClichedMessage(@QueryParam("id") List<String> ids)
关于java - 如何在 Jersey 中映射以分号分隔的 PathParams?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503272/
我创建了一个 Spring Boot 应用程序,我想在其中使用 Web 套接字。当我在没有参数的情况下使用它时,它工作正常。下面是不带参数的代码 @Configuration @EnableWebSo
我有一个 POST 方法(它需要一些 JSON 数据)。我还想访问路径参数“id”进行处理。但是,当我使用“@PathParam”时,我得到了传递给请求的 JSON 正文。下面是我的代码: @Path
即使您刚刚阅读它,也感谢您的帮助。 问题:返回值为null,为什么?检查了很多论坛,一切似乎都很好,但事实并非如此。 package pack.bb; import javax.ws.rs.GET;
嗨,我已经检查了很多帖子,但我还没有发现我遇到的问题。我的 PathParam 总是为空,谁能告诉我可能是什么问题 界面中的导入: import javax.servlet.http.HttpServ
我想要构建的 API 将包含如下资源: @GET @Path("document/{embedded_path"}) @Produces("text/plain") public String get
我们面临与使路径参数可选相关的问题。 原始网址 /expire/{token} 我们正在尝试做的是为下面的 URL 提供相同的服务。 1. /expire/{token} 2. /expi
我试图从 url 中获取 id,但它始终为空。 @Path("/{id}") @GET @Produces({MediaType.TEXT_XML, MediaType.APPLICATI
我尝试使用 Jersey 来使用 @PathParam,但它总是将其视为 null。 方法如下:网址为 http://localhost:8080/GiftRegistryAPI/api/v2/inv
public Response getCustomerByName( @PathParam("customerName") String customerName) 问题: 我将 customerNa
在 Jersey 类中,两者哪个更合适: 在实例变量上 @PathParam("服务编号")私有(private)字符串 serviceId; 关于方法参数 public Response subsc
我想使用非 spring bean 类对象作为 jersey web 服务类方法的参数。但它在构建时给出了缺少依赖项错误。 我的代码是: @Component @Path("/abcd") publi
我有一个 REST 服务: @POST @Path("/shop/{prodID}/{price}") public Response updatePrice(@PathParam("prodID")
我已经设置了一个 REST 服务来访问存储在数据库中的信息。 我希望能够根据项目的 id 或 name 进行访问。 假设我有一个记录 name | id | description mine | 65
@RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @
我是 RESTful Jersey 的新手。请问@PathParam有什么区别和 @QueryParam Jersey ? 最佳答案 查询参数添加到?后面的url标记,而路径参数是常规 URL 的一部
我正在处理创建 Jersey 客户端的任务。我正在使用 Jersey 1.18。目标 URL 如下所示。 https://api.test.com/test/{id}?param1=test1&par
我正在开发一个 REST 网络服务,我在其中使用 URL 中的 ID 来指定患者,如下所示: WS/services/patient/1 其中“1”是患者的 ID。所以,在代码中,我这样指定: @GE
我正在使用 JAX-RS 1.x 开发一个简单的 RESTFul 服务,如/{app_id}/job/list/ 所以我定义了一个资源类,如下所示: @Path("/{app_id}/job") @P
我已经使用基本的 Express 服务器启动并运行了 TypeScript,但我遇到了一个问题。 import bodyParser from 'body-parser'; import expres
我在 jax-rs 中有一个服务文件,它有一个通过 @QueryParam 注释接受参数的方法。 注释显然是在编译过程中的某个地方进行处理的。执行修改操作的是哪个类?就像 QueryParam 从 U
我是一名优秀的程序员,十分优秀!