Hi


On Tue, Jul 5, 2011 at 8:28 PM, KARR, DAVID (ATTSI) <[email protected]> wrote:
>> -----Original Message-----
>> From: AgentSmith [mailto:[email protected]]
>> Sent: Tuesday, July 05, 2011 11:36 AM
>> To: [email protected]
>> Subject: JAX-RS variable length parameters
>>
>> How do I define @Path to except variable length parameters as part of
>> my
>> end-point. For example, I would like to be able to make the following
>> requests for the same end-points:
>>
>> http://whatever.com/MyWS/mywebservices/foo  - works
>> http://whatever.com/MyWS/mywebservices/foo/1 - 404 requested resource
>> () is
>> not available
>> http://whatever.com/MyWS/mywebservices/foo/1/2  - 404 requested
>> resource ()
>> is not available
>> http://whatever.com/MyWS/mywebservices/foo/1/2/3  - 404 requested
>> resource
>> () is not available
>> http://whatever.com/MyWS/mywebservices/foo?param1=10 - works
>> http://whatever.com/MyWS/mywebservices/foo/1/2?param1=20&param2=5 - 404
>> requested resource () is not available
>> etc
>>
>> ----------------------------------
>> @Path("foo")
>> @GET
>> public String foo(@Context UriInfo ui){
>>   MultivaluedMap&lt;String, String&gt; queryParams =
>> ui.getQueryParameters();
>>   MultivaluedMap&lt;String, String&gt; pathParams =
>> ui.getPathParameters();
>>
>>   return "foo(): " +  queryParams.size() + ", " + pathParams.size();
>> }
>
> Your first problem is getting the Path expression to match your URI.  As far 
> as I know, the only practical way to do this is to specify a "catch-all 
> expression" in the URI.
>
> For instance, the following:
>
>  @Path("{id:.*}")

Yes, and @Path("/foo/{id:.*}")

should also work, should match
/foo, foo/1, etc

>
> Will provide the "id" parameter with the entire path info.  The drawback is 
> that you may have to manually parse the data you have.
>
Yes, with the regular expression.
Subresources may help too, ex:

public Resource {

@Path("foo")
Resource foo() {
return this;
}

@Path("{id}")
Resource id(@PathParam("id") int id) {
return this;
}

@GET
@Path("{id}")
Resource lastId(@PathParam("id") int id) {
return this;
}

}

foo() method is more specific so it will be preferred, lastId() will
be matched only when the last segment is remaining and id() should be
matched in between

Cheers, Sergey

>



-- 
Sergey Beryozkin

http://sberyozkin.blogspot.com
Talend - http://www.talend.com

Reply via email to