[英]Modify the url path for GET in the rest service
我實現了一個簡單的 rest GET 服務,我想修改該服務的 ulr。
url 現在是: http://localhost:8011/types/id?date=2019-07-30T11:35:42
我想添加一個過濾器並在日期中添加一些括號 [ ] ,
像這樣: http://localhost:8011/types/id ? 過濾[日期] =2019-07-30T11:35:42
這是我的 Get 服務,在值中我有“類型/ID”,但我不知道如何為請求的參數添加過濾器和括號。
@RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<?> getTicketids( @RequestParam(required = false) String date)
{
...
}
我會很感激任何關於我可以改變什么或我應該讀什么的建議。
我假設您使用 Spring 來定義您的 RestController。 一般來說,參數應該像這樣工作:
public ResponseEntity<?> getTicketids( @RequestParam(name = "filterDate", required = false) String date)
{
...
}
此代碼允許對http://localhost:8011/types/id?filterDate=mydate 的請求
但是, 在 URL 中不允許使用方括號,因此您可能需要重新考慮該特定方法。
為什么要在 RequestParam 中添加方括號?是因為您需要多種過濾器並想要區分它們嗎? 在這種情況下,您可以改為添加兩個請求參數 -
?filterName=date&filterValue=2019-07-30T11:35:42
此外,請確保這些值是 url 編碼的,以避免字符串的任何部分使您的 url 無效。
希望這可以幫助。
我建議您不要在 URL 中使用方括號,而是傳遞您想要的過濾器列表,如下所示:
http://localhost:8011/types/id?filters=firstValue,secondValue,thirdValue
並像這樣更改您的 controller:
@RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void receiveArrayOfValues(@RequestParam String[] filters)
{
// Handle values here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.