Framework/Spring Boot

ModelMapper 수동으로 필드명 매핑 설정

잔망루피 2022. 9. 12. 12:38
@Configuration
@RequiredArgsConstructor
public class CustomModelMapper {
    private final ModelMapper modelMapper;

    @Bean
    public ModelMapper standardMapper(){
        modelMapper.getConfiguration()
                .setMatchingStrategy(MatchingStrategies.STANDARD);
        modelMapper.createTypeMap(Post.class, PostResponseDto.class)
                .addMapping(Post :: getId, PostResponseDto :: setPostId)
                .addMapping(Post :: getTitle, PostResponseDto :: setPostTitle)
                .addMapping(Post :: getContent, PostResponseDto :: setPostContent);

        return modelMapper;
    }

}

addMapping()으로 매핑을 해주었다.

 

 

참고 👇

http://modelmapper.org/user-manual/property-mapping/

 

ModelMapper - Property Mapping

Property Mapping For most object models, ModelMapper does a good job of intelligently mapping source and destination properties. But for certain models where property and class names are very dissimilar, a PropertyMap can be created to define explicit mapp

modelmapper.org

 

 

 

 

반응형