Using PagingAndSortingRepository the content returns with {} and right number of content from page url parameter. Example: My table has 20 rows and in url a put ?page=0&size=10. When returns in the postman the key content return
"content": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
]
Debuging the request and return it has values. But is not showing. Don't know why.
my repository
public interface LocalUtilRepository extends PagingAndSortingRepository<LocalUtilModel, Integer> {
Page<LocalUtilModel> findAll(Pageable pageable);
}
Spring boot version
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
The result from request
{
"content": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 10,
"unpaged": false,
"paged": true
},
"totalPages": 2,
"totalElements": 11,
"last": false,
"size": 10,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"numberOfElements": 10,
"first": true,
"empty": false
}
My model
@Entity(name = "EXPLOCALUTIL")
public class LocalUtilModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@Size(min = 5, max = 255)
@Column(name = "DESCLOCALUTIL")
private String descLocalUtil;
@Column(name = "COORD")
private String coordenada;
private String referencia;
@NotNull
@Column(name = "IDEXPFEIRA")
private Integer idExpFeira;
}
Based on your JSON response it is showing like "offset":0
.
Try to pass this in your repository pageable parameter.
PageRequest.of(0, 10)
here 0 is the page number and 10 is offset.
Formula definition:
int start = pageNumber * offset + 1;
int end = start+offset.
So in our scenario the start will be 1 and end will 10. This will fetch the top 10 returns.
ResponseEntity.ok().body( localUtilRepository.findAll(PageRequest.of(0, 10))
Just pass your page in first Param and size in second Param.
我必须在模型类中使用@Data
进行注释,因为我正在使用lombok
。
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.