简体   繁体   English

在Apache Wink中声明可选命名路径参数的最佳方法是什么

[英]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 ? 使用Apache Wink在REST路径中定义可选命名参数的最佳方法是什么?

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} . 这样我就可以捕获干净的{id}

Another option would be: 另一种选择是:

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

but then the {id} will contain the / character and it will require a cleanup. 但是{id}将包含/字符,并且需要清除。

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. 我在我的(µ)Micro框架中使用Wink,并计划坚持使用它,建议其他/更好(?)相似的框架目前无法回答此问题。

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(“ / db”)',然后为每个受支持的目录级别提供连续的方法,即,由于REST基于URL,因此必须将'/'字符视为目录定界符。

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

handles "db/items", and 处理“ db / items”,并且

@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 处理“ db / items / 123”,并且

@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". 处理“ 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? ). 但是您可以看到,在较长的路径上,这很快变得很麻烦,而且我还没有弄清楚如何处理n深度路径( 有人吗? )。 Hope that's of some help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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