@Target(value=TYPE) @Retention(value=CLASS) public @interface Mapper
Example 1: Creating mapper
@Mapper
public interface CarMapper {
CarDto toCarDto(Car source);
}
Example 2: Use additional mappers with parameters uses()
, componentModel()
and injectionStrategy()
// we have MarkMapper (map field "mark" to field "name" to upper case)
@Mapper(componentModel = "spring")
public class MarkMapper {
public String mapMark(String mark) {
return mark.toUpperCase();
}
}
// we have CarMapper
@Mapper(
componentModel = "spring",
uses = MarkMapper.class,
injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface CarMapper {
@Mapping(source = "mark", target = "name")
CarDto convertMap(CarEntity carEntity);
}
// generates
@Component
public class CarMapperImpl implements CarMapper {
private final MarkMapper markMapper;
@Autowired
public CarMapperImpl(MarkMapper markMapper) {
this.markMapper = markMapper;
}
@Override
public CarDto convertMap(CarEntity carEntity) {
if ( carEntity == null ) {
return null;
}
CarDto carDto = new CarDto();
carDto.setName( markMapper.mapMark( carEntity.getMark() ) );
return carDto;
}
}
Modifier and Type | Optional Element and Description |
---|---|
Builder |
builder
The information that should be used for the builder mappings.
|
CollectionMappingStrategy |
collectionMappingStrategy
The strategy to be applied when propagating the value of collection-typed properties.
|
String |
componentModel
Specifies the component model to which the generated mapper should
adhere.
|
Class<?> |
config
A class annotated with
MapperConfig which should be used as configuration template. |
boolean |
disableSubMappingMethodsGeneration
If MapStruct could not find another mapping method or apply an automatic conversion it will try to generate a
sub-mapping method between the two beans.
|
String |
implementationName
Specifies the name of the implementation class.
|
String |
implementationPackage
Specifies the target package for the generated implementation.
|
Class<?>[] |
imports
Additional types for which an import statement is to be added to the generated mapper implementation class.
|
InjectionStrategy |
injectionStrategy
Determines whether to use field or constructor injection.
|
Class<? extends Annotation> |
mappingControl
Allows detailed control over the mapping process.
|
MappingInheritanceStrategy |
mappingInheritanceStrategy
The strategy to use for applying method-level configuration annotations of prototype methods in the interface
specified with
config() . |
NullValueCheckStrategy |
nullValueCheckStrategy
Determines when to include a null check on the source property value of a bean mapping.
|
NullValueMappingStrategy |
nullValueMappingStrategy
The strategy to be applied when
null is passed as source argument value to the methods of this mapper. |
NullValuePropertyMappingStrategy |
nullValuePropertyMappingStrategy
The strategy to be applied when a source bean property is
null or not present. |
ReportingPolicy |
typeConversionPolicy
How lossy (narrowing) conversion, for instance long to integer should be
reported.
|
Class<? extends Exception> |
unexpectedValueMappingException
Exception that should be thrown by the generated code if no mapping matches for enums.
|
ReportingPolicy |
unmappedSourcePolicy
How unmapped properties of the source type of a mapping should be
reported.
|
ReportingPolicy |
unmappedTargetPolicy
How unmapped properties of the target type of a mapping should be
reported.
|
Class<?>[] |
uses
Other mapper types used by this mapper.
|
public abstract Class<?>[] uses
public abstract Class<?>[] imports
Mapping.expression()
,
Mapping.defaultExpression()
or using
their simple name rather than their fully-qualified name.public abstract ReportingPolicy unmappedSourcePolicy
config()
public abstract ReportingPolicy unmappedTargetPolicy
config()
public abstract ReportingPolicy typeConversionPolicy
config()
public abstract String componentModel
default
: the mapper uses no component model, instances are
typically retrieved via Mappers.getMapper(Class)
cdi
: the generated mapper is an application-scoped CDI bean and
can be retrieved via @Inject
spring
: the generated mapper is a Spring bean and
can be retrieved via @Autowired
jsr330
: the generated mapper is annotated with @javax.inject.Named
and
@Singleton
, and can be retrieved via @Inject
config()
public abstract String implementationName
<CLASS_NAME>
will be replaced by the
interface/abstract class name.
Defaults to postfixing the name with Impl
: <CLASS_NAME>Impl
implementationPackage()
public abstract String implementationPackage
<PACKAGE_NAME>
will be replaced by the
interface's or abstract class' package.
Defaults to using the same package as the mapper interface/abstract class
implementationName()
public abstract Class<?> config
MapperConfig
which should be used as configuration template. Any settings given
via Mapper
will take precedence over the settings from the referenced configuration source. The list of
referenced mappers will contain all mappers given via uses()
and MapperConfig.uses()
.public abstract CollectionMappingStrategy collectionMappingStrategy
orderDto.addOrderLine()
).
Any setting given for this attribute will take precedence over MapperConfig.collectionMappingStrategy()
,
if present.
public abstract NullValueMappingStrategy nullValueMappingStrategy
null
is passed as source argument value to the methods of this mapper.
If no strategy is configured, the strategy given via MapperConfig.nullValueMappingStrategy()
will be
applied, using NullValueMappingStrategy.RETURN_NULL
by default.null
is passed as source value to the methods of this mapper.public abstract NullValuePropertyMappingStrategy nullValuePropertyMappingStrategy
null
or not present. If no strategy is
configured, the strategy given via MapperConfig.nullValuePropertyMappingStrategy()
will be applied,
NullValuePropertyMappingStrategy.SET_TO_NULL
will be used by default.null
is passed as source property value or the source property
is not present.public abstract MappingInheritanceStrategy mappingInheritanceStrategy
config()
. Annotations that can be inherited are for example Mapping
,
IterableMapping
, MapMapping
, or BeanMapping
.
If no strategy is configured, the strategy given via MapperConfig.mappingInheritanceStrategy()
will be
applied, using MappingInheritanceStrategy.EXPLICIT
as default.
@Mapping
configurations of prototype methods in the interface
specified with config()
.public abstract NullValueCheckStrategy nullValueCheckStrategy
MapperConfig
, BeanMapping
or Mapping
.public abstract InjectionStrategy injectionStrategy
InjectionStrategy.FIELD
will be used as default.public abstract boolean disableSubMappingMethodsGeneration
true
MapStruct will not try to
automatically generate sub-mapping methods.
Can be configured by the MapperConfig.disableSubMappingMethodsGeneration()
as well.
Note: If you need to use disableSubMappingMethodsGeneration
please contact the MapStruct team at
mapstruct.org or
github.com/mapstruct/mapstruct to share what problem you
are facing with the automatic sub-mapping generation.
public abstract Builder builder
MapperConfig.builder()
will be applied.
NOTE: In case no builder is defined here, in BeanMapping
or MapperConfig
and there is a single
build method, then that method would be used.
If the builder is defined and there is a single method that does not match the name of the build method then a compile error will occur
public abstract Class<? extends Annotation> mappingControl
DeepClone
,
NoComplexMapping
,
MappingControl
public abstract Class<? extends Exception> unexpectedValueMappingException
MapperConfig.unexpectedValueMappingException()
will be used, using IllegalArgumentException
by default.
Note:
String
parameter.
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.