java - Convert Page<Entity> to PageDTO<EntityDTO> -
i using spring data jpa.
my controller looks following
@requestmapping(value = "/pages/{pagenumber}", method = requestmethod.get, consumes = mediatype.application_json_value, produces = mediatype.application_json_value) public responseentity<page<user>> paginateduser(@pathvariable final integer pagenumber) { final pagerequest request = new pagerequest(pagenumber - 1, default_page_size, sort.direction.desc, "starttime"); return new responseentity<>(userrepository.findall(request), httpstatus.ok); }
now decided send instead of page object, pagedto object restrict things sending.is there way can convert page pagedto using java 8.
i saw page derived iterable
guess can following not sure how put pagedto , userdto.
streamsupport.stream(userrepository.findall(request).spliterator(),false)
is there effecient java 8 way this.
i came solution
@requestmapping(value = "/pages/{pagenumber}", method = requestmethod.get, consumes = mediatype.application_json_value, produces = mediatype.application_json_value) public responseentity<pagedto> paginateduser(@pathvariable final integer pagenumber) { final pagerequest request = new pagerequest(pagenumber - 1, default_page_size, sort.direction.desc, "starttime"); final page<user> page = userrepository.findall(request); return new responseentity<>(new pagedto(page, streamsupport.stream(page.getcontent().spliterator(), true).map(userdto::new) .collect(collectors.tolist())), httpstatus.ok); } public class pagedto { private int beginindex; private int currentindex; private int endindex; private list<?> entities; public pagedto(final page<?> page, final list<?> entities) { this.entities = entities; this.currentindex = page.getnumber() + 1; this.beginindex = math.max(1, currentindex - 5); this.endindex = math.min(beginindex + 10, page.gettotalpages()); }
would know if there effecient way this?
i know old question, ran same problem , i'd provide possible solution whomever may interested. here found helped me code: https://github.com/pkainulainen/spring-data-jpa-examples/blob/master/query-methods/src/main/java/net/petrikainulainen/springdata/jpa/todo/todomapper.java
i used jparepository paginates data, dto page have same parameters (page number, size etc). here repository:
@repository public interface repository extends jparepository<entity, integer> { /** * name method according query need execute * e.g. findall --> return rows satisfy following conditions, * byusername --> username field in entity class, * @param pageable: pagination applied on data. * @return */ public page<entity> findallbyusername(string username, pageable pageable); }
this method mapping:
public page<entitydto> findbyusername(string username, pageable pageable){ page<entity> entitypage = entityrepository.findallbyusername(username, pageable); list<entitydto> dtos = mapper.entitytoentitydtos(entitypage.getcontent()); return new pageimpl<>(dtos, pageable, entitypage.gettotalelements()); }
and mapstruct mapper:
import org.mapstruct.factory.mappers; /** * mapper converting entity dto. */ @mapper(componentmodel = "spring", uses = {}) public interface mapper { /** * interface declares member instance, providing clients access mapper implementation, * file target\generated-sources\com\company\springapp\dto\mappers\mapperimpl.java * (automatically generated when compiling project). */ auditmapper instance = mappers.getmapper( mapper.class ); /** * convert entity dto. * mappings used 'bind' entity fields dto fields (for mapper's implementation). * @param entity * @return */ @mappings({ @mapping(source = "id", target = "id"), @mapping(source = "username", target = "dtousername"), @mapping(source = "action", target = "dtoaction") }) public entitydto entitytoentitydto(entity entity); /** * convert entities' list dtos' list. * @param entities * @return */ public list<entitydto> entitiestoentitydtos(list<entity> entities); }
Comments
Post a Comment