How To Handle Spring Data Pageable in Feign Client

By | August 28, 2018

If you don’t know about how to get content of Page in Spring Rest Client with Feign, hopefully this tutorial will help you to solve your problems. Let’s see how to handle spring data pageable in Feign Client below.

How To Handle Spring Data Pageable in Feign Client

When I’am building a MicroService with spring cloud, I think that feign is the good choice for the rest api client.
Unfortunately FeignClient interface is not support for Spring Data Pageable functionality. I am so sad 😭. But do not worry about that. If you still really want to retrieve response from Spring Data Pageable, you can make a custom ResponseBody like this example:

Sample response with JSON format:

{
  "code": "SUCCESS",
  "message": "SUCCESS",
  "errors": null,
  "data": {
    "content": [
      {
        "id": "5b82d48aae70060d69785736",
        "version": 0,
        "createdDate": 1535300746521,
        "createdBy": "system",
        "updatedDate": 1535300746521,
        "updatedBy": "system",
        "isDeleted": 0,
        "username": "guest",
        "password": "$2a$10$RPMVL3HIwG/CY/4rf0faVeQejDfC.C/v1IQmRT3tWIBKqbGK7QdXG",
        "fullName": "Guest",
        "email": "guest@guest.com",
        "enabled": true,
        "roles": [
          "ROLE_GUEST"
        ],
        "lastPasswordResetDate": null,
        "menuAccess": [
          {
            "id": null,
            "version": null,
            "createdDate": null,
            "createdBy": null,
            "updatedDate": null,
            "updatedBy": null,
            "isDeleted": 0,
            "accessCode": "DASHBOARD",
            "accessName": "Dashboard",
            "pathUrl": "/dashboard"
          }
        ]
      }
    ],
    "pageable": {
      "sort": {
        "sorted": true,
        "unsorted": false
      },
      "offset": 0,
      "pageSize": 20,
      "pageNumber": 0,
      "unpaged": false,
      "paged": true
    },
    "totalPages": 1,
    "totalElements": 1,
    "last": true,
    "size": 20,
    "number": 0,
    "numberOfElements": 1,
    "first": true,
    "sort": {
      "sorted": true,
      "unsorted": false
    }
  },
  "serverTime": 1535423509684
}

Custom class (for the response):

//to be continue 22:00 today

How To Handle Spring Data Pageable in Feign Client

That is how to get content of Page in Spring Rest Client with Spring Data Pageable in Feign Client.
Hopefully in the future this library (Feign Client) will adding support for spring data pageable functionality.

More tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *