25

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

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
rafa.ferreira
  • 1,997
  • 7
  • 26
  • 41
  • 1
    Would you please post more information including: the and tags from `web.xml`, sample URLs, the @Path annotation that matched each sample URL, and all values of path parameters that Jersey received? (Edit your question to add this info.) – Daniel Trebbien Jun 01 '10 at 10:45
  • @Daniel - Thanks for your answer, I add some more information to the question. Regards! – rafa.ferreira Jun 01 '10 at 14:19

4 Answers4

21

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.

reevesy
  • 3,452
  • 1
  • 26
  • 23
jaga
  • 753
  • 11
  • 18
  • 1
    If you want to match any characters after "resources/", use the pattern `.+`, for example, `@Path("{subResources: .+}")`, as the above wont match urls with spaces or periods in the. For example "/resources/test/me.jpg" wouldn't match. – Brad Parks Sep 15 '15 at 13:10
  • and one more slight modification - if you want to ensure that & isn't included in the `subResources` part, use the `[^&?]` pattern instead of `.+` – Brad Parks Sep 22 '15 at 18:11
11

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.

paullth
  • 1,731
  • 2
  • 15
  • 19
7

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.

user306708
  • 921
  • 1
  • 9
  • 12
0

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
P. Deters
  • 815
  • 2
  • 9
  • 18