Library6: API Rest Book CRUD with Postman Swagger and Pageable - AlbertProfe/cifojava2022-3 GitHub Wiki
Welcome to the cifojava2022-3 wiki!
-
Base project:
- @Entity
bookandList<Book> - POM
- @RestController
- @Entity
-
To work from base-project
- DataBase H2:
Library2-
First-time CREATE DDL : First-time CREATE DDL option (after that
UPDATE) inapplication.properties
-
First-time CREATE DDL : First-time CREATE DDL option (after that
- Application.properties
- Command Line Runner with methods to test (
Java Faker) - @CrudRepository JPA 2.0, @Component
- @Test JUnit Jupiter
- DataBase H2:
-
Seurity:
-
Injectfrom@Value{$(password)}into java class, DynamoDB example: linlk - Vault is a secrets management system allowing you to store sensitive data which is encrypted at rest. It’s ideal to store sensitive configuration details such as passwords, encryption keys, API keys.
- Encryption with
java BCrypt: link, project - Environment variables Heroku: link
-
-
Interface PagingAndSortingRepository<T,ID>: class-
CrudRepository mainly provides CRUD functions, if any doubt, use this @Repository
-
PagingAndSortingRepository provides methods to do pagination and sorting records (extends CrudRepository).
-
JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.
@Repository //public interface BookRepository extends CrudRepository <Book, Long> { public interface BookRepository extends PagingAndSortingRepository<Book, Long> { } //CRUD: read @GetMapping("books") public Page bookPageable(Pageable pageable) { return bookRepository.findAll(pageable); }
-
-
css to paginate: w3 css pagination
-
test th: Thymeleaf Testing Library
-
version 1.0 : basic project from
library1withPageable-
http://localhost:8080/api/books?size=2pagination [page=0, size=2] -
/api/bookssort by [id, descending] (default) & pagination [page=0, size=20] (default) -
/api/books?sort=title,ascsort by [title, ascending] & pagination [page=0, size=20] (default) -
/api/books?sort=author,desc&sort=title,ascorder by column [author, descending], then order by column [title, ascending] & pagination [page=0, size=20] (default) -
/api/books?page=1&size=5&sort=author,desc&sort=title,ascorder by column [author, descending], then order by column [title, ascending] & pagination [page=1, size=5]
-
-
version 2.0 : basic project from
library1withPageableand Thymeleaf, @Service and @Controller for TH html :-
@Service
findPaginated(int pageNo, int pageSize)public Page<Book> findPaginated(int pageNo, int pageSize) { Pageable pageable = PageRequest.of(pageNo - 1, pageSize); return bookRepository.findAll(pageable); } -
@Controller
booksPaginated(@PathVariable(value = "pageNo")@GetMapping("/page/{pageNo}") public String booksPaginated(@PathVariable(value = "pageNo") int pageNo, Model model) { int pageSize = 10; Page<Book> page = bookService.findPaginated(pageNo, pageSize); List<Book> listBooks = page.getContent(); model.addAttribute("currentPage", pageNo); model.addAttribute("totalPages", page.getTotalPages()); model.addAttribute("totalItems", page.getTotalElements()); model.addAttribute("listBooks", listBooks); return "books";}
-
-
version 2.1 and 2.2 : minor changes in TH: and security
<div th:if="${totalPages > 1}"> <!-- qty total books --> <div> Total Rows: [[${totalItems}]] </div> <!-- render all LINKS TO Controller --> <!-- we wil not render the LINK of the page we are --> <div> <span th:each="i: ${#numbers.sequence(1, totalPages)}"> <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}}">[[${i}]]</a> <span th:unless="${currentPage != i}">[[${i}]]</span> </span> </div> <!-- link to PREVIOUS PAGE --> <div> <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage - 1}}">Previous</a> <span th:unless="${currentPage < totalPages}">Previous</span> </div> <!-- link to NEXT PAGE --> <div> <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}}">Next</a> <span th:unless="${currentPage < totalPages}">Next</span> </div> <!-- link to LAST PAGE --> <div> <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}}">Last</a> <span th:unless="${currentPage < totalPages}">Last</span> </div> <!-- link to FIRST PAGE --> <div> <a th:href="@{'/page/' + 1}">First Page</a> </div> </div>-
Heroku Config Vars, Configuration and Config Vars : a single app always runs in multiple environments, including at least on your development machine and in production on Heroku. An open-source app might be deployed to hundreds of different environments.
