开发者

@Path and regular expression (Jersey/REST)

开发者 https://www.devze.com 2023-01-02 06:55 出处:网络
I\'m using Jersey in a REST project and I\'m needing to use regular expression. Digging about it is simple like:

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

Questio开发者_运维技巧n

  • 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.


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.

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.


Can you try removing your @PathParam annotation and instead get the path by 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.


Would you be opposed to accepting a single PathParam, representing a collection of subResources delimited by some token?

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
0

精彩评论

暂无评论...
验证码 换一张
取 消