You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin中ModelMapper无法映射自定义类的问题求助

Android Kotlin中ModelMapper无法映射自定义类的问题求助

我尝试用ModelMapper将AWThoughtDTO自定义对象映射到AWThought,但一直返回空对象,完全没映射成功。

AWThoughtDTO 类代码

class AWThoughtDTO(
    var id: Int = 0,
    var thought: String = "",
    var author: String = "",
    var authorEmail: String = "",
    var idLanguage: Int = 0,
    var active: Int = 0): Serializable {

    var painting: AWPaintingDTO = AWPaintingDTO()
    var trackId = 0
    var spotifyPreviewUrl: String = ""
    var trackIdI = 0
    var trackIdV = 0
    var trackIdIV = 0
    var calendar: Calendar = Calendar.getInstance()
}

我尝试用ModelMapper将AWThoughtDTO自定义对象映射到AWThought,但一直返回空对象,完全没映射成功。

AWThought 类代码

class AWThought(
    var id: Int = 0,
    var thought: String = "",
    var author: String = "",
    var authorEmail: String = "",
    var idLanguage: Int = 0,
    var active: Int = 0) : Serializable {

    var painting: AWPainting = AWPainting()
    var trackId = 0
    var spotifyPreviewUrl: String = ""
    var trackIdI = 0
    var trackIdV = 0
    var trackIdIV = 0
    var calendar: Calendar = Calendar.getInstance()

    override fun toString(): String {
        return thought
    }

    override fun equals(other: Any?): Boolean {
        if (other !is AWThought) {
            return false
        }
        return other.id == id
    }

    override fun hashCode(): Int {
        var result = id
        result = 31 * result + thought.hashCode()
        result = 31 * result + author.hashCode()
        result = 31 * result + authorEmail.hashCode()
        result = 31 * result + idLanguage
        result = 31 * result + active
        result = 31 * result + painting.hashCode()
        result = 31 * result + trackId
        result = 31 * result + spotifyPreviewUrl.hashCode()
        result = 31 * result + trackIdI
        result = 31 * result + trackIdV
        result = 31 * result + trackIdIV
        result = 31 * result + calendar.hashCode()
        return result
    }
}

ObjectMapperUtils 工具类代码

object ObjectMapperUtils {
    lateinit var modelMapper: ModelMapper

    private fun setDependencies(){
        val entryPoint = EntryPointAccessors.fromApplication(aw.context, Dependencies.IEntryPoint::class.java)
        this.modelMapper = entryPoint.modelMapper()
    }

    /**
     *
     * Note: outClass object must have default constructor with no arguments
     *
     * @param <D>      type of result object.
     * @param <T>      type of source object to map from.
     * @param entity   entity that needs to be mapped.
     * @param outClass class of result object.
     * @return new object of `outClass` type.
    </T></D> */
    fun <D, T> map(entity: T, outClass: Class<D>): D {
        return modelMapper.map(entity, outClass)
    }

    /**
     *
     * Note: outClass object must have default constructor with no arguments
     *
     * @param entityList list of entities that needs to be mapped
     * @param outCLass   class of result list element
     * @param <D>        type of objects in result list
     * @param <T>        type of entity in `entityList`
     * @return list of mapped object with `<D></D>` type.
    </T></D> */
    @JvmStatic
    fun <T, D> mapAll(entityList: Collection<T>?, outCLass: Class<D>?): MutableList<D> {
        val listType = TypeToken.getParameterized(
            MutableList::class.java, outCLass
        ).type
        return modelMapper.map(entityList, listType)
    }

    @JvmStatic
    fun <T, D> mapAllNull(entityList: Collection<T>?, outCLass: Class<D>?): MutableList<D>? {
        if (entityList.isNullOrEmpty()) return null
        val listType = TypeToken.getParameterized(
            MutableList::class.java, outCLass
        ).type
        return modelMapper.map(entityList, listType)
    }

    /**
     * Maps `source` to `destination`.
     *
     * @param source      object to map from
     * @param destination object to map to
     */
    fun <S, D> map(source: S, destination: D): D {
        modelMapper.map(source, destination)
        return destination
    }

    /*
     * Model mapper property setting are specified in the following block.
     * Default property matching strategy is set to Strict see {@link MatchingStrategies}
     * Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
     */
    init {
        setDependencies()
        modelMapper.configuration.matchingStrategy = MatchingStrategies.LOOSE
    }
}

映射代码

val art = ObjectMapperUtils.mapAllNull(awPreferences.alreadyReadThoughtsFromPreferences, AWThought::class.java)

当前结果

[ , , , ]

awPreferences.alreadyReadThoughtsFromPreferences 目前包含4个AWThoughtDTO对象,预期结果是一个包含4个AWThought元素的列表,但现在得到的是一个空内容的列表。

我接下来可以尝试什么?

备注:内容来源于stack exchange,提问作者Diego Perez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 08:58:06