问题
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