|
@@ -459,7 +459,8 @@ In this section we add custom handling for the exceptions we created before. A `
|
459
|
459
|
- Provide the detail and developer messages from the `ResourceNotFoundException`
|
460
|
460
|
|
461
|
461
|
```
|
462
|
|
-@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {...}
|
|
462
|
+@ExceptionHandler(ResourceNotFoundException.class)
|
|
463
|
+public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {...}
|
463
|
464
|
```
|
464
|
465
|
|
465
|
466
|
|
|
@@ -533,3 +534,69 @@ NotEmpty.poll.question=Question is a required field
|
533
|
534
|
Size.poll.options=Options must be greater than {2} and less than {1}
|
534
|
535
|
```
|
535
|
536
|
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+# Part 6 - Pagination
|
|
540
|
+* To optimize performance, it is important to limit the amount of data returned, especially in the case of a mobile client.
|
|
541
|
+* REST services have the ability to give clients access large datasets in manageable chunks, by splitting the data into discrete pages or _paging data_.
|
|
542
|
+* For this lab, we will approach this by implementing the _page number pagination pattern_.
|
|
543
|
+
|
|
544
|
+-
|
|
545
|
+### Get Data From Page
|
|
546
|
+* For example, a client wanting a blog post in page 3 of a hypothetical blog service can use a `GET` method resembling the following:
|
|
547
|
+`http://blog.example.com/posts?page=3`
|
|
548
|
+
|
|
549
|
+-
|
|
550
|
+### Limit Data Retrieved From Page
|
|
551
|
+* It is possible for the client to override the default page size by passing in a page-size parameter:
|
|
552
|
+`http://blog.example.com/posts?page=3&size=20`
|
|
553
|
+
|
|
554
|
+-
|
|
555
|
+### Pagination Data
|
|
556
|
+* Pagination-specific information includes
|
|
557
|
+ * total number of records
|
|
558
|
+ * total number of pages
|
|
559
|
+ * current page number
|
|
560
|
+ * page size
|
|
561
|
+* In the above blog-scenario, one would expect a response body with pagination information closely resembling the `JSON` object below.
|
|
562
|
+
|
|
563
|
+```JSON
|
|
564
|
+{
|
|
565
|
+"data": [
|
|
566
|
+ ... Blog Data
|
|
567
|
+ ],
|
|
568
|
+ "totalPages": 9,
|
|
569
|
+ "currentPageNumber": 2,
|
|
570
|
+ "pageSize": 10,
|
|
571
|
+ "totalRecords": 90
|
|
572
|
+}
|
|
573
|
+```
|
|
574
|
+* Read more about REST pagination in Spring by clicking [here](https://dzone.com/articles/rest-pagination-spring).
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+-
|
|
578
|
+## Part 6.1 - Load Dummy Poll Data
|
|
579
|
+
|
|
580
|
+* Create a `src/main/resource/import.sql` file with _DML statements_ for populating the database upon bootstrap. The `import.sql` should insert at least 15 polls, each with 3 or more options.
|
|
581
|
+ * Below is an example of `SQL` statements for creating a single poll with only one option.
|
|
582
|
+
|
|
583
|
+ * Poll Creation
|
|
584
|
+
|
|
585
|
+ ```sql
|
|
586
|
+ insert into poll (poll_id, question) values (1, 'What is your favorite color?');
|
|
587
|
+ ```
|
|
588
|
+ * Option Creation
|
|
589
|
+
|
|
590
|
+ ```sql
|
|
591
|
+ insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
|
|
592
|
+ ```
|
|
593
|
+
|
|
594
|
+* Restart your application.
|
|
595
|
+* Use Postman to ensure database is populated by `import.sql`.
|
|
596
|
+
|
|
597
|
+-
|
|
598
|
+## Part 6.2 - Spring's Built-in Pagination
|
|
599
|
+* Make use of Spring's built-in page number pagination support by researching `org.springframework.data.repository.PagingAndSortingRepository`.
|
|
600
|
+* Modify respective `Controller` methods to handle `Pageable` arguments.
|
|
601
|
+* Send a `GET` request to `http://localhost:8080/polls?page=0&size=2` via Postman.
|
|
602
|
+* Ensure the response is a `JSON` object with pagination-specific information.
|