Tuesday, April 13, 2021

Spring paging

https://reflectoring.io/spring-boot-paging/ ; "Paging is the act of loading one page of items after another from a database, in order to preserve resources. This is what most of this article is about. Pagination is the UI element that provides a sequence of page numbers to let the user choose which page to load next."


https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/web/PagedResourcesAssembler.html

few tweaks to make it work with the spring

import org.springframework.data.web.PageableHandlerMethodArgumentResolver; 
import org.springframework.hateoas.client.LinkDiscoverer; 
import org.springframework.hateoas.client.LinkDiscoverers; 
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer; 
import org.springframework.plugin.core.SimplePluginRegistry; 
import org.springframework.web.method.support.HandlerMethodArgumentResolver; 



 
@Configuration 
@EnableSwagger2  
MySwaggerConfig extends WebMvcConfigurationSupport { 
      @Override public void addArgumentResolvers(List argumentResolvers) {
             argumentResolvers.add(new PageableHandlerMethodArgumentResolver()); 
 } 


//https://stackoverflow.com/questions/58431876/why-hateoas-starts-creating-issue-for-spring-boot-version-2-2-x-during-startu 

 @Bean public LinkDiscoverers discoverers() { 
    List plugins = new ArrayList<>(); 
    plugins.add(new CollectionJsonLinkDiscoverer()); 
    return new LinkDiscoverers(SimplePluginRegistry.create(plugins)); 
 }



The pageable default page and size


page -
The default-pagenumber the injected Pageable should get if no corresponding parameter defined in request (default is 0). 

size -
The default-size the injected Pageable should get if no corresponding parameter defined in request (default is 10). 

sort -
The properties to sort by by default. 

value -
Alias for size() 






adding pagination with mongo

pass in size,page,sort...

?page=0&size=2
?sort=name&sort=email,asc