我想为我的REST API实现分页。 该应用程序使用Spring Hateoas 0.8,Spring MVC 3.2.4和Spring Integration 2.2.4。 我看到Spring Hateoas有PagedResources类,但这并没有将结果分成页面。 似乎spring-data需要正确创建页面,并正确创建上一个和下一个链接。
我试过这个但链接不正确并显示所有结果:
@RequestMapping(value = "/resources", method = RequestMethod.GET, produces="application/json")
public HttpEntity<Resources<RequestResource>> showAllAsResources() {
Iterable<? extends Request> people = requestAccess.getRequests();
RequestAssembler assembler = new RequestAssembler();
List<RequestResource> resources = assembler.toResources(people);
Resources<RequestResource> wrapped = new Resources<RequestResource>(resources, linkTo(RequestsController.class)
.withSelfRel());
return new HttpEntity<Resources<RequestResource>>(wrapped);
}
所有谷歌搜索似乎都指向spring-data。 我不能使用spring-data,因为我从spring-integration获取我的数据,它调用soap web服务并返回搜索的所有结果。 我确实尝试添加spring-data类:
@RequestMapping(value = "/resources2", method = RequestMethod.GET, produces="application/json")
public HttpEntity<PagedResources<Resource<RequestResource>>> viewResources()
{
List<Request> requests = requestAccess.getRequests2();
Page page = new PageImpl<Request>(requests, new PageRequest(0, 5), requests.size());
RequestAssembler requestAssembler = new RequestAssembler();
return new ResponseEntity<PagedResources<Resource<RequestResource>>>(pagedResourcesAssembler.toResource(page, requestAssembler), HttpStatus.OK);
}
这显示下面的响应似乎有正确的链接,但所有数据都显示,而不仅仅是页面数据:
{
"links": [
{
"rel": "next",
"href": "http://lwss:9084/lerest/v1/resources2?page=2&size=5"
},
{
"rel": "prev",
"href": "http://lwss:9084/lerest/v1/resources2?page=0&size=5"
}
],
"content": [
{
"givenName": "Joe",
"familyName": "Bloggs101",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/101"
}
]
},
...
{
"givenName": "Joe",
"familyName": "Bloggs200",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/101"
}
]
},
]
}
],
"page": {
"size": 5,
"totalElements": 100,
"totalPages": 20,
"number": 1
}
}
是否有任何Spring类可以帮助我实现这一目标,还是需要实现自己的分页?
非常感谢, 马丁
更新
使用Custom pagination by array list中的解决方案,下面的代码会产生下面的响应,但我认为这有点过分。是否有任何Spring助手类可以实现相同的目标?
@RequestMapping(value = "/resources1", method = RequestMethod.GET, produces="application/json")
public HttpEntity<PagedResources<Resource<RequestResource>>> viewResources1(Pageable pageable)
{
int page = pageable.getPageNumber();
int count = pageable.getPageSize();
RequestAssembler requestAssembler = new RequestAssembler();
List<Request> requests = requestAccess.getRequests2();
int max = (count*(page+1)>requests.size())? requests.size(): count*(page+1);
Page<Request> pageRequests = new PageImpl<Request>(requests.subList(page*count, max), pageable, requests.size());
return new ResponseEntity<PagedResources<Resource<RequestResource>>>(pagedResourcesAssembler.toResource(pageRequests, requestAssembler), HttpStatus.OK);
}
{
"links": [
{
"rel": "next",
"href": "http://lwss:9084/lerest/v1/resources1?page=4&size=20"
},
{
"rel": "prev",
"href": "http://lwss:9084/lerest/v1/resources1?page=2&size=20"
}
],
"content": [
{
"givenName": "Joe",
"familyName": "Bloggs161",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/161"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs162",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/162"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs163",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/163"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs164",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/164"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs165",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/165"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs166",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/166"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs167",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/167"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs168",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/168"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs169",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/169"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs170",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/170"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs171",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/171"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs172",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/172"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs173",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/173"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs174",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/174"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs175",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/175"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs176",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/176"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs177",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/177"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs178",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/178"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs179",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/179"
}
]
},
{
"givenName": "Joe",
"familyName": "Bloggs180",
"links": [
{
"rel": "self",
"href": "http://lwss:9084/lerest/v1/180"
}
]
}
],
"page": {
"size": 20,
"totalElements": 100,
"totalPages": 5,
"number": 3
}
}