本文介绍了如何在Jersey中映射以分号分隔的PathParams?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有没有办法使用这个参数样式:Is there a way to use this parameter style:在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方法声明为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:更新:这是我的测试代码: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(); }}使用分号转义测试网址: http:// localhost:8080 / resources / test / 1%3B2%3B3 Test URL with semicolon escaped: http://localhost:8080/resources/test/1%3B2%3B3更新: Jersey 1.3的更新日志包括以下信息:我将根据这篇文章检查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推荐答案使用分号时,可以创建矩阵参数。 您可以使用 @MatrixParam 或 PathSegment 来获取它们。示例: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)请注意Matrix参数是遵循原始参数的参数。 因此,对于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.所以如果你想要通过ID获得产品,你可以这样做: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) 这篇关于如何在Jersey中映射以分号分隔的PathParams?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 22:33