I want to implement pagination in a spring boot application which gives link to next page and previous page in the response if applicable along with the current page contents. I have checked this https://github.com/javadevjournal/javadevjournal/tree/master/spring/rest-pagination but hateos mvc dependencies are no longer supported with spring boot 2.2.6. Could you please help me out here.
Asked
Active
Viewed 2,329 times
1
-
Why do you say the HATEOAS dependencies aren't supported? – chrylis -cautiouslyoptimistic- Jun 10 '20 at 11:48
-
Does this help/answer your question: https://stackoverflow.com/questions/32434058/how-to-implement-pagination-in-spring-boot-with-hibernate ? – Oozeerally Jun 10 '20 at 12:00
-
I was able to do it via https://howtodoinjava.com/spring5/hateoas/pagination-links/. @ chrylis -cautiouslyoptimistic- there are some major changes in spring HATEOS which can be found here (https://docs.spring.io/spring-hateoas/docs/current/reference/html/). The link I provided uses old classes which are deprecated now. – TheNightsWatch Jun 10 '20 at 13:42
2 Answers
1
You can achieve the same using AOP
and implementing ResponseBodyAdvice
.
ResponseBodyAdvice allows the customization of the response object before Spring MVC writes it to the response body.
AOP will help you to achieve this in generic way so that it will apply to all applicable controller methods.
AOP code
This code will set the page in the PageStoreClass
class.
Create the PageStoreClass containing Page as attribute/field
@Aspect
@Configuration
public class PaginationAdvice {
@Autowired
private PageStoreClass pageStoreClass;
@AfterReturning(value = "execution(org.springframework.data.domain.Page com.test.repository.*.*(..))", returning = "page")
public void afterReturning(JoinPoint joinPoint, Page page) {
pageStoreClass.setPage(page);
}
}
ResponseBodyAdvice implementation code
@RestControllerAdvice
public class ResponsePagination implements ResponseBodyAdvice<Object> {
@Autowired
private PageStoreClass pageStoreClass;
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
Page page = pageStoreClass.getPage();
final StringBuilder header = new StringBuilder();
if (page != null) {
if (!page.isFirst()) {
String firstPage = ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam("page", 0)
.replaceQueryParam("size", page.getSize()).build().encode().toUriString();
header.append(firstPage + "rel= first");
}
if (page.hasPrevious()) {
final String prevPage = ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam("page", pageable.getPageNumber()).replaceQueryParam("size", pageable.getPageSize()).build().encode().toUriString();
header.append(prevPage + "rel= prev"));
}
if (page.hasNext()) {
final String nextPage = ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam("page", pageable.getPageNumber()).replaceQueryParam("size", pageable.getPageSize()).build().encode().toUriString();
header.append(nextPage + "rel= next"));
}
if (!page.isLast()) {
final String lastPage = ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam("page", page.getTotalPages() - 1).replaceQueryParam("size", page.getSize()).build().encode().toUriString();
header.append(lastPage + "rel= last"));
}
if (header.length() > 0)
response.getHeaders().add(HttpHeaders.LINK, header.toString());
}
return body;
}

SSK
- 3,444
- 6
- 32
- 59
0
This worked for me. Spring HATEOAS – Pagination links -> https://howtodoinjava.com/spring5/hateoas/pagination-links

TheNightsWatch
- 371
- 10
- 26