Displaying common list dracers

I have a ListWrapper like

public class ListWrapper<T> {

private List<T> entries = new ArrayList<T>();

public List<T> getEntries() {
    return entries;
}

public void setEntries(List<T> entries) {
    this.entries = entries;
}

and bean like

public class AccountBo {
    private String accountName;

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

}

and another bean like

public class AccountDto {
    private String accountName;

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }    
}

now the idea is to populate a list of beans of type AccountBo and use Dozer to match the list, and then populate AccountDto Beans.

    AccountBo accountA = new AccountBo();
    accountA.setAccountName("Person A");        
    AccountBo accountB = new AccountBo();
    accountB.setAccountName("Person B");

    ListWrapper<AccountBo> listWrapperBo = new ListWrapper();

    listWrapperBo.getEntries().add(accountA);
    listWrapperBo.getEntries().add(accountB);

    ListWrapper<AccountDto> dtoList = EntityMapper.getInstance().map(listWrapperBo, ListWrapper.class);

    List<AccountDto> listDto = dtoList.getEntries();

But - the beans in the target list are of type AccountBo ....

What can I do to get an AccountDto list?

+3
source share
1 answer

I recommend using ModelMapper instead of Dozer for this.

The simplest solution is to create a subclass of ListWrapper that includes AccountDto:

public class DtoListWrapper extends ListWrapper<AccountDto> {
}

Then, when you go to the map, ModelMapper will know that the ListWrapper containing AccountBOs needs to be converted to a ListWrapper containing AccountDtos.

ModelMapper modelMapper = new ModelMapper();
ListWrapper<AccountDto> listWrapper = modelMapper.map(listWrapperBo, DtoListWrapper.class);

! ModelMapper:

http://modelmapper.org

+2

All Articles