Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-1380

Enable Simpler APIs using Pageable and Sort

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None

      Description

      I'd like to open a discussion about simplifying the use of Pageable API's using the Pageable (and PageableHandlerMethodArgumentResolver).

      Currently the PageableHandlerMethodArgumentResolver will only provide a Pageable#isPaged Object when either the page or pageSize Parameter are set. But if a sort Parameter was specified without the use of those Parameter, you will end up with a Pageable.unpaged() ommitting the sort.

      I know you can also inject a Sort into the Controller, which works fine. But then you'd need to decide whether you use the Pageable or Sort to pass on (see below).

      So I would propose something like the following:

      1. Support Sort in Unpaged, like isPaged and isSorted
      2. Support the parsing of Sort in PageableHandlerMethodArgumentResolver when there is no page info (page, size) but a sort
      3. Change all usages of Pageable#isPaged to account for Pageable#isSorted, like here https://github.com/spring-projects/spring-data-jpa/blob/476baa480912f5e239905da0172fae1faba1d902/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java#L352

       

      If this could be implemented, we could write a simple Controller and account for all possible combinations of page, size and sort missing or provided. This would be the usage example:

      @GetMapping("/people")
      public ResponseEntity<?> people(Pageable pageRequest) {
          return ResponseEntity.ok(
                  personRepository.findAll(pageRequest)
          );
      }
      

      This is the current example you'd have to use.

      @GetMapping("/people")
      public ResponseEntity<?> people(Pageable pageRequest, Sort sort) {
          Page<Person> people;
          if(pageRequest.isPaged()){
              people = personRepository.findAll(pageRequest);
          }else{
              people = personRepository.findAll(sort);
          }
          return ResponseEntity.ok(people);
      }
      

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              menostos menostos
              Last updater:
              Jens Schauder
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: