Package org.modelmapper
Class ModelMapper
java.lang.Object
org.modelmapper.ModelMapper
ModelMapper - Performs object mapping, maintains 
Configuration and stores TypeMaps.
 
 - To perform object mapping use map.
- To configure the mapping of one type to another use createTypeMap.
- To add mappings for specific properties use addMappingssupplying aPropertyMap.
- To configure ModelMapper use getConfiguration().
- To validate mappings use validate().
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescription<S,D> void addConverter(Converter<S, D> converter) Registers theconverterto use when mapping instances of typesStoD.<S,D> void addConverter(Converter<S, D> converter, Class<S> sourceType, Class<D> destinationType) Registers theconverterto use when mapping instances of typesStoD.<S,D> TypeMap<S, D> addMappings(PropertyMap<S, D> propertyMap) Adds mappings from thepropertyMapinto the TypeMap corresponding to source typeSand destination typeD.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType) Creates a TypeMap for thesourceTypeanddestinationTypeusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType) Creates a TypeMap for thesource's type anddestinationTypeusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeusing theconfiguration.<S,D> TypeMap<S, D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType) Creates an empty TypeMap for thesourceType,destinationType.<S,D> TypeMap<S, D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates an empty TypeMap for thesourceType,destinationType.Returns the ModelMapper's configuration.<S,D> TypeMap<S, D> getTypeMap(Class<S> sourceType, Class<D> destinationType) Returns the TypeMap for thesourceTypeanddestinationType, else returnsnullif none exists.<S,D> TypeMap<S, D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationTypeandtypeMapName, else returnsnullif none exists.Collection<TypeMap<?,?>> Returns all TypeMaps for the ModelMapper.<D> DMapssourceto an instance ofdestinationType.<D> DMapssourceto an instance ofdestinationType.voidMapssourcetodestination.voidMapssourcetodestination.<D> DMapssourceto an instance ofdestinationType.<D> DMapssourceto an instance ofdestinationType.registerModule(Module module) Register a module<S,D> TypeMap<S, D> Returns the TypeMap for thesourceType,destinationType, creates TypeMap automatically if none exists.<S,D> TypeMap<S, D> Returns the TypeMap for thesourceType,destinationType, andtypeMapNamecreates TypeMap automatically if none exists.voidvalidate()Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that aConverterwassetfor the TypeMap.
- 
Constructor Details- 
ModelMapperpublic ModelMapper()Creates a new ModelMapper.
 
- 
- 
Method Details- 
addConverterRegisters theconverterto use when mapping instances of typesStoD. Theconverterwill besetagainst TypeMap corresponding to theconverter's type argumentsSandD.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- converter- to register
- Throws:
- IllegalArgumentException- if- converteris null or if type arguments- Sand- Dare not declared for the- converter
- See Also:
 
- 
addConverterpublic <S,D> void addConverter(Converter<S, D> converter, Class<S> sourceType, Class<D> destinationType) Registers theconverterto use when mapping instances of typesStoD. Theconverterwill besetagainst TypeMap corresponding to theconverter's type argumentsSandD.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- converter- to register
- Throws:
- IllegalArgumentException- if- converteris null or if type arguments- Sand- Dare not declared for the- converter
- See Also:
 
- 
addMappingsAdds mappings from thepropertyMapinto the TypeMap corresponding to source typeSand destination typeD. Explicit mappings defined in thepropertyMapwill override any implicit mappings for the same properties.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- propertyMap- from which mappings should be loaded
- Returns:
- TypeMap corresponding to the propertyMap
- Throws:
- IllegalArgumentException- if- propertyMapis null
- ConfigurationException- if a configuration error occurs while adding mappings for the- propertyMap
 
- 
createTypeMapCreates a TypeMap for thesourceTypeanddestinationTypeusing the ModelMapper's configuration.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- sourceType-
- destinationType-
- Throws:
- IllegalArgumentException- if- sourceTypeor- destinationTypeare null
- IllegalStateException- if a TypeMap already exists for- sourceTypeand- destinationType
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMappublic <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeusing theconfiguration.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- sourceType-
- destinationType-
- configuration- to apply to TypeMap
- Throws:
- IllegalArgumentException- if- sourceType,- destinationTypeor- configurationare null
- IllegalStateException- if a TypeMap already exists for- sourceTypeand- destinationType
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMappublic <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- sourceType-
- destinationType-
- typeMapName-
- Throws:
- IllegalArgumentException- if- sourceType,- destinationTypeor- typeMapNameare null
- IllegalStateException- if a TypeMap already exists for- sourceType,- destinationTypeand- typeMapName
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMappublic <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing theconfiguration.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- sourceType-
- destinationType-
- typeMapName-
- configuration- to apply to TypeMap
- Throws:
- IllegalArgumentException- if- sourceType,- destinationType,- typeMapNameor- configurationare null
- IllegalStateException- if a TypeMap already exists for- sourceType,- destinationTypeand- typeMapName
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMapCreates a TypeMap for thesource's type anddestinationTypeusing the ModelMapper's configuration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- source-
- destinationType-
- Throws:
- IllegalArgumentException- if- sourceor- destinationTypeare null
- IllegalStateException- if a TypeMap already exists for- source's type and- destinationType
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMappublic <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeusing theconfiguration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- source-
- destinationType-
- configuration- to apply to TypeMap
- Throws:
- IllegalArgumentException- if- source,- destinationTypeor- configurationare null
- IllegalStateException- if a TypeMap already exists for- source's type and- destinationType
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMapCreates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- source-
- destinationType-
- typeMapName-
- Throws:
- IllegalArgumentException- if- source,- destinationTypeor- typeMapNameare null
- IllegalStateException- if a TypeMap already exists for- source's type,- destinationTypeand- typeMapName
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
createTypeMappublic <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing theconfiguration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
- S- source type
- D- destination type
- Parameters:
- source-
- destinationType-
- typeMapName-
- configuration- to apply to TypeMap
- Throws:
- IllegalArgumentException- if- source,- destinationType,- typeMapNameor- configurationare null
- IllegalStateException- if a TypeMap already exists for- source's type,- destinationTypeand- typeMapName
- ConfigurationException- if the ModelMapper cannot create the TypeMap
- See Also:
 
- 
getConfigurationReturns the ModelMapper's configuration.
- 
getTypeMapReturns the TypeMap for thesourceTypeanddestinationType, else returnsnullif none exists.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceTypeor- destinationTypeare null
- See Also:
 
- 
getTypeMappublic <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationTypeandtypeMapName, else returnsnullif none exists.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceType,- destinationTypeor- typeMapNameare null
- See Also:
 
- 
typeMapReturns the TypeMap for thesourceType,destinationType, creates TypeMap automatically if none exists.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceType,- destinationTypeare null
 
- 
typeMappublic <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationType, andtypeMapNamecreates TypeMap automatically if none exists.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceType,- destinationTypeor- typeMapNameare null
 
- 
emptyTypeMapCreates an empty TypeMap for thesourceType,destinationType.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceTypeor- destinationTypeare null, or- TypeMap<SourceType, DestinationTypealready defined in the TypeMapStore
 
- 
emptyTypeMappublic <S,D> TypeMap<S,D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates an empty TypeMap for thesourceType,destinationType.- Type Parameters:
- S- source type
- D- destination type
- Throws:
- IllegalArgumentException- is- sourceTypeor- destinationTypeare null, or- TypeMap<Source Type, DestinationTypealready defined in the TypeMapStore
 
- 
getTypeMapsReturns all TypeMaps for the ModelMapper.
- 
mapMapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()anddestinationTypethen one is created.- Type Parameters:
- D- destination type
- Parameters:
- source- object to map from
- destinationType- type to map to
- Returns:
- fully mapped instance of destinationType
- Throws:
- IllegalArgumentException- if- sourceor- destinationTypeare null
- ConfigurationException- if the ModelMapper cannot find or create a TypeMap for the arguments
- MappingException- if a runtime error occurs while mapping
 
- 
mapMapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destinationTypeandtypeMapNamethen one is created.- Type Parameters:
- D- destination type
- Parameters:
- source- object to map from
- destinationType- type to map to
- typeMapName- name of existing TypeMap to use mappings from
- Returns:
- fully mapped instance of destinationType
- Throws:
- IllegalArgumentException- if- source,- destinationTypeor- typeMapNameare null
- ConfigurationException- if the ModelMapper cannot find or create a TypeMap for the arguments
- MappingException- if a runtime error occurs while mapping
 
- 
mapMapssourcetodestination. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()anddestination.getClass()then one is created.- Parameters:
- source- object to map from
- destination- object to map to
- Throws:
- IllegalArgumentException- if- sourceor- destinationare null
- ConfigurationException- if the ModelMapper cannot find or create a TypeMap for the arguments
- MappingException- if an error occurs while mapping
 
- 
mapMapssourcetodestination. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destination.getClass()andtypeMapNamethen one is created.- Parameters:
- source- object to map from
- destination- object to map to
- typeMapName- name of existing TypeMap to use mappings from
- Throws:
- IllegalArgumentException- if- source,- destinationor- typeMapNameare null
- ConfigurationException- if the ModelMapper cannot find or create a TypeMap for the arguments
- MappingException- if an error occurs while mapping
 
- 
mapMapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()anddestinationTypethen one is created.To map a parameterized destination type, subclass TypeTokenand obtain its Type:Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType);- Type Parameters:
- D- destination type
- Parameters:
- source- object to map from
- destinationType- type to map to
- Returns:
- fully mapped instance of destinationType
- Throws:
- IllegalArgumentException- if- sourceor- destinationTypeare null
- ConfigurationException- if the ModelMapper cannot find or create the TypeMap
- MappingException- if a runtime error occurs while mapping
 
- 
mapMapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destination.getClass()andtypeMapNamethen one is created.To map a parameterized destination type, subclass TypeTokenand obtain its Type:Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType, "string-list");- Type Parameters:
- D- destination type
- Parameters:
- source- object to map from
- destinationType- type to map to
- typeMapName- name of existing TypeMap to use mappings from
- Returns:
- fully mapped instance of destinationType
- Throws:
- IllegalArgumentException- if- source,- destinationTypeor- typeMapNameare null
- ConfigurationException- if the ModelMapper cannot find or create the TypeMap
- MappingException- if a runtime error occurs while mapping
 
- 
validatepublic void validate()Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that aConverterwassetfor the TypeMap. If not, a ConfigurationException is thrown detailing any missing mappings.- Throws:
- ValidationException- if any TypeMaps contain unmapped properties
 
- 
registerModuleRegister a module- Parameters:
- module- a module for extension
 
 
-