Convert type of Spring Data JPA Page content

Turns out that Page has its own .map() method, to which you can pass a method reference to do the conversion.

Here is how I ended up doing the conversion.

final Page<ContactDto> contactDtoPage = contactPage.map(this::convertToContactDto);

The convertToContactDto method simply creates and returns an instance of the class I’m trying to convert to:

private ContactDto convertToContactDto(final Contact contact) {
    final ContactDto contactDto = new ContactDto();
    //get values from contact entity and set them in contactDto
    //e.g. contactDto.setContactId(contact.getContactId());
    return contactDto;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)