@Path和正则表达式(Jersey / REST) [英] @Path and regular expression (Jersey/REST)
问题描述
我在 REST 项目中使用 Jersey ,我需要使用正则表达式。
I'm using Jersey in a REST project and I'm needing to use regular expression.
挖掘它很简单:
@Path("/resources")
public class MyResource {
@GET
@Path("{subResources:.*}")
public String get(@PathParam("subResources") String subResources) {...}
}
但是,这样做,方法就是获取请求仅如果我通过1个参数,例如:
But, doing like this, the method is getting the request only if I passes 1 param, example:
GET:... / resources / firstSubResource
GET: .../resources/firstSubResource
如果我使用多于1个参数,方法不获取请求,例如:
If I use more then 1 parameter the method is not getting the request, example:
获取:... / resources / firstSubResource / seccondSubResource / thirdSubResource
GET: .../resources/firstSubResource/seccondSubResource/thirdSubResource
如果我的@Path包含变量或文本值,我只能使用正则表达式,例如:
I'm only capable of using regex if in my @Path contains a variable or text value, example:
@Path("{SubResource1}/{subResources:.*}")
或
@Path("/hardCodeString/{subResources:.*}")
今天我可以运行这个变量的解决方案,但不是我的观点。
Today I could run with this solution of a variable, but is not oK for my perspective.
我的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>
(...)
问题
- 有没有人使用过相关的东西?
- 我'我做错了什么?
- 我认为这可能是一个错误,当使用多个@Path时,一个在Class中,另一个在Method中。
- 感谢任何提示!
- 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!
问候
推荐答案
你可以尝试使用 JAX-RS 1.1概述
您的案例代码如下所示
@Path("resources/")
public class MyResource {
@GET
@Path("{subResources: [a-zA-Z0-9_/]+}")
public String get(@PathParam("subResources") String subResources) {...}
}
希望有所帮助。
这篇关于@Path和正则表达式(Jersey / REST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!