Java 使用Spring Boot 2将Spring数据页对象序列化为JSON?
我正在尝试将我的Spring Boot 1.5项目升级到Spring Boot 2.0.2。这还包括对SpringData2.x的更新 我现在注意到Java 使用Spring Boot 2将Spring数据页对象序列化为JSON?,java,spring-boot,spring-data,Java,Spring Boot,Spring Data,我正在尝试将我的Spring Boot 1.5项目升级到Spring Boot 2.0.2。这还包括对SpringData2.x的更新 我现在注意到页面的JSON表示已经改变。现在看来: { "content": [{ "id": "96ab09c6-2cfc-4195-899b-899b623e6e97", "title": "Test Title", "shortDescription": "Short description",
页面
的JSON表示已经改变。现在看来:
{
"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
}
}
如果对其进行排序,则类似于以下内容:
"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
}
请注意以前不存在的
pageable
元素。另外,sort
元素也不是很有用。这是我们的初衷吗?或者在我的REST控制器中返回org.springframework.data.domain.Page
对象是个坏主意?从某种意义上讲,Spring倾向于向后兼容
这可能是向后不兼容的一个小例子,但Pageable是一个特定于Spring的构造,谁说它永远不会改变呢。取决于Spring的一个实现数据类,直到REST层似乎不正确为止。最好在服务层中处理此更改
这是一个很好的例子,展示了“分层”体系结构的好处:)我可以很容易地对其进行更改,问题更多的是我是否应该更改,以及我是否应该使用Spring数据中的其他内容。我仍然建议从REST controller返回您自己的对象,因为在以后的版本中可能会出现相同的问题,即使您选择使用另一种spring方式/class.Related: