【发布时间】:2018-07-05 10:13:20
【问题描述】:
我有一个带有一个参数的控制器:
@GetMapping("/people")
public Page<People> list(
@RequestParam(name="name", required = false) String name
Pageable pageable
){
Page<People> peoples=PeopleService.findByName(pageable,name);
return peoples;
}
当我转到localhost:8080/people?name=John 时,它给了我正确的数据,但是当我转到localhost:8080/people 时,它没有给我任何数据,但我希望它给我所有人。
发现是Spring引起的,还在搜索where name=null。
如何解决这个问题,因为我有更多的参数,比如年龄、日期等?
【问题讨论】:
-
为什么不使用不同的 URL 呢?例如
/people/all所有人 -
因为你还在调用
findByName方法。如果参数是null,并且您想返回所有内容,请使用findAll。为此,您将需要控制器中的一些逻辑。 -
是JPA服务吗?
-
您可能还想在“name”参数后添加一个逗号,并将“peoples”变量重命名为“people”。
标签: spring spring-data spring-rest