簡體   English   中英

SpringBoot Rest API 獲取方法

[英]SpringBoot Rest API Get Method

I am building REST API using SpringBoot, this REST API accept more than one data from consumer like eg empId, empName, empDept. 在我當前的代碼中,我使用@RequestParam注釋從請求中訪問查詢參數值。 但我看到我們也可以使用@PathVariable從 URI 的占位符中獲取數據。

想知道使用@PathVariable @RequestParam或其他東西獲取多輸入請求的最佳實踐是什么?

想象一下。 如果您有用戶列表:

GET /users (here it lists all your users)

您可能會點擊一個用戶來訪問他的詳細信息:

GET /users/{id}

您將這個{id}用作@PathVariable

您可能希望使用@RequestParam過濾用戶:

GET /users?name=tst&age=21

並且您的 GET /users 有這些請求參數供您過濾:

@RequestParam(value ="name")
@RequestParam(value ="age")

通常,@PathVariable 用於“getByAnything”等情況,例如getUserById和一個 Url 只能有一個占位符變量(理想)。 如果您的 API 有多個變量,您的選擇是 @RequestParam。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM