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.
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;
}
This worked for me. Spring HATEOAS – Pagination links -> https://howtodoinjava.com/spring5/hateoas/pagination-links
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.