2017-11-16 10 views
1

@RepositoryRestResource 의 옵션과 매우 유사하지만 페이징을 시도하고 있지만 Entity가 아닌 객체에만 적용하려고합니다. 엔티티가 아닌 객체에서 페이징을 수행하는 방법은 무엇입니까?

코드 :

public class FloorplanDevice { 

private String FloorplanName; 
private Long FloorplanId; 

private Long DeviceId; 
private String DeviceName; 
private String macAddress; 
private Long groupId; 
private LocatableType deviceType; 

public String getFloorplanName() { 
    return FloorplanName; 
} 

public void setFloorplanName(String floorplanName) { 
    FloorplanName = floorplanName; 
} 

public Long getFloorplanId() { 
    return FloorplanId; 
} 

public void setFloorplanId(Long floorplanId) { 
    FloorplanId = floorplanId; 
} 

public Long getDeviceId() { 
    return DeviceId; 
} 

public void setDeviceId(Long deviceId) { 
    DeviceId = deviceId; 
} 

public String getDeviceName() { 
    return DeviceName; 
} 

public void setDeviceName(String deviceName) { 
    DeviceName = deviceName; 
} 

public String getMacAddress() { 
    return macAddress; 
} 

public void setMacAddress(String macAddress) { 
    this.macAddress = macAddress; 
} 

public Long getGroupId() { 
    return groupId; 
} 

public void setGroupId(Long groupId) { 
    this.groupId = groupId; 
} 

public LocatableType getDeviceType() { 
    return deviceType; 
} 

public void setDeviceType(LocatableType deviceType) { 
    this.deviceType = deviceType; 
} 

public FloorplanDevice() { 
} 

public FloorplanDevice(String floorplanName, Long floorplanId, Long deviceId, String deviceName, String macAddress, Long groupId, LocatableType deviceType) { 
    FloorplanName = floorplanName; 
    FloorplanId = floorplanId; 
    DeviceId = deviceId; 
    DeviceName = deviceName; 
    this.macAddress = macAddress; 
    this.groupId = groupId; 
    this.deviceType = deviceType; 
    } 
} 

이 객체 는 저장소이없는 있지만, 컨트롤러가 :

@RequestMapping(
     path = arrayOf("/floorplanDevice/{groupId}", "/floorplanDevice/{groupId}/"), 
     method = arrayOf(RequestMethod.GET)) 
open fun getFloorplanDevice(@PathVariable("groupId") groupId: Long): ResponseEntity<*>{ 

var floorplanDevice= floorplanService.getFloorplanDevice(groupId) 
return ResponseEntity(floorplanDevice, HttpStatus.OK) 

} 

그래서 어떻게 페이지 수와 크기로이 개체에 페이징을 수행 할 수 있습니다 (가능하다면 정렬도 가능합니까?) 나는 자바 스프링을 사용하고

답변

1

이런 식으로 뭔가를 시도 감사 :

public Page<FloorplanDevice> getFloorplanDevice(@PathVariable("groupId") Long groupId, 
      @PageableDefault Pageable pageable) 
    List<FloorplanDevice> list = floorplanService.getFloorplanDevice(groupId); 

    MutableSortDefinition sort = pageable.getSort() != null ? 
      StreamSupport.stream(pageable.getSort().spliterator(), false) 
       .findFirst() 
       .map(it -> new MutableSortDefinition(it.getProperty(), it.isIgnoreCase(), it.isAscending())) 
       .orElse(null) 
      : null; 

    PagedListHolder<FloorplanDevice> pageHolder = new PagedListHolder<>(list, sort); 
    pageHolder.setPage(pageable.getPageNumber()); 
    pageHolder.setPageSize(pageable.getPageSize()); 
    pageHolder.resort(); 

    List<FloorplanDevice> content = pageHolder.getPageList(); 
    Page<FloorplanDevice> page = new PageImpl<>(content, pageable, list.size()); 

    return page; 
} 
+0

솔루션에 감사드립니다. 나는 다른 것을 가지고있다, 병은 지금 그것을 게시한다. –

0

솔루션 :이 PageListHolder를 사용하여 내 솔루션은 입니다.

 @RequestMapping(
     path = arrayOf("/floorplanDevice/{groupId}/page/{pageNum}/size/{sizeNum}", "/floorplanDevice/{groupId}/page/{pageNum}/size/{sizeNum}/"), 
     method = arrayOf(RequestMethod.GET)) 
open fun getFloorplanDevicePage(@PathVariable("groupId") groupId: Long, @PathVariable("pageNum") pageNum: Int,@PathVariable("sizeNum") sizeNum: Int): ResponseEntity<*>{ 

var floorplanDevice= floorplanService.getFloorplanDevice(groupId) 
var paging= PagedListHolder<FloorplanDevice>(floorplanDevice) 
var setsize= paging.setPageSize(sizeNum) 
if(paging.pageCount>pageNum){ 
var setpage=paging.setPage(pageNum)} 
else{ 
    return throw RuntimeException("page: $pageNum is too big, insert smaller page from 0 to ${paging.pageCount-1}") 
    } 
var pageList= paging.pageList 
return ResponseEntity(pageList, HttpStatus.OK) 

}