在Pageable

时间:2018-11-19 13:59:55

标签: java spring rest controller

我正在使用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中,我得到了例如“ url?name = erick&direction = asc”,但我需要更改为“ url?name = erick!asc”

我该如何更改?

2 个答案:

答案 0 :(得分:0)

您可以执行此操作。请看https://www.ietf.org/rfc/rfc1738.txt

的第3页

答案 1 :(得分:0)

在这种情况下,应使用@RequestParam(“ name”)而不是@PathVariable。然后,请求url将类似于“ url?name = erick&direction = asc”

Spring具有三种注释。

  • @PathVariable

此注释表示变量位于网址上。例如:

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

该变量放在网址的花括号之间。

  • @RequestParam

此注释表示变量是搜索参数的一部分,请求网址类似于

  

stackoverflow.com?name=hhhh

例如:

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

此批注意味着您将从请求正文中接收到一些数据,并且某些转换器(例如jackson)会将其转换为适当的对象。例如:

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