简体   繁体   中英

What is the best way to declare optional named path parameters in Apache Wink

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

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, ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM