我有这个控制器:
@RequestMapping("/api")
public ResponseEntity<?> a(Pageable pageable) throws IOException {
ObjectMapper mapper = new ObjectMapper( );
File file = new File( "samplejson.json" );
JsonNode jsonNode = mapper.readTree( file );
List<JsonNode> nodes = new ArrayList<>();
if ( !jsonNode.isArray() )
nodes.add( jsonNode );
else
for ( JsonNode node : jsonNode )
nodes.add( node );
return new ResponseEntity<Object>( new PageImpl( nodes, pageable, nodes.size() ), HttpStatus.OK );
}
当我邮寄它时,它给了我这个回复:
{
"content": [
{
"id": 1,
"first_name": "Dode",
"email": "dcardall0@zimbio.com",
"gender": "Female",
"timestamp": "11/12/1981"
},
{
"id": 2,
"first_name": "Andrus",
"email": "amcgeever1@jigsy.com",
"gender": "Male",
"timestamp": "10/25/1988"
},
{
"id": 3,
"first_name": "Allyn",
"email": "afakes2@samsung.com",
"gender": "Male",
"timestamp": "9/3/1997"
},
{
"id": 4,
"first_name": "Merell",
"email": "mmoreton3@census.gov",
"gender": "Male",
"timestamp": "3/7/2009"
}
],
"last": true,
"totalElements": 10,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": [
{
"direction": "DESC",
"property": "id",
"ignoreCase": false,
"nullHandling": "NATIVE",
"descending": true,
"ascending": false
}
],
"first": true,
"numberOfElements": 10 }
在URI中我有这个
http://localhost:8080/api?sort=id, desc
然而,分页不起作用。如您所见,响应中的排序方向表示&#34; DESC&#34;。所以它应该按降序排列,对吧?但它不起作用。我认为因为它位于由分页创建的对象内容中。但我不知道如何访问内部内容。
答案 0 :(得分:0)
为了应用排序等页面请求选项,您需要使用PagingAndSortingRepository(参见Spring Paging documentation)
因此,在存储库逻辑中执行排序和获取页面。请参阅org.springframework.data.jpa.repository.support.SimpleJpaRepository.readPage