kotlin Android ModelMapper:Map一个原始对象子属性的属性时出现问题

xu3bshqb  于 2023-10-23  发布在  Kotlin
关注(0)|答案(1)|浏览(132)

我有一个“Test”类,它有一个“Questions”属性,同时又有一个“Images”属性,我还有三个DTO类,“TestDTO”、“QuestionDTO”和“ImageDTO”(“Questions”和“Images”都是List)。
我可以用ModelMapper将TestMap到TestDTO,结果TestDTO具有包含数据的List QuestionDTO,但是即使List中的每个QuestionDTO都包含一个List ImageDTO,List中也包含空ImageDTO对象。
测试:

@DynamoDBTable(tableName = AWSTables.Tables.TESTS)
class Test(@get:DynamoDBHashKey(attributeName = TableTest.test_id) var idTest: String = "",
           @get:DynamoDBAttribute(attributeName = TableTest.test_type) var idTestType: String = "",
           @get:DynamoDBAttribute(attributeName = TableTest.test_subject) var idTestSubject: String = "",
           @get:DynamoDBAttribute(attributeName = TableTest.test_lng) var testLng: String = "",
           @get:DynamoDBAttribute(attributeName = TableTest.test) var testName: String = "",
           @get:DynamoDBAttribute(attributeName = TableTest.test_level) var testLevel: Int = 0,
           @get:DynamoDBAttribute(attributeName = TableTest.test_level_number) var testLevelNumber: Int = 0,
           @get:DynamoDBAttribute(attributeName = TableTest.time_limit) var timeLimit: Int = 0,
           @get:DynamoDBAttribute(attributeName = TableTest.permitted_errors) var permittedErrors: Int = 0,
           @get:DynamoDBAttribute(attributeName = TableTest.timestamp) var timestamp: Int = 0,
           @get:DynamoDBAttribute(attributeName = TableTest.f_p) var fp: String = ""
    ):Serializable {

    @get:DynamoDBAttribute(attributeName = AWSTables.TableTest.active)
    var active: String = ""
    var score = 0
    var questions: MutableList<Question> = mutableListOf()
    override fun toString(): String {
        return testName
    }
}

测试DTO:

class TestDTO : Serializable {
    var idTest: String = ""
    var idTestType: String = ""
    var idTestSubject: String = ""
    var testLng: String = ""
    var testName: String = ""
    var testLevel = 0
    var testLevelNumber = 0
    var timeLimit = 0
    var permittedErrors = 0
    var timestamp = 0
    var questions: MutableList<QuestionDTO> = mutableListOf()
    var answeredQuestions: List<AnswerDTO> = listOf()
    var active: String = ""
    var fp: String = ""
    var score = 0
    var recordTag: String = ""
    var chartTag: String = ""
    var testState4Records: String = ""
    var testScore4Records = 0
    var testResult = 0
    var testState: String = ""
    var timerState = 0
    var averageQuestionTime = 0
    var timerPreference = 0
    var correctAnswers = 0
    var incorrectAnswers = 0
    var notAnsweredQuestions = 0
    var testScore = 0
    var testTime: String = ""

    constructor()
    constructor(questions: MutableList<QuestionDTO>) {
        this.questions = questions
    }

    override fun toString(): String {
        return testName
    }
}

问题:

class QuestionDTO : Serializable {
    var idQuestion: String = ""
    var idTest: String = ""
    var idCorrectAnswer: String = ""
    var question: String = ""
    var instructions: String = ""
    var explanations: String = ""
    var timestamp = 0
    var questionNumber = 0
    var urlAudio: String = ""
    var images: MutableList<ImageDTO> = mutableListOf()
    var answers: MutableList<AnswerDTO> = mutableListOf()
}

ImageDTO:

class ImageDTO : Serializable {
    val idImage: String = ""
    val idQuestion: String = ""
    val image: String = ""
    val timestamp: Int = 0
    var saveMode: String = ""
}

Map方法:

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

我Map的地方:

override fun getTests(strSearchConfig: String): List<TestDTO>? {
    val active: String
    val tmVersion: String
    var filteredTests: List<Test>? = null
    var resultOK = true
    try {
        //get search configuration
        val objJSON = strSearchConfig.toJson()
        active = objJSON!!.getString("active")
        tmVersion = objJSON.getString("tmVersion")

        //unmodifiable lists with results stored in a class object
        val awsTables = readAllTables()

        //first generate list of tests object
        filteredTests = AWSHelper.getFilteredTests(active, tmVersion, awsTables)
    } catch (e: Exception) {
        resultOK = false
        e.logException()
    }
    return if(resultOK) mapAll(filteredTests, TestDTO::class.java) else null
}

我如何调查为什么会发生这种情况?

vlju58qv

vlju58qv1#

好吧,我自己回答。这个问题很微妙,以便一眼就能看出。“ImageDTO”类中的属性被声明为瓦尔,而不是var,这意味着它们是只读的。将所有声明从瓦尔更改为var,现在它可以正常工作了。

@JsonClass(generateAdapter = true)
class ImageDTO : Serializable {
    var idImage: String = ""
    var idQuestion: String = ""
    var image: String = ""
    var timestamp: Int = 0
    var saveMode: String = ""
}

相关问题