0

I am trying to upgrade my Spring Boot 1.5 project to Spring Boot 2.0.2. This includes an update to Spring Data 2.x as well.

I have now noticed that the JSON representation of Page has changed. It now looks like:

{
    "content": [{
        "id": "96ab09c6-2cfc-4195-899b-899b623e6e97",
        "title": "Test Title",
        "shortDescription": "Short description",
        "description": "Test Description",
        "date": "2018-02-14",
        "imageUrl": "/api/images/newsposts/f637e6bd-a13a-4ebc-8c58-8ba639e09f70"
    }],
    "pageable": "INSTANCE",
    "totalPages": 1,
    "totalElements": 1,
    "last": true,
    "size": 0,
    "number": 0,
    "first": true,
    "numberOfElements": 1,
    "sort": {
        "unsorted": true,
        "sorted": false
    }
}

If it is sorted, it looks similar to this:

    "pageable": {
        "sort": {
            "sorted": true,
            "unsorted": false
        },
        "offset": 0,
        "pageSize": 20,
        "pageNumber": 0,
        "paged": true,
        "unpaged": false
    },
    "totalElements": 1,
    "last": true,
    "totalPages": 1,
    "size": 20,
    "number": 0,
    "first": true,
    "numberOfElements": 1,
    "sort": {
        "sorted": true,
        "unsorted": false
    }

Notice the pageable element that was not there before. Also the sort element is not very useful. Is this something that was intended? Or it is just a bad idea to return org.springframework.data.domain.Page object in my REST controller?

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211

1 Answers1

0

APIwise, Spring tends to be very backward compatible.

This could be one of the minor case of backward incompatibility but Pageable is a Spring-specific construct and who's to say it will never change. Depending on one of the Spring's implementation data class till the REST layer does not seem right somehow. It would be good to handle this change in the service layer.

This is a very good example which shows the benefits of a 'layered' architecture :)

gargkshitiz
  • 2,130
  • 17
  • 19
  • I _can_ easily change it, the question is more if I should, and if maybe there is something else in Spring Data that I should use instead. – Wim Deblauwe May 14 '18 at 07:15
  • I would still suggest to return your own objects from REST controller, because same problem can occur in later versions, even if you choose to use another spring way/class. – gargkshitiz May 14 '18 at 07:17