Java @Path 和正则表达式 (Jersey/REST)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945947/
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
@Path and regular expression (Jersey/REST)
提问by rafa.ferreira
I'm using Jerseyin a RESTproject and I'm needing to use regular expression.
我在REST项目中使用Jersey,我需要使用正则表达式。
Digging about it is simple like:
挖掘它很简单,例如:
@Path("/resources")
public class MyResource {
@GET
@Path("{subResources:.*}")
public String get(@PathParam("subResources") String subResources) {...}
}
But, doing like this, the method is getting the request onlyif I passes 1 param, example:
但是,这样做,只有当我传递 1 个参数时,该方法才会获取请求,例如:
GET: .../resources/firstSubResource
获取:.../resources/firstSubResource
If I use more then 1 parameter the method is notgetting the request, example:
如果我使用超过 1 个参数,则该方法未收到请求,例如:
GET: .../resources/firstSubResource/seccondSubResource/thirdSubResource
获取:.../resources/firstSubResource/seccondSubResource/thirdSubResource
I'm only capable of using regex if in my @Path contains a variableor text value, example:
如果在我的@Path 中包含变量或文本值,我只能使用正则表达式,例如:
@Path("{SubResource1}/{subResources:.*}")
Or
或者
@Path("/hardCodeString/{subResources:.*}")
Today I could run with this solution of a variable, but is not oK for my perspective.
今天我可以使用这个变量的解决方案来运行,但在我看来是不行的。
My web.xml
我的 web.xml
(...)
<servlet>
<servlet-name>Jersey Spring Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.myproject.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Spring Web Application</servlet-name>
<url-pattern>/1.0/*</url-pattern>
</servlet-mapping>
(...)
Question
题
- Does anyone have worked with something related?
- I'm doing something wrong?
- I think that this could be a bug, when working with more then one @Path, one in the Class and other in the Method.
- Any tips is appreciated!
- 有没有人做过相关的事情?
- 我做错了什么?
- 我认为这可能是一个错误,当使用多个@Path,一个在类中,另一个在方法中时。
- 任何提示表示赞赏!
Regards
问候
回答by P. Deters
Would you be opposed to accepting a single PathParam, representing a collection of subResources delimited by some token?
您是否会反对接受单个 PathParam,代表由某个标记分隔的子资源集合?
For example ...
例如 ...
@Path("/resources) public class MyResource { @GET @Path("{subResources}") public String get(@PathParam("subResources") String subResources) { String[] params = StringUtils.split(subResources, ";"); } }
.. should handle
.. 应该处理
GET: .../resources/firstSubResource & GET: .../resources/firstSubResource;seccondSubResource;thirdSubResource
回答by jaga
Can you try using regular expression as given in Overview of JAX-RS 1.1
您可以尝试使用JAX-RS 1.1 概述中给出的正则表达式吗
Code snippet would look as below for your case
对于您的情况,代码片段如下所示
@Path("resources/")
public class MyResource {
@GET
@Path("{subResources: [a-zA-Z0-9_/]+}")
public String get(@PathParam("subResources") String subResources) {...}
}
Hope that helps.
希望有帮助。
回答by user306708
Can you try removing your @PathParamannotation and instead get the path by UriInfo:
您可以尝试删除@PathParam注释,而是通过UriInfo获取路径:
@Context UriInfo uriInfo;
@GET
@Path("{subResources:.*}")
public String get()
{
String path = uriInfo.getPath();
}
I don't know why but it works in my application.
我不知道为什么,但它适用于我的应用程序。
回答by paullth
I know this is a really old question, but I just came across it whilst trying looking for the solution for myself. Im trying to accept s3 style filenames, eg /folder/whatever/blah/.../image.jpg that could be of any conceivable length and contain many /
s.
我知道这是一个非常古老的问题,但我只是在尝试为自己寻找解决方案时遇到了它。我试图接受 s3 样式的文件名,例如 /folder/whatever/blah/.../image.jpg 可以是任何可以想象的长度并包含许多/
s。
Anyway your own solution:
无论如何你自己的解决方案:
@Path("/hardCodeString/{subResources:.*}")
Gave me an idea... turns out this works:
给了我一个想法......原来这有效:
@Path("/{subResources:.*}")
notice the initial /
. Now maybe in three years this is something that they've fixed or whatever, but I hope this helps someone as this page seems to be the only place that mentions this predicament.
注意初始/
. 现在也许三年后,他们已经解决了这个问题,但我希望这对某人有所帮助,因为这个页面似乎是唯一提到这种困境的地方。