How to returnDTO instead of entity in Spring boot pagination?

Tags: ,

My DTO is different from entity. How can I return a DTO instead of entity with pagination while still showing information of all pages?


public Page<Student> getStudent(@PathVariable(value = "name") String name, Pageable pageable){
     Page <Student> page = studentService.getStudent(name, pageable);
     return page;


public Page<Student> getStudent(String name, Pageable pageable){
    Page<Student> students = studentRepository.findAllByName(name, pageable);
    return students;


public interface StudentRepository extends 
    PagingAndSortingRepository<Student, Long> {
    Page<Student> findAllByName(String name, Pageable pageable);


public class StudentDTO extends ResourceSupport {
   Long _id;
   String name;


@NoArgsConstructor(force = true, access = AccessLevel.PUBLIC)
public class Student {

    private Long id;
    private String name;
    private Long grade;


The StudentDTO class can have a constructor with a Student parameter.

public StudentDTO(Student student) {
    this._id = student.getId(); = student.getName();

Then you can call map on the Page object.

Page<StudentDTO> dtoPage = -> new StudentDTO(student));

Source: stackoverflow