0

What is the best way to define optional named parameters in a REST path using Apache Wink?

Right now I am using something like this:

/items{sep: (?)}{id: (.*)}")

for matching requests such as:

/items/123
/items/
/items

so that I can capture a clean {id}.

Another option would be:

/items{id: (/?/[^/]+?)}

but then the {id} will contain the / character and it will require a cleanup.

I am using Wink in my framework (µ)Micro and I am planning to stick with it, recommending other/better(?) similar frameworks would not answer this question at this time.

Thank you!
-florin

Florin
  • 528
  • 5
  • 14

1 Answers1

1

This might be a bit cumbersome, dunno, maybe this isn't a better solution than yours (I don't know your requirements) but here's how I did it. My resource class has an annotation of '@Path("/db")', and then successive methods for each supported directory level, i.e., since REST is based on URLs that will necessarily treat '/' characters as directory delimiters.

@Path("{id}")
@GET
public Response getJson( @PathParam("id") String id )
{  
    String path = id;
    // TODO
}

handles "db/items", and

@Path("{id1}/{id2}")
@GET
public Response getJson( 
        @PathParam("id1") String id,
        @PathParam("id2") String id2 )
{
    String path = id1 + '/' + id2;
    // TODO
}

handles "db/items/123", and

@Path("{id1}/{id2}/{id3}")
@GET
public Response getJson( 
        @PathParam("id1") String id1, 
        @PathParam("id2") String id2, 
        @PathParam("id3") String id3 )
{ 
    String path = id1 + '/' + id2 + '/' + id3;
    // TODO
}

handles "db/items/123/456".

But you can see this quickly becomes cumbersome on longer paths, and I haven't yet figured out how to handle n-depth paths (anyone?). Hope that's of some help.

Ichiro Furusato
  • 620
  • 6
  • 12
  • Thank you, Ichiro. I am really interested to see if Wink can accomodate optional path parameters. I know how to do it with Rails, [here](http://stackoverflow.com/questions/7281592/routing-with-an-optional-parameter) is a good example and I thought I can use a similar syntax with Wink. I [solved my problem](https://github.com/florinpatrascu/micro-examples/blob/master/todos/WEB-INF/config/routes.yml#L13) using regexp though I am still hoping there is a cleaner way. I'll keep the question open for now. Thanks again! – Florin Jun 20 '13 at 21:37