Library6: API Rest Book CRUD with Postman Swagger and Pageable - AlbertProfe/cifojava2022-3 GitHub Wiki

Welcome to the cifojava2022-3 wiki!

Spring Boot Projects: Library6

Base project

  • Base project:

    • @Entity book and List<Book>
    • POM
    • @RestController
  • To work from base-project

    • DataBase H2: Library2
    • Application.properties
    • Command Line Runner with methods to test (Java Faker)
    • @CrudRepository JPA 2.0, @Component
    • @Test JUnit Jupiter

New tools

  • Seurity:

    • Inject from @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);
       }          
      

      Crud vs PagingAndSorting

  • css to paginate: w3 css pagination

  • test th: Thymeleaf Testing Library

Versions

  • version 1.0 : basic project from library1 with Pageable

    • http://localhost:8080/api/books?size=2 pagination [page=0, size=2]
    • /api/books sort by [id, descending] (default) & pagination [page=0, size=20] (default)
    • /api/books?sort=title,asc sort by [title, ascending] & pagination [page=0, size=20] (default)
    • /api/books?sort=author,desc&sort=title,asc order 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,asc order by column [author, descending], then order by column [title, ascending] & pagination [page=1, size=5]

    Images PagingAndSorting

  • version 2.0 : basic project from library1 with Pageable and 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";
      

      }

Pagination HTML TH

  • 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>&nbsp;
              </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>
    
⚠️ **GitHub.com Fallback** ⚠️