Springboot Modelmapper Starter

'A Springboot starter for modelmapper'

License

License

Categories

Categories

Spring Boot Container Microservices ModelMapper General Purpose Libraries Bean Mapping
GroupId

GroupId

com.github.rozidan
ArtifactId

ArtifactId

modelmapper-spring-boot-starter
Last Version

Last Version

2.3.1
Release Date

Release Date

Type

Type

jar
Description

Description

Springboot Modelmapper Starter
'A Springboot starter for modelmapper'
Project URL

Project URL

https://github.com/rozidan/modelmapper-spring-boot-starter.git
Project Organization

Project Organization

Idan Rozenfeld
Source Code Management

Source Code Management

https://github.com/rozidan/modelmapper-spring-boot-starter.git

Download modelmapper-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.rozidan/modelmapper-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.rozidan</groupId>
    <artifactId>modelmapper-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.rozidan/modelmapper-spring-boot-starter/
implementation 'com.github.rozidan:modelmapper-spring-boot-starter:2.3.1'
// https://jarcasting.com/artifacts/com.github.rozidan/modelmapper-spring-boot-starter/
implementation ("com.github.rozidan:modelmapper-spring-boot-starter:2.3.1")
'com.github.rozidan:modelmapper-spring-boot-starter:jar:2.3.1'
<dependency org="com.github.rozidan" name="modelmapper-spring-boot-starter" rev="2.3.1">
  <artifact name="modelmapper-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.rozidan', module='modelmapper-spring-boot-starter', version='2.3.1')
)
libraryDependencies += "com.github.rozidan" % "modelmapper-spring-boot-starter" % "2.3.1"
[com.github.rozidan/modelmapper-spring-boot-starter "2.3.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.modelmapper : modelmapper jar 2.3.0

runtime (3)

Group / Artifact Type Version
org.springframework : spring-beans jar
org.springframework : spring-context jar
org.springframework.boot : spring-boot-autoconfigure jar

Project Modules

There are no modules declared in this project.

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

Versions

Version
2.3.1
2.3.0
1.0.0