0

使用Apache Wink在 REST 路径中定义可选命名参数的最佳方法是什么?

现在我正在使用这样的东西:

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

用于匹配请求,例如:

/items/123
/items/
/items

这样我就可以拍干净了{id}

另一种选择是:

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

但随后{id}将包含该/字符,并且需要进行清理。

我在我的框架(µ)Micro中使用 Wink,我打算坚持使用它,推荐其他/更好的(?)类似的框架目前不会回答这个问题。

谢谢!
-弗洛林

4

1 回答 1

1

这可能有点麻烦,不知道,也许这不是比你更好的解决方案(我不知道你的要求),但这就是我的做法。我的资源类有一个注释'@Path("/db")',然后是每个受支持的目录级别的连续方法,即,因为 REST 基于 URL,这些 URL 必须将 '/' 字符视为目录分隔符。

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

处理“db/items”,以及

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

处理“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
}

处理“db/items/123/456”。

但是您可以看到这在较长的路径上很快变得很麻烦,而且我还没有弄清楚如何处理 n 深度路径(有人吗?)。希望这会有所帮助。

于 2013-06-19T12:27:06.360 回答