Pagination in Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is bashar in this tutorial we are going to implement pagination in springboot as usual you can find the codes in my github repository again we will clone this repo and we have this branches main and the final and the final branch is containing the codes we are doing this in this tutorial so let's clone the project running git clone here [Music] and opening this folder in visual studio code let's check the pom xml and again we are running springboard 2.3.4 and running java version 11 and we have the dependencies of spring data jpa spring web and we have this in-memory database h2 and the other two dependencies are for the development dev tools and the lombok and in our source we have this package of user we have this user entity it has these fields we have this user controller user service which are not implemented yet and we have this user repository so let's start with implementing a get mapping for listing all the users so in this user controller let's add the rest controller annotation and we will have a get mapping here our get requests will be handled here and our path will be api 1 0 users this endpoint will be listing all the users and we are going to return back the list of users let's say the method name is get users and we will ask user service to get the users for us so let's inject it right here [Music] this auto wired and we will call user service get users function this is not created yet but it is going to be returning back the list of users so we can just return the result coming from this function and let's create this in the user service and this is going to be a service class and we are going to get the users from database so we use the user repository [Music] we are injecting it right here and user repository is basically just an interface extending the jpa repository and it is coming with the default functions like the getting users by id or getting all users so we are going to use this user repository and here we have this final method which is returning back the list of users so we are going to just call this one saving all these changes and running the application so the application is started now we are going to send our request with postman we will send the get request to this user's endpoint let's send this request and here we are receiving a 200 okay having an empty array now let's add users to our application and we will do it by creating our command line runner instance here in this main class we can do that here we are going to create a command line runner let's call this method as run [Music] and to make this instance to be part of the spring context we are going to add the beam annotation right here so spring will be calling this function this run method and in the end it's going to be returning back this command line runner and when spring sees an implementation of command line runner it's going to be running the code in it so we are going to return a new command line runner and this is an interface we are going to implement the method of this run method and in this method we are going to add users to our application we can create multiple users by going with the traditional for loop or we can use int stream and we can range from let's say one two to thirty for each of them we will get the iterated number and in this method we are going to create a user [Music] so this user will have a username and let's say it is user and the index and it will have the email again it is going to be something like this user index at mail.com and we can set the password like this and then we are going to save each of these users the database so we need user repository to be injected right here [Music] so when spring is running this function it is going to be providing us the user repository and in the end we are going to be returning an instance of this command line runner and in this method we are going to create 30 users so here we will call you the repository save with the user instance so that's it and alternatively we can convert this and this implementation to lambda function like this so saving this one and going back to postman and sending a request once again and here we are seeing the users here we have all the users listed here now let's think about like this we have like thousands of users [Music] let's try it again and here we have this amount of payload and the users are listed right here and as you can guess in applications like the twitter or the others there are billions of users so if we would have an endpoint like this we cannot be providing the content in an array we must be providing the content by page nation so user must be navigating page by page to browse the content now let's change this user count to 30 once again and we will be implementing the page nation and this amount of users are enough to demonstrate how we can navigate between pages so the request must be having data about the page and size like we must be providing the request parameters like let's say we would be setting the page size and page number like this we want each page to have 10 items and we want to start from the first page the indexed 0. so we would be sending requests like that and we would be receiving the response accordingly for now if we send it like this nothing changes we are still receiving all the users listed in this array now let's implement this functionality on our controller so we will take page size and page number so here in this user controller we are going to get the request parameters via this request param annotation and the values are this page size which is integer and the other one is page number and that's also an integer so we will get an integer of page size this name is identical with this parameter right here and the other one is again request param integer page number then we will pass these values like this page number and page size to this get users method in user service let's add these parameters now we are going to use these parameters in our query but first let's check what are the alternatives provided through this user repository here we have this user repository is having find all methods which are taking different parameters like this one is taking an example object this one is taking a pageable and this is returning back us the page response so this is what we are looking for so we will call this find all now we need to provide a pageable object to it and we can create an instance right here pageable [Music] and that is the instance coming from this one spring framework data domain pageable and let's say this is page and we will create a pageable instance with this page request this has is off factory method and we will pass the page number and page size so this is going to be page number and the next one is page size so we have this pageable instance and we will pass it to this one and this is returning back the page object so we don't need this find all anymore and since this is returning page we need to update our method return type right here we have to return a page back and since we updated this method return type we have to update this controller part also so this is going to be returning back a page object now let's save these changes and sending request again with postman now we have a different response this is an object and we have this content array and we have information about the the total elements how many pages are there and what is the size of the current page like that and in the content we have these users from 1 to 10 so we limited our response to receive just 10 users and we can play with these numbers like we can say we want three users to be existed in each response and here we have three users and we can navigate the next page by increasing this page number and we got the user four five six and if you go like this we will be getting the other users our parameters are limiting the results that we are looking for and on the other hand the spring data is handling the the remaining calculations like how many pages are there like the total page count and what is the current page if we are on the last page or first page generated by spring data but now we are passing the user inputs like this one the page size and the page number into our query so we have to make sure these values are valid and in the limits so let's start with this one let's say if user does not send this page number at all what happens so here we are receiving this bad request this is saying the missing servlet request parameter exception this is strong because this page number is not present in the request so we can fix it like this we can say the page number we we can use this request perm configurations and we can say this is not required by setting it to false and also we can add the other option if there is no this page number then let's assign a default value to it and this default value parameter is looking for a string value but the page number is integer so we will provide the let's say by default the page number will be zero so we pass the default value as zero but it's going to be in this string format so let's save this one and let's send the request again and here we are seeing the request is properly handled but the existence of the parameter is not the only control point a user may be entering values like at like invalid values which is lower than zero so maybe user is requesting a value of -3 page number -3 and this is also triggering the exception and also we have the potential let's say we might have the millions of users and the user may be setting the page sizes million so our page nation would be meaningless for that case so we have to validate these parameters and we have to set some boundaries for them before running our query in the database so for solution instead of processing these parameters like this way we can use spring functionality and we can tell spring that provide us the pageable so spring is going to be taking those request parameters and it is going to do the validation necessary validations in those values and it would be handling the default values of those parameters for us and in the end we would be receiving just the proper page request object in our controller and we can pass it to our service method and in that service method we can change this one to take pageable and we no longer need to create our own page instance [Music] so let's save all these changes now let's go back to postman let's send the valid request first [Music] so we are sending a request of having page size 3 page number as 0 but when we check the content length here we have 20 users so it is not carrying what we are setting right here and if we change the page number again it is not going to the next page because this page size and the page number are our custom request parameter names and we can make these parameter names to be working with spring springs this pageable object by setting the configuration parameters we have this spring data web page bull and we have this page parameter and for this one we can set it to page number and we have the other web pageable [Music] size parameter and for this one we can say page size so saving this one and sending the request once again and here we are in page one the it's it is starting from user four five and six so the page is limited to three users and we have total ten pages so this is properly working and if we go like the negative numbers [Music] it is properly working it is assuming the page is zero by default if it is not a valid entry or if we say the page size is like this minus three it is assuming the page size is 20 and returns to 20 users and again it has the upper limits let's say we want 1 million users to be returned in the response so we we we don't have that much user in our application but here in the here in this part we see the size is limited to 2000 so even we would be requesting this much for the page size in the end we would be getting only 2 000 items in the response and this pageable is not only supporting this page and size parameters but also it has sort parameters too let's change this to let's say three back and we can add them sort and the sort is taking two values one of them is the which field we are going to sort and it is let's say we will sort the result based on the id and adding a comma and this second parameter is the the order direction of this sort and for the direction currently it is ascending from lower value to hires but we want let's say in reverse order so we said descending disk sending the request again and here we have the this is the first page the first user 30 29 28 and if you go to the next page we get 27 and it's going in descending order so this pageable is providing more capability than we were trying to achieve with our custom implementation and also by default this page and size has default values so if we go back go to this this properties class we can see here by default this page parameter is page size parameter is size so we can just get rid of these custom values and we can convert this to let's say this was size and this is page and sending the request and here it is working again in properties we can override the default values like in this properties we have this page size and we can set the index the page index to be starting from one instead of zero by setting this one index parameters to true so currently if we go one if you want to go to the first page we set the page as zero but if you don't like this one if you want to go to first page by setting pages one all we need to do is just set this one index parameters to true right here one indexed parameters to true like this and for the others like this default page size or max page size we can override the the defaults let's say we want default page size to be just 10. default page size not the this the spring data rest default page but this one the spring data web pageable default page size and here it is saying the default is 20 so we are setting it to let's say 10. and in postman if we just remove these parameters and send our request it is going to be returning back the response with the defaults so the page will be first page and the page size will be the one we set in the properties so it's going to be returning 10 users back so let's check the response and here we have 10 users right here and it's saying the total pages is 3 because we have 30 users but this properties is globally applied so if we have different controllers having this pageable those page size would be also having the default size s10 but we can also overwrite those defaults for individually any endpoint let's say we have another get mapping right here and let's say this one is for let's say ordered users let's say get ordered users in this one we are not going to provide any parameter in any request parameter but we want the users to be sorted based on the id in descending direction and we also want the page size to be like let's say five and we do that by adding another annotation right before this pageable which is pageable default and it has the options of let's say the first one is the sort and we want the users to be sorted based on the id and the other one is the direction and we wanted the direction to be in descending and also we want size to be five so that's how we can overwrite the defaults right at this controller method level so saving this one and going to postman again this time i'm going to send the request to ordered users and here we are receiving the content ordered based on the id in descending and we are receiving only five users and here it is saying there are six pages and we haven't provided any parameter but of course we can change the page size like this one let's say we want 30 users and here it is returning all the users back [Music] that's all for the pagination thanks for watching hope to see you in the next tutorials
Info
Channel: Programming with Basar
Views: 13,311
Rating: undefined out of 5
Keywords:
Id: rn-q5AmVUt4
Channel Id: undefined
Length: 26min 6sec (1566 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.