How to use pagination in Spring Boot — OpenFeign

Teten Nugraha
Backend Habit
Published in
1 min readOct 16, 2020
Photo by Nathan Dumlao on Unsplash

Let assume that you using Open Feign as HTTP Client which this library

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

and you have requirement to get data from other services with pagination,let says that you have target endpoint like this.

@GetMapping("users")
public ResponseEntity<Page<User>> getUser(Pageable pageable) {

return userService.getUserPageable(pageable);
}

Feign Part

Lets create simple page custom object to get page data source above.

and also need to create custom config for Feign Interface that you will used.

because this case using pagination you should make bean PageableQueryEncoder like this

and as usual you use SimplePageImpl on Feign like this,

@GetMapping("/api/users")
ResponseMessage<SimplePageImpl<User>> getUsers(
Pageable page
);

but another important thing is you should add bean PageJacksonModule becaouse feign is using jackson.

If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.

--

--

Teten Nugraha
Backend Habit

Software Engineer, 8 years of experience. Expertise with Microservices, Spring Boot, CICD, Docker https://www.linkedin.com/in/teten-nugraha