Paging7:12 with Craig Dennis
Let's learn what to do when you have the awesome problem of too much data. We'll also explore custom search queries.
Okay, so we have a good problem, right? 0:00 We have way too much data. 0:02 Now a typical solution to this is that I introduced paged results and 0:04 guess what, it's super easy to do. 0:08 So let's pop over to the CourseRepository. 0:11 Now remember this is what's generating our controllers, right. 0:14 So let's pop over here, and if we simply change this from 0:16 CrudRepository to PagingandSortingRepository. 0:21 And then, let's also do that for reviews while we're in here because we're probably 0:28 gonna have a lot of these too, at least hopefully, right? 0:31 And if we swing down here and we reboot our server, I'm gonna ahead and 0:33 click this Reboot button. 0:38 Let's let this go. 0:44 Awesome, so now if we flip over back to our courses page, if I refresh this, if 0:47 we scroll down, we will see that there is a lot of new things here at the bottom. 0:52 Namely first, right? 0:58 So this is the first page and there's only 20. 1:00 So right, so there's only 20 on this page. 1:02 It still kind of seems like a lot but there's only 20 and 1:04 it shows the next page which is page 1 and it shows the last page. 1:06 Pretty cool, right? 1:11 Now let's think about this for a minute, what if the users of our API, 1:12 like our other teams, what if they were trying to add a feature to find all 1:15 the courses with a certain word in it? 1:19 I guess what they'd have to do now right is search through each of these pages. 1:21 That'd be a bummer, right? 1:24 Well again, no surprise. 1:26 This can just happen. 1:28 Okay, so let me show you. 1:30 So, if you head over back to your course repository, 1:31 we can just add a new query here, right? 1:35 And if we use the standard naming conventions, 1:37 our query will just be exposed. 1:41 So what do we say we wanted? 1:43 Right. 1:45 Let's say that we wanted to find all the courses with a certain word in 1:46 their title. 1:49 So to do that, what we'll do is we'll add a method that returns a page of courses. 1:50 And the proper naming thing, we're gonna say, findByTitleContaining. 1:57 You probably remember this from spring data, findByTitle containing, right. 2:03 And we're gonna pass in the parameter of title, 2:08 parameter of a named query param there. 2:12 And it's going to be the title. 2:15 And then finally we're gonna pass in an iterator. 2:17 It will get an interior. 2:22 Expect an iterator. 2:26 Okay. 2:28 Give us some more space there. 2:30 There we go. So, 2:32 again a special name findByTitleContaining, 2:32 title being the field. 2:35 And we're gonna say, call it title when it comes across. 2:36 Watch this. 2:40 So, we're gonna restart and refresh the course. 2:41 Pop up over here. If we take a look and 2:50 we go again down all the way to the bottom, we'll see here that there's this 2:51 new search item that's available, so that says that we can search on courses. 2:56 So let's bring that up and 3:00 again it's just added that automatically because we have something there. 3:02 And now there's a thing called findByTitleContaining and 3:05 it shows you what you can do here, what parameters it takes. 3:09 This is a templated, right, so this is a templated search. 3:12 So we're gonna go ahead and let's do this, let's say findByTitleContaining. 3:14 And the query parameter that we're gonna pass is title and we want it to start with 3:21 something, whatever, let's say, Java lets see what, what comes up. 3:26 Here is all of the Java reports, look at that? 3:29 Looks like our database loader has the same number of things, I'm assuming. 3:40 So let's add one more on here. 3:47 And let's say we wanna learn about spring HATEOAS. 3:50 So let's refresh this. 3:57 That will happen if you're generating data and they have the same. 4:00 So the trick that kind of isn't working for the the different modelos there. 4:02 So we rebooted this. 4:06 Let's go ahead and look again for everything containing java. 4:07 There we go. 4:11 See that the titles are different now. 4:12 Java 9, different course numbers. 4:13 There we go. 4:16 And also look, it's still paged, right? 4:19 So we know that there's only 18 on this. 4:22 There's only 1 page. 4:24 But isn't that cool? 4:26 It's still paged. 4:27 Search results, so what if we didn't like this findByTitleContaining? 4:28 What if that seems a little bit awkward to use in a REST world? 4:32 So one thing that you can do, and you can do this actually with just about anything 4:35 that gets generated over here, is we can change what this is displayed as. 4:41 So if we use the annotation RestResource, we can say that the rel, you 4:46 know this is what the the link is called in the search there, that's available. 4:52 Since we're on a search here. 4:57 I'm sorry, back here in the search page here. 5:00 We'll see that this is the REL here, findByTitleContaining. 5:05 So we can change that to be title-contains. 5:08 And we can also even change the path to it. 5:11 So let's make that read, let's say instead of find, 5:14 let's have it be called containsTitle. 5:17 That should be clear, right? 5:19 So again, if we refresh 5:22 Come over here, you'll see that it's now called title-contains and is looking for 5:31 contains-title as the link. 5:35 Pretty cool, right? 5:36 For more on these powerful search queries, see the teacher's notes. 5:37 Now, side note here too is that this rest resource little name that we did, 5:40 on the course repository. 5:45 We can also change this up here. 5:47 So you could change what the default pluralization of the route. 5:49 Now you can also change the global paging size with a simple configuration change. 5:53 Let me show you this. 5:57 It's a pretty common stack overflow request. 5:58 So first remember that spring boot looks specifically for 6:00 property files in the class path. 6:03 So if we come over here and we make a new file In the resources root new file and 6:06 we call it application.properties. 6:13 We can set different configuration things. 6:18 So let's say that we find that our team wants to only show five of these 6:20 courses on a page at a time or reviews too and 6:23 they keep specifying a different size in the URL, we don't wanna make them 6:26 do that what we really wanna always have the default page size to be 5, right? 6:30 So you do spring.data.rest.default-page-size. 6:34 And now, by default it was 20, right? 6:41 We were seeing 20 on the page. 6:43 What if we say 5? 6:44 So we'll just do equals 5, and then if we come back over to our 6:45 All right, PI over here. 6:55 If you just look at courses, you'll see that there are only 5. 6:57 And now it shows that the last page is page 20. 7:03 There's 20 pages of fun. 7:05 Awesome, right? 7:07 We'll go over a few more of the options that you can tweak in here and 7:08 I've included the link in the teacher's notes. 7:10
You need to sign up for Treehouse in order to download course files.Sign up