REST如何传递空路径参数?

我正在使用Netbean 7.1.1 Glassfish 3.1.2构建REST Web应用程序

我有2个url:

 "http://myPage/resource/getall/name" (get some data by name) "http://myPage/resource/getall" (get all data) 

当客户端使用第一个URL发送请求时,将调用下面的servlet并执行一些处理。

 @Path("getall/{name}") @GET @Produces("application/json") public Object Getall(@PathParam("name") String customerName) { //here I want to call SQL if customerName is not null. is it possible??? } 

但我也想要第二个URL来调用这个servlet。

我以为servlet会被调用,我可以检查customerName == null然后调用不同的SQL等等。

但是当客户端使用第二个URL(即没有路径参数)发送请求时,不会调用servlet,因为URL没有{name}路径参数。

是不是可以调用第二个URL并调用上面的servlet?

我能想到的另一种选择是使用query parameter

 http://myPage/resource/getall?name=value 

也许我可以解析它,看看"value"是否为null然后采取相应的行动..

您可以为Path Parameter指定正则表达式(请参阅2.1.1。@ Path)。

如果你使用.*匹配空名和非空名所以如果你写:

 @GET @Path("getall/{name: .*}") @Produces("application/json") public Object Getall(@PathParam("name") String customerName) { //here I want to call SQL if customerName is not null. is it possible??? } 

它将匹配“http:// myPage / resource / getall”和“http:// myPage / resource / getall / name”。

 @GET @Path("getall{name:(/[^/]+?)?}") @Produces("application/json") public Object Getall(@PathParam("name") String customerName) { //here I want to call SQL if customerName is not null. is it possible??? }