Alex Rivera | Logout

@Path and regular expression (Jersey/REST)

Asked 2010-05-31T21:02:03.280
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>
(...)

                    
                    
                    
Edit
Report

1 Answer

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.

answered 2010-10-08T08:52:36.283

Your Answer