@Target(value={TYPE,METHOD}) @Retention(value=CLASS) public @interface Named
Will be used to to select the correct mapping methods when mapping a bean property type, element of an iterable type or the key/value of a map type.
Example (both methods of Titles
are capable to convert a string, but the ambiguity is resolved by applying
the qualifiers in @Mapping
:
@Named("TitleTranslator")
public class Titles {
@Named("EnglishToGerman")
public String translateTitleEG(String title) {
// some mapping logic
}
@Named("GermanToEnglish")
public String translateTitleGE(String title) {
// some mapping logic
}
}
@Mapper( uses = Titles.class )
public interface MovieMapper {
@Mapping( target = "title", qualifiedByName = { "TitleTranslator", "EnglishToGerman" } )
GermanRelease toGerman( OriginalRelease movies );
}
The following implementation of MovieMapper
will be generated:
public class MovieMapperImpl implements MovieMapper {
private final Titles titles = new Titles();
@Override
public GermanRelease toGerman(OriginalRelease movies) {
if ( movies == null ) {
return null;
}
GermanRelease germanRelease = new GermanRelease();
germanRelease.setTitle( titles.translateTitleEG( movies.getTitle() ) );
return germanRelease;
}
}
Mapping.qualifiedByName()
,
IterableMapping.qualifiedByName()
,
MapMapping.keyQualifiedByName()
,
MapMapping.valueQualifiedByName()
public abstract String value
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.