Reputation: 45
How i can initialize List in field and add in this list some values?
@Mapper(componentModel = "spring")
public interface MapStructAdapter {
@Mapping(source = "some", target = "some")
@Mapping(expression = "java(new java.util.ArrayList<LegalEntity>())", target = "legalEntities")
@Mapping(expression = "java(new my.some.package.LegalEntity())", target = "getLegalEntities().add()")
@Mapping(source = "entityShortName", target = "legalEntities.legalEntity.shortName")
Representative convert(Message message);
}
Upvotes: 0
Views: 305
Reputation: 4160
Just add a (factory)method to your mapper. No argument and return the List. Can be a default method or a method in s class you @Mapper#uses
Upvotes: 0
Reputation: 26
You should be able to add your own conversion methods in a mapper.
https://mapstruct.org/documentation/stable/reference/html/#adding-custom-methods
Example,
@Mapper(componentModel = "spring")
public interface MapStructAdapter {
@Mapping(source = "some", target = "some")
@Mapping(source = "entityShortName", target = "legalEntities")
Representative convert(Message message);
default List<LegalEntity> toLegalEntities(String entityShortName) {
LegalEntity legalEntity = new LegalEntity();
legalEntity.setShortName(entityShortName);
return Collections.singletonList(legalEntity);
}
}
Upvotes: 1