gpt4 book ai didi

kotlin - Jackson - 无法从 START_OBJECT token 中反序列化 `java.util.ArrayList` 的实例

转载 作者:行者123 更新时间:2023-12-02 12:10:23 27 4
gpt4 key购买 nike

我正在尝试通过 HTTP 请求从我的背部获取一些数据,并将其映射为我的模型的 MutableList。

代码在两种情况下完全相同,但其中一种不起作用,并且我第一次遇到错误?我在 Google 上搜索过它,但所有代码示例都与我的完全不同,解决方案也是如此。

这是我遇到的错误:

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())

这是我发出 GET 请求的 API:

    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)

最佳答案

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

In order to deserialize Kotlin data classes we have to register Jackson’s KotlinModule on an ObjectMapper instance.

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

Module that adds support for serialization/deserialization of Kotlin classes and data classes. Previously a default constructor must have existed on the Kotlin object for Jackson to deserialize into the object. With this module, single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported.

关于kotlin - Jackson - 无法从 START_OBJECT token 中反序列化 `java.util.ArrayList` 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com