A Spring Boot starter that let you use ModelMapper within your Spring Boot application
Switch branches/tags
Nothing to show
Clone or download

README.md

Spring Boot ModelMapper Starter

A Spring Boot starter that let you use ModelMapper within your Spring Boot application.

Base on jmnarloch modelmapper project with Modelmapper newest version adjustments.

Build Status Coverage Status Maven Central License

Features

Register the ModelMapper to your Spring Boot application and allows to configure it and register object mappings.

Setup

In order to add ModelMpaeer to your project simply add this dependency to your classpath:

<dependency>
    <groupId>com.github.rozidan</groupId>
    <artifactId>modelmapper-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
compile 'com.github.rozidan:modelmapper-spring-boot-starter:1.0.0'

For snapshots versions add the sonatype public repository:

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/groups/public" }
    ...
}

Change ModelMapper global configuration

In order to set change ModelMapper global configuration, simply register within Spring application context instance of MapperConfigurer:

@Component
public class GlobalConfiguration extends MapperConfigurer {
    @Override
    public void configure(Configuration configuration) {
        configuration.setSkipNullEnabled(true);
        configuration.setMatchingStrategy(MatchingStrategies.STRICT);
    }
}

Overriding the default mapping

In order to override the default mapping of some object types, lets say User -> UserDto, simply register within Spring application context instance of TypeMapConfigurer:

@Component
public class MapperConfigurer extends TypeMapConfigurer<User, UserDto> {
    @Override
    public void configure(TypeMap<User, UserDto> typeMap) {
        typeMap.addMapping(User::getAge, UserDto::setAge);
        typeMap.addMappings(mapping -> mapping.skip(UserDto::setMiddleName));
        typeMap.setPreConverter(context -> {
            String[] name = context.getSource().getName().split(" ");
            context.getDestination().setFirstName(name[0]);
            context.getDestination().setLastName(name[1]);
            return context.getDestination();
        });
        
    }
}

Custom converter

In order to register ModelMapper Converter, simply register within Spring application context instance of ConverterConfigurer:

@Component
public class SomeEnumTypeConverter extends ConverterConfigurer<ProductCategory, ProductCategoryDto> {
        @Override
        public Converter<ProductCategory, ProductCategoryDto> converter() {
            return new AbstractConverter<ProductCategory, ProductCategoryDto>() {
                @Override
                protected ProductCategoryDto convert(ProductCategory source) {
                    ...
                }
            };
        }
    }

Testing

Scan for mapping components with the WithModelMapper annotation

@RunWith(SpringRunner.class)
public class MapperTest {
    
    @Test
    public void someTest() {
        
    }
    
    @Configuration
    @WithModelMapper(basePackage = "com.company.program.mapping")
    public static class Application {
    }
}

License

Apache-2.0