Table of Contents
Ordinary, Spring Pageable starts page number from zero. Here I introduce the way to begin page number with 1.
Environment
- Spring 5
- Spring Boot 2
How to Do
Add the following Configuration
. I show Java and Kotlin code.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.improve_future.sample.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; @Configuration public class CustomMvcConfiguration implements WebMvcConfigurer { @Override public void addArgumentResolvers( List<HandlerMethodArgumentResolver> argumentResolvers) { PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver(); resolver.setOneIndexedParameters(true); argumentResolvers.add(resolver); } } |
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.enjoy_your_sound.sample.config import org.springframework.context.annotation.Configuration import org.springframework.data.web.PageableHandlerMethodArgumentResolver import org.springframework.web.method.support.HandlerMethodArgumentResolver import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @Configuration open class WebMvcConfiguration : WebMvcConfigurer { override fun addArgumentResolvers(argumentResolvers: MutableList<HandlerMethodArgumentResolver>?) { val resolver = PageableHandlerMethodArgumentResolver() resolver.setOneIndexedParameters(true) argumentResolvers!!.add(resolver) } } |
Then, page number starts with 1.
Example
Kotlin code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.improve_future.sample.presentation.sample import com.improve_future.sample.domain.sample.service.SampleService import com.improve_future.sample.presentation.core.Pager import org.springframework.beans.factory.annotation.Autowired import org.springframework.data.domain.Pageable import org.springframework.data.web.PageableDefault import org.springframework.http.MediaType import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.* import org.springframework.web.servlet.mvc.support.RedirectAttributes import javax.servlet.http.HttpServletRequest @Controller @RequestMapping(value = "sample") open class SampleController { @Autowired private lateinit var sampleService: SampleService @GetMapping( consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE)) @ResponseBody fun list( @PageableDefault(size = 30) pageable: Pageable ): Map<String, Any?> { val samplePage = sampleService.findAll(pageable) return SampleJsonView.list(samplePage) } } |
Then, we can get records on /sample
. First 30 records will be gotten on /sample?page=1
.
I show the service and the repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.improve_future.sample.domain.sample.service import com.improve_future.sample.domain.sample.model.Sample import com.improve_future.sample.domain.sample.repository.SampleRepository import com.improve_future.sample.domain.sample.repository.SampleSpecification import org.springframework.beans.factory.annotation.Autowired import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service @Service class SampleService { @Autowired lateinit private var sampleRepository: SampleRepository fun findAll(pageable: Pageable): Page<Sample> { return sampleRepository.findAll(pageable) } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.improve_future.sample.domain.sample.repository import com.improve_future.sample.domain.sample.model.Sample import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaSpecificationExecutor import org.springframework.data.repository.PagingAndSortingRepository @org.springframework.stereotype.Repository interface SampleRepository: JpaRepository<Sample, Long>, PagingAndSortingRepository<Sample, Long>, JpaSpecificationExecutor<Sample> |