Skip to content

yoshikawaa/modelmapper-spring-boot-starter

Repository files navigation

modelmapper-spring-boot-starter

Build Status Codacy Badge Codacy Badge Sonatype Nexus (Snapshots) Maven Central License

Spring Boot Starter for ModelMapper.

Notes

  • Supports upper Java 8
  • Supports ModelMapper 2.3.5
  • Supports Spring Boot 2.1.6, 2.2.0(experimental)

Getting Start

Configure dependency

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>

Use ModelMapper

All you need is define a @Autowired field.

@Autowired
private ModelMapper modelMapper;

public Destination sample(Source source) {
    return modelMapper.map(source, Destination.class);
}

Features

Bean Based Configuration

Property Mapping

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);
        }
    };
}

Converters

Define a Converter or AbstractConverterbean. 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.

Providers

Define a Provider or AbstractProviderbean. 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 type Provider using Property Mapping.

Conditional 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 type Condition using Property Mapping.

More Customize

You can customize ModelMapper freely to define a Module bean.

@Bean
Module module() {
    return modelMapper -> /* cutomize */;
}

Property Based Configuration

Behavior

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

Spring Integration

Register SpringProvider use Spring BeanFactory to provide destination objects. Please see Spring Integration.

name candidates
modelmapper.spring-provider-enabled boolean

Validation

Validate Property Mapping. Please see Validation.

name candidates
modelmapper.validate-enabled boolean

Logging

If you want to log Configuration, enable trace level log.

name candidates
logging.level.io.github.yoshikawaa.modelmapper.spring.boot.autoconfigure trace

About

Spring Boot Starter for ModelMapper http://modelmapper.org

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages