Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

java - PageRequest above page 1 returning the same result

I have the following get function which should return trips inside of a page with optional specifications.

@GetMapping("/trip")
    Page<Trip> all(@SearchSpec Specification<Trip> specs,
                   @RequestParam(defaultValue = "0") int page,
                   @RequestParam(defaultValue = "1000") int pageSize) {
        //return repository.findAll(Specification.where(specs));
        return repository.findAll(Specification.where(specs), PageRequest.of(page, pageSize, Sort.by("searchKey").ascending()));
    }

Now when I make the following request to my API:
http://localhost:8080/trip?page=0&pageSize=2&search=departureAirport:'AMS'

I get back 2 records(trips) from my API which is correct. When I change the request to:
http://localhost:8080/trip?page=1&pageSize=2&search=departureAirport:'AMS'

So to the next page, I get the next 2 records(trips) from my API which is also correct, but as soon as I want to go to page 2 the API returns the same records as on page 1. So every page above page 1 returns the same records.

I am able to increase the pageSize and when I do this the API returns more records, but again every page above 1 returns the same. So only the data on page 0 and 1 is different.

I tried removing the Specification and the sorting but this didn't fix the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ended up using my own paging algorithm since the problem was probably related to Sqlite and I didn't want to change my database client. I ended up using the following code in my Get request:

List<Trip> trips = repository.findAll(Specification.where(specs));
        if (sort.equals("desc")) trips.sort(Collections.reverseOrder());
        if(page > 0) {
            if ( page == 1) {
                trips = trips.subList(0, pageSize);
            } else {
                trips = trips.subList(((page - 1) * pageSize), page * pageSize);
            }
        }
        return trips;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...