简体   繁体   English

春季启动:@GetMapping与Pageable作为请求参数无法正常工作

[英]Spring Boot: @GetMapping with Pageable as request parameter don't work as expected

I am using Spring Boot 2 and I have write a @RestController within a @GetMapping that takes a Pageable as parameter. 我正在使用Spring Boot 2,并且在@RestController内编写了一个@GetMapping ,该参数以Pageable作为参数。

@GetMapping
public ResponseEntity<Page<AppointmentTO>> findAll(Pageable pageable) {
    Page<AppointmentTO> page = appointmentService.findAll(pageable);
    return ResponseEntity.ok(page);
}

The problem is the following: 问题如下:

By each request, the queries-parameters pageSize and offset are always reset to default when they arrived in Spring Boot Backend ( ?offset=0&pageSize=20 ), however I send different parameters in the url of my request ( ?offset=15&pageSize=5 for example). 对于每个请求, 查询参数 pageSizeoffset在到达Spring Boot Backend( ?offset=0&pageSize=20 )时始终会重置为默认值,但是我在请求的URL中发送了不同的参数( ?offset=15&pageSize=5例如)。

Spring Boot maps the request params to org.springframework.data.domain.PageRequest that extends AbstractPageRequest Spring Boot将请求参数映射到扩展AbstractPageRequest org.springframework.data.domain.PageRequest

  AbstractPageRequest implements Pageable, Serializable {
    ...
    private final int page;
    private final int size;

    public long getOffset() {
        return (long)this.page * (long)this.size;
    }
    ...

You should use following url: 您应该使用以下网址:

http://localhost:8080?page=3&size=5

Also you could add sorting by ...&sort=name 您也可以按...&sort = name添加排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM