Spring Boot Starter for ModelMapper.
- Supports upper Java 8
- Supports ModelMapper 2.3.5
- Supports Spring Boot 2.1.6, 2.2.0(experimental)
Add dependency modelmapper-spring-boot-starter
.
<dependency>
<groupId>io.github.yoshikawaa.modelmapper.spring.boot</groupId>
<artifactId>modelmapper-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
All you need is define a @Autowired
field.
@Autowired
private ModelMapper modelMapper;
public Destination sample(Source source) {
return modelMapper.map(source, Destination.class);
}
Define a TypeMapConfigurer
bean.
Please see Property Mapping.
@Bean
TypeMapConfigurer<Source, Destination> typeMap() {
return new TypeMapConfigurer<Source, Destination>() {
@Override
public void configure(TypeMap<Source, Destination> typeMap) {
typeMap.addMapping(Source::getName, Destination::setUsername);
}
};
}
Define a Converter
or AbstractConverter
bean.
Please see Converters.
@Bean
Converter<String, String> converter() {
return new Converter<String, String>() {
@Override
public String convert(MappingContext<String, String> context) {
return context.getSource() == null ? null : context.getSource().toUppercase();
}
};
}
Notice. Now,
Converter
for the entire application can not be defined as functional interface. See modelmapper#487.
Define a Provider
or AbstractProvider
bean.
Please see Providers.
@Bean
Provider<Object> provider() {
return request -> {
Object source = request.getSource();
return source != null && request.getRequestedType().isAssignableFrom(source.getClass()) ? source : null;
};
}
Notice. Only one
Provider
can be registered for the entire application. Please register specific typeProvider
using Property Mapping.
Define a Condition
bean.
Please see Conditional Mapping.
@Bean
Condition<String, String> condition() {
return context -> !StringUtils.isEmpty(context.getSource());
}
Notice. Only one
Condition
can be registered for the entire application. Please register specific typeCondition
using Property Mapping.
You can customize ModelMapper
freely to define a Module
bean.
@Bean
Module module() {
return modelMapper -> /* cutomize */;
}
Change the behavior of ModelMapper
.
Please see Configuration.
name | candidates |
---|---|
modelmapper.source-name-tokenizer | Camel Case ,Underscore |
modelmapper.source-name-transformer | Javabeans Accessor ,Javabeans Mutator |
modelmapper.source-naming-convention | Javabeans Accessor ,Javabeans Mutator ,None |
modelmapper.destination-name-tokenizer | Camel Case ,Underscore |
modelmapper.destination-name-transforme | Javabeans Accessor ,Javabeans Mutator |
modelmapper.destination-naming-convention | Javabeans Accessor ,Javabeans Mutator ,None |
modelmapper.matching-strategy | Loose ,Standard ,Strict |
modelmapper.field-access-level | public ,protected ,package_private ,private |
modelmapper.method-access-level | public ,protected ,package_private ,private |
modelmapper.field-matching-enabled | boolean |
modelmapper.ambiguity-ignored | boolean |
modelmapper.full-type-matching-required | boolean |
modelmapper.implicit-mapping-enabled | boolean |
modelmapper.skip-null-enabled | boolean |
modelmapper.collections-merge-enabled | boolean |
modelmapper.use-o-s-gi-class-loader-bridging | boolean |
modelmapper.deep-copy-enabled | boolean |
Register SpringProvider
use Spring BeanFactory to provide destination objects.
Please see Spring Integration.
name | candidates |
---|---|
modelmapper.spring-provider-enabled | boolean |
Validate Property Mapping. Please see Validation.
name | candidates |
---|---|
modelmapper.validate-enabled | boolean |
If you want to log Configuration, enable trace level log.
name | candidates |
---|---|
logging.level.io.github.yoshikawaa.modelmapper.spring.boot.autoconfigure | trace |