0

I have a rest controller that takes pageable as a parameter

  @GetMapping(value = "/user", produces = APPLICATION_JSON_VALUE,
      consumes = APPLICATION_JSON_VALUE)
  public BaseResponse getUsers(
      Pageable pageRequest) {
      System.out.println(pageRequest);
    );
  }

now if i hit the url http://localhost:8080/user?page=1&size=20&sort=userId i can see the syso getting printed as : Page request [number: 1, size 20, sort: userId: ASC]

but if i change the controller method and make pageable as required = false

@RequestParam(required= false)Pageable pageRequest

and if i hit the same url this time my syso will print null. I want to make pageable object as not required. How to achieve it

Basically my requirement is if the user triggers the url like the below

http://localhost:8080/user?page=1&size=20&sort=userId

and when i print my Pageable object it should print Page request [number: 1, size 20, sort: userId: ASC]

but if i trigger my url like this http://localhost:8080/user then my pageable object should be printed as null and not some default values

Abhishek
  • 650
  • 1
  • 8
  • 31
  • What makes you think the pageable object is required? ALso using `@RequestParam` is changing your controller method, currently it is used as a model attribute (or when using it properly with a dedicated converter, whichi is automatically registered). So it isn't required, if there are no properties in the request a pageable with default values will be created. – M. Deinum Jan 12 '22 at 09:17
  • @M.Deinum let say we don't pass any parameters like page,size,sort in postman we still have default pageable parameters printing. – Abhishek Jan 12 '22 at 09:19
  • Correct, as I stated that is the behavior you get. – M. Deinum Jan 12 '22 at 09:22
  • @M.Deinum but i want to make sure that Pageable is not null how to do this In my request i can either get page,size,sort or i might not get. If i get page,size,sort from request Pageable object needs to get populated from request. If not pageable should be null – Abhishek Jan 12 '22 at 09:27
  • It won't it will always have values as that is what the special converter does. You can add the parameters as mapping information (using the `params` in request maping) and construct a second one that doesn't provide a `Pageable`. You then thave 2 methods one with and one without pageable. – M. Deinum Jan 12 '22 at 09:41
  • @M.Deinum can you look into my final requirement which i have edited at last. is it possible to achieve this. because when i generate my swagger yaml file the swagger yaml file gives this Paged object as required=true when my ui is passing the request to backend. first thing it check what is my pageable object in my swagger file. in this case it is required. so my ui will give an error because it does not have any pageable object to be passed as a paremeter – Abhishek Jan 12 '22 at 09:56
  • It isn't required from a spring perspective, not sure why swagger thinks it is. I would say that is a swagger issue not a spring issue. But as stated write 2 methods with different mappings (one with and one without parameters). Or map to a `Pageable` yourself and include 3 parameters in your method signature and make those individual ones optional. – M. Deinum Jan 12 '22 at 10:24

0 Answers0