Skip to content

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

License

Notifications You must be signed in to change notification settings

rozidan/modelmapper-spring-boot-starter

Repository files navigation

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 Sonatype Nexus (Snapshots)

License

Features

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

Setup

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

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

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