I'm not sure what I'm missing here. My custom logic applies to all String attributes, instead of just one, that I specified for target.
@ApiModel(value="ShopProduct")
@Data
public class ShopProductDTO {
private String description;
private String fullImagePath;
}
@Entity
@Table(name = "shop_product")
public class ShopProduct implements Serializable {
@Column(name = "desc")
private String description;
@Column(name = "thumb_path")
private String thumbPath;
//getters,setters
ImageMapper:
@Component
public class ImagePathMapper {
AppConfig appConfig;
public ImagePathMapper(AppConfig appConfig) {
this.appConfig = appConfig;
}
public String toFullImagePath(String thumbPath){
return appConfig.getImagePath() + thumbPath;
}
}
ShopProduct Mapper:
@Mapper(componentModel = "spring", uses = {ImagePathMapper.class})
@Component
public interface ShopProductMapper {
@Mapping(target = "fullImagePath", source = "thumbPath")
ShopProductDTO shopProductToShopProductDTO(ShopProduct shopProduct);
}
The generated mapstruct class:
ShopProductDTO shopProductDTO = new ShopProductDTO();
shopProductDTO.setDescription( imagePathMapper.toFullImagePath( shopProduct.getName() ) );
shopProductDTO.setFullImagePath( imagePathMapper.toFullImagePath( shopProduct.getThumbPath() ) );
}
Why is the description field also being used with toFullImagePath?
Shouldn't this "@Mapping(target = "fullImagePath", source = "thumbPath")" specify that I want fullImagePath changed only?