簡體   English   中英

自定義Pageable中REST參數的路徑

[英]Customize Path to REST params in Pageable

我正在使用spring使用PageAble構建REST api,以獲取numberofPages,itens ...

首先,我做了這樣的映射

  public ResponseEntity<Data> findByName(@PathVariable(value="name",required=true) String name, @RequestParam(value="page", defaultValue="0") Integer page, @RequestParam(value="qtd", defaultValue="10") Integer linesPerPage, @RequestParam(value="sort", defaultValue="nome") String sort, @RequestParam(value="direction", defaultValue="ASC") String direction)

因此,在我的網址中,我得到了例如“ url?name = erick&direction = asc”,但我需要更改為“ url?name = erick!asc”

我該如何更改?

你可以這樣做。 請參閱https://www.ietf.org/rfc/rfc1738.txt的第3頁

在這種情況下,應使用@RequestParam(“ name”)而不是@PathVariable。然后,請求url應類似於“ url?name = erick&direction = asc”

Spring具有三種注釋。

  • @PathVariable

此注釋表示變量位於url上,例如:

@RequestMapping("/{id}")
public void pathVariable(@PathVariable("id") Long id){}

將該變量放在網址的花括號之間。

  • @RequestParam

這個注釋意味着變量是quest參數的一部分,請求url看起來像

stackoverflow.com?name=hhhh

例如:

@RequestMapping("/")
public void requestParam(@RequestParam("id")Long id){}
  • @RequestBody

這個注釋意味着您將從請求主體接收一些數據,並且某種轉換器(例如jackson)會將其轉換為適當的對象,例如:

@PostMapping("/")
public void requestBody(@RequestBody Example example){}

暫無
暫無

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

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