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.
This commit is contained in:
parent
eda766c83c
commit
a2488e4943
|
@ -17,8 +17,8 @@
|
|||
</parent>
|
||||
|
||||
<properties>
|
||||
<!-- <spring-boot.version>2.1.9.RELEASE</spring-boot.version>-->
|
||||
<java.version>1.8</java.version>
|
||||
<springdoc.version>1.2.32</springdoc.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -26,6 +26,14 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -36,24 +44,13 @@
|
|||
<!-- SpringDoc -->
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-core</artifactId>
|
||||
<version>1.1.49</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<artifactId>springdoc-openapi-ui</artifactId>
|
||||
<version>${springdoc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-ui</artifactId>
|
||||
<version>1.1.49</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
<version>4.8.44</version>
|
||||
<artifactId>springdoc-openapi-data-rest</artifactId>
|
||||
<version>${springdoc.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -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<Book> filterBooks(Pageable pageable) {
|
||||
return repository.getBooks(pageable);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Book updateBook(@PathVariable("id") final String id, @RequestBody final Book book) {
|
||||
|
|
|
@ -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<Book> getBooks() {
|
||||
return books.values();
|
||||
}
|
||||
|
||||
public Page<Book> getBooks(Pageable pageable) {
|
||||
int toSkip = pageable.getPageSize() * pageable.getPageNumber();
|
||||
List<Book> result = books.values().stream().skip(toSkip).limit(pageable.getPageSize()).collect(toList());
|
||||
|
||||
return new PageImpl<>(result, pageable, books.size());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue