繁体   English   中英

REST如何传递空路径参数?

[英]REST how to pass empty path parameter?

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

我有2个网址:

"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??? 
  }

暂无
暂无

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

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