All Projects → jmnarloch → Modelmapper Spring Boot Starter

jmnarloch / Modelmapper Spring Boot Starter

Licence: apache-2.0
Spring Boot Starter for ModelMapper http://modelmapper.org

Programming Languages

java
68154 projects - #9 most used programming language

Spring Boot ModelMapper starter

A Spring Boot starter that will help you configure ModelMapper within the application context.

Build Status Coverage Status

Features

Registers the ModelMapper as a Spring bean and allows to configure it and register specific object mappings.

Setup

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

<dependency>
    <groupId>com.github.jmnarloch</groupId>
    <artifactId>modelmapper-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

Additional configuration

If you need to set additonal configuration options simply register within Spring application context instance of ModelMapperConfigurer

@Component
public class CustomConfigurer implements ModelMapperConfigurer {
 
       void configure(ModelMapper modelMapper) {
           modelMapper.getConfiguration()
               .setSourceNamingConvention(NamingConventions.NONE);
               .setDestinationNamingConvention(NamingConventions.NONE);
       }
  }
  

Overriding the default mapping

Similarly registering the mapping can be performed through extending PropertyMapConfigurerSupport class.

@Component
public class UserDtoMapping extends PropertyMapConfigurerSupport<User, UserDto> {

   @Override
   public PropertyMap<User, UserDto> mapping() {

       return new PropertyMap<User, UserDto>() {
           @Override
           protected void configure() {
               map().setName(source.getFirstName());
           }
       };
   }
}
  

Additionally custom ModelMapper's converters can be registered:

@Component
public class UserDtoConverter extends ConverterConfigurerSupport<User, UserDTO> {
    @Override
    protected Converter<User, UserDTO> converter() {
        return new AbstractConverter<User, UserDTO>() {

            @Override
            protected UserDTO convert(User source) {
                String[] parts = source.getName().split(" ");
                return new UserDTO(parts[0], parts[1]);
            }
        };
    }
}

License

Apache 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].