From a2488e4943f5eb219c7b42ee26ff11f4d7b51b10 Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Mon, 9 Mar 2020 19:48:44 +0330 Subject: [PATCH] Bael 3653: Adding Pageable Support (#8766) * Removed the commented code * Upgrading the Spring Doc Version * Adding the Pageable Example In order to use `Pageable`, I had to add `spring-data-jpa` dep and H2. Also, the Spring doc version was quite outdated, so an upgrade for that, too. * Adding the Pageable Example In order to use `Pageable`, I had to add `spring-data-jpa` dep and H2. Also, the Spring doc version was quite outdated, so an upgrade for that, too. --- .../spring-boot-springdoc/pom.xml | 29 +++++++++---------- .../springdoc/controller/BookController.java | 7 +++++ .../springdoc/repository/BookRepository.java | 20 +++++++++---- .../src/main/resources/application.properties | 3 ++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/spring-boot-modules/spring-boot-springdoc/pom.xml b/spring-boot-modules/spring-boot-springdoc/pom.xml index 9212e56c5a..fd8a9e71ae 100644 --- a/spring-boot-modules/spring-boot-springdoc/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/pom.xml @@ -17,8 +17,8 @@ - 1.8 + 1.2.32 @@ -26,6 +26,14 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + org.springframework.boot @@ -36,24 +44,13 @@ org.springdoc - springdoc-openapi-core - 1.1.49 - - - io.github.classgraph - classgraph - - + springdoc-openapi-ui + ${springdoc.version} org.springdoc - springdoc-openapi-ui - 1.1.49 - - - io.github.classgraph - classgraph - 4.8.44 + springdoc-openapi-data-rest + ${springdoc.version} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/controller/BookController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/controller/BookController.java index 4d7d9e3d85..05f8c5a946 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/controller/BookController.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/controller/BookController.java @@ -6,6 +6,8 @@ import javax.validation.Valid; import javax.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -41,6 +43,11 @@ public class BookController { return repository.getBooks(); } + @GetMapping("/filter") + public Page filterBooks(Pageable pageable) { + return repository.getBooks(pageable); + } + @PutMapping("/{id}") @ResponseStatus(HttpStatus.OK) public Book updateBook(@PathVariable("id") final String id, @RequestBody final Book book) { diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/repository/BookRepository.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/repository/BookRepository.java index 4040ba28c2..0e3636d084 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/repository/BookRepository.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/repository/BookRepository.java @@ -1,13 +1,14 @@ package com.baeldung.springdoc.repository; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - +import com.baeldung.springdoc.model.Book; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; -import com.baeldung.springdoc.model.Book; +import java.util.*; + +import static java.util.stream.Collectors.toList; @Repository public class BookRepository { @@ -25,4 +26,11 @@ public class BookRepository { public Collection getBooks() { return books.values(); } + + public Page getBooks(Pageable pageable) { + int toSkip = pageable.getPageSize() * pageable.getPageNumber(); + List result = books.values().stream().skip(toSkip).limit(pageable.getPageSize()).collect(toList()); + + return new PageImpl<>(result, pageable, books.size()); + } } diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties b/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties index f03e90feb6..45378e610b 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties @@ -3,3 +3,6 @@ springdoc.swagger-ui.path=/swagger-ui-custom.html # custom path for api docs springdoc.api-docs.path=/api-docs + +# H2 Related Configurations +spring.datasource.url=jdbc:h2:mem:springdoc \ No newline at end of file