[英]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.