英文:
How to sorting repository entity with pagination in spring boot
问题
我正在使用Spring Boot编写一个REST API。在这里,我尝试使用'PagingAndSortingRepository'对列表进行排序。这是我尝试的方法:
在我的Controller类中的方法:
@GetMapping
public ResponseEntity<PagedModel<ComplainDTO>> getComplains(
@RequestParam(defaultValue = "0", value = "page", required = false) int page,
@RequestParam(value = "sortBy", required = false, defaultValue = "complainId") String sortBy,
PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page, sortBy)));
}
在我的服务中的方法:
@Override
public Page<ComplainDTO> getAllComplains(int page, String sortBy) {
Pageable pageable = PageRequest.of(page, 20, Sort.by(sortBy));
Page<ComplainEntity> result = complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
我正在从消费者那里获取输入,以根据给定的属性对列表进行排序,然而Sort的默认行为是升序的。那么,我如何实现在使用if-else之外的情况下,根据用户参数来实现升序或降序的功能呢?
英文:
I am writing an REST API with Spring Boot.Here,I am trying to sort the list using the 'PagingAndSortingRepository'.This is what I have tried
Method in my Controller class
@GetMapping public ResponseEntity
<PagedModel<ComplainDTO>> getComplains(@RequestParam(defaultValue = "0",value = "page",required =false)int page, @RequestParam(value="sortBy" ,required = false,defaultValue = "complainId") String sortBy, PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page,sortBy)));
}
Method on my service
@Override
public Page<ComplainDTO> getAllComplains(int page,String sortBy) {
Pageable pageable = PageRequest.of(page,20, Sort.by(sortBy));
Page<ComplainEntity> result =complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
I am taking input from the consumer to sort the list based on given attribute however the default behavior of Sort is Ascending .So,how can I implement this taking user params whether it being ascending or descending aside from using if-else.
答案1
得分: 0
以下是翻译好的内容:
其中一个选项是要求用户使用@RequestParam
提供排序顺序
@GetMapping
public ResponseEntity<PagedModel<ComplainDTO>> getComplains(
@RequestParam(defaultValue = "0", value = "page", required = false) int page,
@RequestParam(value = "sortBy", required = false, defaultValue = "complainId") String sortBy,
@RequestParam(value = "orderBy", required = false, defaultValue = "ASC") String orderBy,
PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page, sortBy, orderBy)));
}
然后在服务中将排序顺序传递给Sort
方法
@Override
public Page<ComplainDTO> getAllComplains(int page, String sortBy, String orderBy) {
Pageable pageable = PageRequest.of(page, 20, Sort.by(Sort.Direction.valueOf(orderBy), sortBy));
Page<ComplainEntity> result = complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
英文:
The one option is asking the user to provide the sorting order using @RequestParam
@GetMapping public ResponseEntity
<PagedModel<ComplainDTO>> getComplains(@RequestParam(defaultValue = "0",value = "page",required =false)int page,
@RequestParam(value="sortBy" ,required = false,defaultValue = "complainId") String sortBy,
@RequestParam(value="orderBy" ,required = false,defaultValue = "ASC") String orderBy, PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page,sortBy,orderBy)));
}
And then in the service pass the sorting order to Sort
method
@Override
public Page<ComplainDTO> getAllComplains(int page,String sortBy, String orderBy) {
Pageable pageable = PageRequest.of(page,20, Sort.by(Sort.Direction.valueOf(orderBy),sortBy));
Page<ComplainEntity> result =complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论