Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
"status": true,
"data": []

the above result is im getting in postman but i need to get response in dto form, Any help would be very helpful. thanks in advance

What I have tried:

@Override
   public ReporterResponse getAdminsByCompanyId(String companyId) {
       try {
           List<AdminEntity> res = adminRepo.findAllByCompanyId(companyId);


           ArrayList<AdminDto> access = new ArrayList<AdminDto>();
           if (Objects.nonNull(res)) {
          res.stream().forEach(adminEntity -> { access.add(AdminDto.convertDaoToDto(adminEntity));
          });
               System.out.println(access);
           response=ReporterResponse.builder().status(true).data(res).build();

           }
           else
               response = ReporterResponse.builder().status(false).data(null).build();
           return response;
       } catch (Exception e) {
           e.printStackTrace();
           response = ReporterResponse.builder().status(false).data(null).build();
           return response;

       }
Posted
Updated 4-Sep-21 12:42pm
v2

1 solution

Hello,

what do you mean by dot form?

Another option could be to use MapStruct and do something like this:


public List<AdminDto> getAdminsByCompanyId(String companyId) {
    return adminRepo.findAllByCompanyId(companyId).stream()
            .map(AdminMapper::toDto) // I assume you could also do this .map(AdminDto::convertDaoToDto)
            .collect(Collectors.toList());
}


if you want to wrap into your object ReporterResponse the resutlat you could also do this:

public ResponseDate getAdminsByCompanyId(String companyId) {
        ReporterResponse reporterResponse = new ReporterResponse();
        reporterResponse.setData(adminRepo.findAllByCompanyId(companyId).stream()
                .map(AdminMapper::toDto)
                .collect(Collectors.toList()));
        reporterResponse.setStatus(true);
}


Hope it help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900