当前位置: 首页 > 知识库问答 >
问题:

Jackson-无法从START_OBJECT标记反序列化`java.util.ArrayList`实例

吴均
2023-03-14
D/OkHttp: Callback failure for call to http://10.0.2.2:5000/...
D/OkHttp: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
D/OkHttp:  at [Source: (String)"{"recurrenceId":1,"meetingId":4,"weekDay":21}"; line: 1, column: 1]
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
D/OkHttp:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
D/OkHttp:     at fr.intech.neith.api.RecurrenceApi.toDo(RecurrenceApi.kt:34)
        at fr.intech.neith.helpers.ApiHelper$GET$1.onResponse(ApiHelper.kt:35)
D/OkHttp:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
package fr.intech.neith.models

data class Recurrence(val recurrenceId: Int = 0,
                      val meetingId: Int = 0,
                      val weekDay: Array<Int> = arrayOf())
    package fr.intech.neith.api

import...

class RecurrenceApi(context: Context): IOnApiResponse {
    var itodo: IToDoRecurrenceList? = null
    override fun toDo(body: String) {
        val mapper = ObjectMapper()

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        //val reader: ObjectReader = mapper.reader().forType(Meeting.class);
        val reader = mapper.readValue<MutableList<Recurrence>>(body)
        itodo!!.toExecuteR(reader)
    }

    var client = OkHttpClient()
    var api = ApiHelper(client, context)
    var url: String = "http://10.0.2.2:5000" + "/api/recurrence" // 192.168.43.135 4G - 10.0.2.2 ÉMULATEUR

    fun getRecurrenceList(meetingId: Int, cb: IToDoRecurrenceList) {
        val url2: String = url + "/${meetingId}";
        itodo = cb
        api.GET(url2, this)
    }
}
package fr.intech.neith.models
import java.io.Serializable
import java.util.*

data class Meeting(val hostFirstName: String = "",
                   val hostLastName: String = "",
                   val estimatedTime: String = "",
                   val startDate: Date = Date(),
                   val meetingId: Int = 0,
                   val name: String = "",
                   val hasEnded: Boolean = false,
                   val hostId: Int = 0,
                   val recurrence: Array<Int> = arrayOf(),
                   val index: Int = 0,
                   val isRecurrent: Boolean = false): Serializable

package fr.intech.neith.api


import android.content.Context
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import fr.intech.neith.IOnApiResponse
import fr.intech.neith.IToDoList
import fr.intech.neith.helpers.*
import fr.intech.neith.models.Meeting
import okhttp3.OkHttpClient

class MeetingApi(context: Context): IOnApiResponse {
    var itodo: IToDoList<Meeting>? = null
    override fun toDo(body: String) {
        val mapper = ObjectMapper()

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        val reader = mapper.readValue<MutableList<Meeting>>(body)
        itodo!!.toExecute(reader)
    }

    var client = OkHttpClient()
    var api = ApiHelper(client, context)
    var url: String = "http://10.0.2.2:5000" + "/api/meeting" // 192.168.43.135 4G - 10.0.2.2 ÉMULATEUR

    fun getMeetingList(userId: Int, cb: IToDoList<Meeting>) {
        val url2: String = url + "/${userId}/names"
        itodo = cb
        api.GET(url2, this)
    }
}

谢谢你的回答。

编辑:这解决了问题:mapper.configure(deserializationfeature.accept_single_value_as_array,true)

共有1个答案

程成天
2023-03-14

从https://craftsmen.nl/kotlin-create-rest-services-using-jersey-and-jackson/:

为了反序列化Kotlin数据类,我们必须在ObjectMapper实例上注册Jackson的KotlinModule。

https://github.com/fasterxml/jackson-module-kotlin

 类似资料: