何時使用 @QueryParam 與 @PathParam - When to use @QueryParam vs @PathParam

問題:

I am not asking the question that is already asked here: What is the difference between @PathParam and @QueryParam我不是在問這裏已經問過的問題: @PathParam 和 @QueryParam 有什麼區別

This is a "best practices" or convention question.這是一個“最佳實踐”或約定問題。

When would you use @PathParam vs @QueryParam .你什麼時候使用@PathParam@QueryParam

What I can think of that the decision might be using the two to differentiate the information pattern.我能想到的決定可能是使用兩者來區分信息模式。 Let me illustrate below my LTPO - less than perfect observation.讓我在下面說明我的 LTPO - 不夠完美的觀察。

PathParam use could be reserved for information category, which would fall nicely into a branch of an information tree. PathParam 可以保留用於信息類別,它可以很好地落入信息樹的一個分支中。 PathParam could be used to drill down to entity class hierarchy. PathParam 可用於深入到實體類層次結構。

Whereas, QueryParam could be reserved for specifying attributes to locate the instance of a class.而 QueryParam 可以保留用於指定屬性以定位類的實例。

For example,例如,

  • /Vehicle/Car?registration=123
  • /House/Colonial?region=newengland

/category?instance

@GET
@Path("/employee/{dept}")
Patient getEmployee(@PathParam("dept")Long dept, @QueryParam("id")Long id) ;

vs /category/instance/category/instance

@GET
@Path("/employee/{dept}/{id}")
Patient getEmployee(@PathParam("dept")Long dept, @PathParam("id")Long id) ;

vs ?category+instance?category+instance

@GET
@Path("/employee")
Patient getEmployee(@QueryParam("dept")Long dept, @QueryParam("id")Long id) ;

I don't think there is a standard convention of doing it.我不認爲有這樣做的標準約定。 Is there?有沒有? However, I would like to hear of how people use PathParam vs QueryParam to differentiate their information like I exemplified above.但是,我想聽聽人們如何使用 PathParam 與 QueryParam 來區分他們的信息,就像我上面舉例說明的那樣。 I would also love to hear the reason behind the practice.我也很想聽聽這種做法背後的原因。


解決方案:

參考一: https://en.stackoom.com/question/mTGa
參考二: https://stackoom.com/question/mTGa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章