How to map only specified fields using mapStruct?

醉酒当歌 提交于 2019-12-23 09:56:12

问题


MapStruct is mapping all the properties of source and destination by default if they have same name. Although we can use ignore property in @Mapping for omitting any field mapping. But that's not I want. I want control over the mapping strategy. I've to specify something like

@Mapper(STRATEGY=MAPPING_STRATEGY.SPECIFIED)
public interface EmployeeToEmployeeDTOMapper {
        @Mappings({
                   @Mapping(target="id", source="id"),
                   @Mapping(target="name", source="name")
                 })
        public EmployeeDTO employeeToEmployeeDTO (Employee emp);
}

Now this mapping is only meant to map id and name from source to destination.No other fields should be mapped unless specified in the mappings annotation.


回答1:


What you are looking for is a feature request in #1392. There is a pending PR so it would be available to use in the next version (1.3.0). The final API is not yet defined. Follow the issue and the PR to be notified when it gets done



来源:https://stackoverflow.com/questions/49527537/how-to-map-only-specified-fields-using-mapstruct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!