To document a Spring Pageable interface in Swagger, you typically need to describe the query parameters that are accepted by the endpoint. Here's how you can document Pageable in Swagger using annotations with Spring Boot and Springfox (Swagger):
Assume you have a Spring MVC controller method that accepts Pageable as a parameter:
import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class UserController { @GetMapping("/users") public Page<User> getUsers(Pageable pageable) { // Example implementation fetching users using Spring Data JPA return userRepository.findAll(pageable); } } Make sure you have Swagger (Springfox) dependencies configured in your pom.xml (if using Maven) or build.gradle (if using Gradle).
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2'
PageableAdd Swagger annotations to document Pageable in your controller method:
import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.swagger2.annotations.EnableSwagger2; @RestController @RequestMapping("/api") @EnableSwagger2 public class UserController { @GetMapping("/users") @ApiOperation(value = "Get users with pagination") @ApiImplicitParams({ @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query", value = "Results page you want to retrieve (0..N)"), @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query", value = "Number of records per page."), @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "Sorting criteria in the format: property(,asc|desc). " + "Default sort order is ascending. " + "Multiple sort criteria are supported.") }) public Page<User> getUsers(@ApiIgnore Pageable pageable) { // Example implementation fetching users using Spring Data JPA return userRepository.findAll(pageable); } } @ApiIgnore: Prevents Pageable from being included in Swagger documentation as a separate parameter.@ApiOperation: Describes the operation (HTTP method and endpoint).@ApiImplicitParams: Lists the query parameters (page, size, sort) accepted by the endpoint.@ApiImplicitParam: Describes each query parameter (page, size, sort) with its name, data type, parameter type (query), and description.Run your Spring Boot application, and navigate to Swagger UI:
http://localhost:8080/swagger-ui.htmlReplace localhost:8080 with your actual host and port if different.
@ApiOperation, @ApiImplicitParams, @ApiImplicitParam) as needed for your specific requirements.page, size, sort) to Pageable in controller methods.springfox-swagger2, springfox-swagger-ui) are compatible with your Spring Boot version.By following this approach, you can effectively document and expose pagination capabilities using Pageable in your Spring RESTful API with Swagger, facilitating better API discovery and usage for consumers. Adjust the annotations and configurations according to your API design and documentation needs.
Swagger documentation for Spring Pageable with default settings:
Pageable interface with default settings.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } Pageable interface in a controller method to paginate results, documented with Swagger @ApiOperation.Swagger documentation for Spring Pageable with custom page size:
Pageable interface with a custom page size using Swagger.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@PageableDefault(size = 20) Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } @PageableDefault annotation.Swagger documentation for Spring Pageable with sorting:
Pageable interface with sorting enabled.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@PageableDefault(sort = "name") Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } name field in paginated results using @PageableDefault annotation.Swagger documentation for Spring Pageable with dynamic sorting:
Pageable interface with dynamic sorting options in Swagger.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@PageableDefault(sort = "name", direction = Sort.Direction.DESC) Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } name field in descending order using Sort.Direction with Swagger documentation.Swagger documentation for Spring Pageable with custom request parameters:
Pageable interface with custom request parameters.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size, @RequestParam(defaultValue = "name,asc") String[] sort) { Pageable pageable = PageRequest.of(page, size, Sort.by(sort)); Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } @RequestParam annotations, documented in Swagger.Swagger documentation for Spring Pageable with multiple sort fields:
Pageable interface with multiple sort fields.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@PageableDefault(sort = {"name", "age"}, direction = Sort.Direction.DESC) Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } name and age fields in descending order using @PageableDefault with Swagger.Swagger documentation for Spring Pageable with pageable response:
Page object using Pageable interface in Swagger.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<Page<User>> getAllUsers(Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users); } Page of User objects with pagination information, documented in Swagger.Swagger documentation for Spring Pageable with custom page parameters:
Pageable interface with custom page parameters.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(@PageableDefault(page = 0, size = 15) Pageable pageable) { Page<User> users = userRepository.findAll(pageable); return ResponseEntity.ok().body(users.getContent()); } @PageableDefault with Swagger documentation.Swagger documentation for Spring Pageable with pageable and filter:
Pageable interface and filter options in Swagger.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<Page<User>> getAllUsers(Pageable pageable, @RequestParam(required = false) String filter) { Page<User> users; if (filter != null && !filter.isEmpty()) { users = userRepository.findByFilter(filter, pageable); } else { users = userRepository.findAll(pageable); } return ResponseEntity.ok().body(users); } Swagger documentation for Spring Pageable with total pages count:
Pageable interface.@GetMapping("/users") @ApiOperation(value = "Get all users") public ResponseEntity<List<User>> getAllUsers(Pageable pageable) { Page<User> users = userRepository.findAll(pageable); HttpHeaders headers = new HttpHeaders(); headers.add("X-Total-Pages", String.valueOf(users.getTotalPages())); return ResponseEntity.ok().headers(headers).body(users.getContent()); } X-Total-Pages header with total pages count in response for paginated results, documented in Swagger.code-first sax intellij-13 development-environment distribution blur disabled-input cross-platform ngzone mcrypt