Skip to content

Latest commit

 

History

History
90 lines (62 loc) · 2.44 KB

README.md

File metadata and controls

90 lines (62 loc) · 2.44 KB

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