我在使用mapstruct正确映射嵌套dto字段时遇到问题。我有几个dto:
@Data
public class InstrumentGet {
.....
private MediaGet image;
private List<LocaleGet> locales;
}
@Data
public class LocaleGet {
private String name;
private String description;
private String iso6391;
private boolean defaultLanguage;
}
@Data
public class MediaGet {
private String uri;
private int width;
private int height;
}
带有相应的映射器
@Mapper(componentModel = "spring")
public interface LocaleMapper {
@Mapping(source = "culture.iso6391",target = "iso6391")
@Mapping(source = "culture.defaultLanguage",target = "defaultLanguage")
LocaleGet toDtoGet(BaseLocale locale);
List<LocaleGet> toDtosGet(List<BaseLocale> locales);
}
@Mapper( componentModel = "spring")
public interface MediaMapper {
MediaGet toDtoGet(Media media);
}
@Mapper(componentModel = "spring",uses = {MediaMapper.class, LocaleMapper.class})
public interface InstrumentMapper {
InstrumentGet instrumentToInstrumentGet(Instrument instrument, List<BaseLocale> locales);
}
到目前为止,一切都很好,生成的代码自动连接其他所需的映射器以正确构建dto。例如,生成的仪器映射器实现
@Component
public class InstrumentMapperImpl implements InstrumentMapper {
@Autowired
private MediaMapper mediaMapper;
@Autowired
private LocaleMapper localeMapper;
@Override
public InstrumentGet instrumentToInstrumentGet(Instrument instrument, List<BaseLocale> locales) {
if ( instrument == null && locales == null ) {
return null;
}
InstrumentGet instrumentGet = new InstrumentGet();
if ( instrument != null ) {
instrumentGet.setDefaultName( instrument.getDefaultName() );
instrumentGet.setImage( mediaMapper.toDtoGet( instrument.getImage() ) );
}
if ( locales != null ) {
instrumentGet.setLocales( localeMapper.toDtosGet( locales ) );
}
return instrumentGet;
}
}
现在,当我试图创建一个嵌套了instrumentdto的映射器时遇到了麻烦。映射器应该使用instrumentMapper来正确创建所需的dto。dto:
@Data
public class MemberGet {
......
private MediaGet image;
private InstrumentGet instrument;
......
}
映射器:
@Mapper(componentModel = "spring",uses = {InstrumentMapper.class, MediaMapper.class})
public interface ClientMapper {
@Mapping(source = "member.band.uid",target = "bandUid")
MemberGet toDtoMemberGet(Member member, List<BaseLocale> locales);
}
生成的代码:
@Component
public class ClientMapperImpl implements ClientMapper {
@Autowired
private MediaMapper mediaMapper;
@Override
public MemberGet toDtoMemberGet(Member member, List<BaseLocale> locales) {
if ( member == null && locales == null ) {
return null;
}
MemberGet memberGet = new MemberGet();
if ( member != null ) {
memberGet.setBandUid( memberBandUid( member ) );
memberGet.setPersonDetails( member.getPersonDetails() );
memberGet.setBirthday( member.getBirthday() );
memberGet.setGender( member.getGender() );
memberGet.setImage( mediaMapper.toDtoGet( member.getImage() ) );
memberGet.setInstrument( instrumentToInstrumentGet( member.getInstrument() ) );
}
return memberGet;
}
private String memberBandUid(Member member) {
if ( member == null ) {
return null;
}
Band band = member.getBand();
if ( band == null ) {
return null;
}
String uid = band.getUid();
if ( uid == null ) {
return null;
}
return uid;
}
protected InstrumentGet instrumentToInstrumentGet(Instrument instrument) {
if ( instrument == null ) {
return null;
}
InstrumentGet instrumentGet = new InstrumentGet();
instrumentGet.setDefaultName( instrument.getDefaultName() );
instrumentGet.setImage( mediaMapper.toDtoGet( instrument.getImage() ) );
return instrumentGet;
}
}
现在媒体映射器被很好地选中并使用了所需的方法,我对Instrument映射器有问题,mapstruct没有自动连接并调用所需方法,而是创建了自己的方法,这不适合我的需要。我显然在这里做错了什么。有人能给我一个提示或任何建议吗?
问候
当直接从另一个映射器使用具有多个源参数的映射器时,mapstruct中存在限制。您可以通过删除引用映射器的两个参数之一来自行测试它。
相反,您可以使用@BeforeMapping或@AfterMapping卷积器来解决您的问题。
上下文参数也可能起作用。