spring-bootmapstructmapper

Map complex DTOs to Entity via Mapstruct


How can you effectively map a complex DTO that includes nested collections of objects to an entity in Spring Boot using the MapStruct library? I can map simple object, but what if I need to map this nested elements from collection?

I am using Kotlin and Springboot.

Some best practices?


Solution

  • you can use decorator on your mapper where you can call generated map function by MapStruct and extend result in your decorator.

    Example:

     open class BatchMapperDecorator : BatchMapperDecorated() { 
        // inject your mapper
        private var mapperProperty: YourMapper = 
        Mappers.getMapper(YourMapper::class.java)
            
        override fun mapMethod(source: DTO): YourEntity 
        {
            val result = mapperProperty.mapMethod(source)
            result.collection = source.collection.map { 
                … map logic … 
            }.toList()
    
            return result
    }