org.modelmapper.AbstractConverter Java Examples

The following examples show how to use org.modelmapper.AbstractConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConverterConfigurerSupportTest.java    From modelmapper-spring-boot-starter with Apache License 2.0 7 votes vote down vote up
@Bean
public ConverterConfigurerSupport<User, UserDto> userConverter() {
    return new 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]);
                }
            };
        }
    };
}
 
Example #2
Source File: DefaultModelMapperFactory.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * ModelMapperを返します。
 * 
 * @return
 */
public static ModelMapper create() {
    // ObjectMappingのためのマッパー
    val modelMapper = new ModelMapper();
    val configuration = modelMapper.getConfiguration();

    configuration.setPropertyCondition(
            // IDフィールド以外をマッピングする
            context -> {
                // DomaDtoのIDカラムは上書きしないようにする
                PropertyInfo propertyInfo = context.getMapping().getLastDestinationProperty();
                return !(context.getParent().getDestination() instanceof DomaDto
                        && propertyInfo.getName().equals("id"));
            });

    // 厳格にマッピングする
    configuration.setMatchingStrategy(MatchingStrategies.STRICT);

    // コンバーター
    val idToInt = new AbstractConverter<ID<?>, Integer>() {
        @Override
        protected Integer convert(ID<?> source) {
            return source == null ? null : source.getValue();
        }
    };

    modelMapper.addConverter(idToInt);

    return modelMapper;
}