@Path 和正则表达式 (Jersey/REST)正则表达式、Path、REST、Jersey

2023-09-06 10:55:45 作者:眼里往走红.

我在 REST 项目中使用 Jersey,我需要使用正则表达式.

挖掘它很简单:

@Path("/resources")公共类 MyResource {@得到@Path("{subResources:.*}")公共字符串获取(@PathParam(subResources")字符串子资源){...}}
爬虫 二 正则表达式

但是,这样做,如果我传递 1 个参数,该方法仅获取请求,例如:

GET: .../resources/firstSubResource

如果我使用超过 1 个参数,则方法是不获取请求,例如:

GET: .../resources/firstSubResource/seccondSubResource/thirdSubResource

如果我的@Path 中包含变量或文本值,我只能使用正则表达式,例如:

@Path("{SubResource1}/{subResources:.*}")

或者

@Path("/hardCodeString/{subResources:.*}")

今天我可以使用这个变量的解决方案来运行,但从我的角度来看还不行.

我的 web.xml

(...)<小服务程序><servlet-name>Jersey Spring Web 应用程序</servlet-name><servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class><初始化参数><param-name>com.sun.jersey.config.property.packages</param-name><param-value>com.myproject.rest</param-value></初始化参数><启动时加载>1</启动时加载></servlet><servlet 映射><servlet-name>Jersey Spring Web 应用程序</servlet-name><url-pattern>/1.0/*</url-pattern></servlet 映射>(...)

问题

有人从事过相关工作吗?我做错了什么?我认为这可能是一个错误,当使用多个@Path、一个在类中和另一个在方法中时.感谢任何提示!

问候

解决方案

你可以尝试使用 JAX-RS 1.1 概述

根据您的情况,代码片段如下所示

@Path("resources/")公共类 MyResource {@得到@Path("{subResources: [a-zA-Z0-9_/]+}")公共字符串获取(@PathParam(subResources")字符串子资源){...}}

希望对您有所帮助.

I'm using Jersey in a REST project and I'm needing to use regular expression.

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 only if I passes 1 param, example:

GET: .../resources/firstSubResource

If I use more then 1 parameter the method is not getting the request, example:

GET: .../resources/firstSubResource/seccondSubResource/thirdSubResource

I'm only capable of using regex if in my @Path contains a variable or text value, example:

@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

(...)
    <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!

Regards

解决方案

Can you try using regular expression as given in Overview of 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.