Spring&Spring Boot

(Spring)(Pageable) Page<F>에서 Page<T>로 바꾸는 클래스

Developer RyanKim 2019. 6. 18. 21:17

 

public class TypeTranslator {

  public static <F, T> Page<T> domainPageToDTOPage(Page<F> domainPage, String dtoClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    List<T> dtoList = new ArrayList<>();
    Class dtoClass = Class.forName(dtoClassName);

    for (F e : domainPage) {
      Object dto = dtoClass.newInstance();
      BeanUtils.copyProperties(e, dtoClass.cast(dto));
      dtoList.add((T) dto);
    }
	return new CustomPageImpl<>(dtoList, domainPage.getPageable(), domainPage.getTotalElements());
  }
}