Home > Enterprise >  Spring Controller not receiving 'page' param from request params for pagination
Spring Controller not receiving 'page' param from request params for pagination

Time:09-01

I have a controller which has the following method

@GetMapping 
public Page<Routine> getAll(
    @RequestParam(required = false) String type,
    @RequestParam(defaultValue = "0") int page,
    @RequestParam(defaultValue = "10") int size){

    Pageable paging = PageRequest.of(page, size);
    return routineRepository.findByTypeAndSharedIsTrue(type, paging);
}

And the following repository

public interface RoutineRepository extends MongoRepository<Routine, String>{
    Page<Routine> findByTypeAndSharedIsTrue(String type, Pageable pageable);
}

When I make a request to that method: curl http://localhost:1235/api/routines?type=Calisthenics, the page parameter is set to 0 as it's its default value, after I try curl http://localhost:1235/api/routines?type=Calisthenics&page=1 though, the page parameter doesn't change and it contains its default value 0 no matter which value I give it in the request

CodePudding user response:

In my case, if you use curl in command line and you want to use & sign to append parameters, you have to use

\ &

CodePudding user response:

You are missing name attribute below are the example .

@GetMapping
    public String getAll(
            @RequestParam(name = "type",required = false) String type,
            @RequestParam(name="page",defaultValue = "0") int page,
            @RequestParam(name="size",defaultValue = "10") int size){
        return type   page  size;
    }

CodePudding user response:

You probably need to provide more information. My code that almost as same as yours works well.

@RestController
@RequestMapping("/api")
public class ApiController {

  @GetMapping("/routines")
  public Object getAll(
      @RequestParam(required = false) String type,
      @RequestParam(defaultValue = "0") int page,
      @RequestParam(defaultValue = "10") int size){

    return page;
  }
}

My request is like http://localhost:8080/api/routines?type=Calisthenics&page=2 .

And I got the correct response which is the value of page parameter I passed in.

  • Related